由于查询导致的数据库插入错误

时间:2013-08-14 10:59:46

标签: java database

我有一个新的数据库。我想从数据库中插入手动值和选择表名。但是它显示了一个错误。我使用下面的代码。我的行值是第一个是String,后面是整数(这里= fcltyname是唯一的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();

错误

 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

4 个答案:

答案 0 :(得分:7)

尝试一次

    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();}'

答案 1 :(得分:1)

尝试这个

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 :(得分:1)

我认为fcltyName是一个varchar类型,所以你需要在'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)";

答案 3 :(得分:0)

不要直接在查询中插入值,使用预备语句并将值作为参数提供。

请参阅http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/以获取示例