0

I have a new database.I want to insert manual values and picking table name from a database. But it shows an error.The following code i use.My row values are first one is a String and following are Integers(here=fcltyname is the only String)

 connection = (Connection) dbSource.getConnection();
        String qry = "SELECT studentName From batcha "; 
        System.out.println(fcltyName);

        stmt = (PreparedStatement)  connection.prepareStatement(qry);
            rs =  stmt.executeQuery();

            while (rs.next()) {

                String check=new String(rs.getString(("studentName")));
                String student = check.replaceAll("\\s","");

                Class.forName("com.mysql.jdbc.Driver");
                connection = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS);

                String sql1 = "INSERT IGNORE INTO "+student+" (fcltyName,CommunicationOral,Communicationwritten,Leadership,AnalyticalAbilities,Interpersonalskills,DecisionMakingSkills,SelfConfidence,Creativity,Punctualityregularity,GeneralAwareness,Commitment,HardWork)VALUES("+fcltyName+",0,0,0,0,0,0,0,0,0,0,0,0)"; 

                newStmt=(PreparedStatement) connection.prepareStatement(sql1); 

            newStmt.executeUpdate(sql1); 

            newStmt.close();

Error

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0,0,0,0,0,0,0,0,0,0,0,0)' at line 1
3
  • Why can't you use the same connection for insert query ? Commented Aug 14, 2013 at 11:05
  • your query goes to server wrong missing ( operator Commented Aug 14, 2013 at 11:06
  • 1
    You have to wrap fcltyName with simple quotes: VALUES('"+fcltyName+"'... Commented Aug 14, 2013 at 11:10

4 Answers 4

7

Try this once

'

    try {
        String insertTableSQL = "INSERT INTO DBUSER"+ "fcltyName,CommunicationOral,Communicationwritten,Leadership,AnalyticalAbilities,Interpersonalskills,DecisionMakingSkills,SelfConfidence,Creativity,Punctualityregularity,GeneralAwareness,Commitment,HardWork) VALUES"+ "(?,?,?,?,?,?,?,?,?,?,?,?,?)";

        dbConnection = getDBConnection();
        preparedStatement = dbConnection.prepareStatement(insertTableSQL);

        preparedStatement.setStringt(1, fcltyName);
        preparedStatement.setInt(2, 0);
        preparedStatement.setInt(3, 0);
        preparedStatement.setInt(4,0);
        preparedStatement.setInt(5, 0);
        preparedStatement.setInt(6, 0);
        preparedStatement.setInt(7,0);
        preparedStatement.setInt(8, 0);
        preparedStatement.setInt(9, 0);
        preparedStatement.setInt(10,0);
        preparedStatement.setInt(11, 0);
        preparedStatement.setInt(12, 0);
        preparedStatement.setInt(13,0);

        // execute insert SQL stetement
        preparedStatement.executeUpdate();}'
Sign up to request clarification or add additional context in comments.

Comments

1

try this one

String sql1 = "INSERT IGNORE INTO "+student+" (fcltyName,CommunicationOral,Communicationwritten,Leadership,AnalyticalAbilities,Interpersonalskills,DecisionMakingSkills,SelfConfidence,Creativity,Punctualityregularity,GeneralAwareness,Commitment,HardWork)VALUES('"+fcltyName+"',0,0,0,0,0,0,0,0,0,0,0,0)"; 

1 Comment

I would not recommend inserting values like this. It is prone to sql injection and the queries cannot be cached.
1

I think fcltyName is a varchar type so you need to insert it within 'fcltyName'

String sql1 = "INSERT IGNORE INTO "+student+" (fcltyName,CommunicationOral,Communicationwritten,Leadership,AnalyticalAbilities,Interpersonalskills,DecisionMakingSkills,SelfConfidence,Creativity,Punctualityregularity,GeneralAwareness,Commitment,HardWork)VALUES("+"'"+fcltyName+"'"+",0,0,0,0,0,0,0,0,0,0,0,0)";

2 Comments

@DeepthiDipin string in java but varchar in sql
@DeepthiDipin as suresh has said,try to use preparedstatement always
0

Do not insert values directly in the query, use a preparedstatment and feed the values as arguments.

See http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/ for an example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.