0

我有一个新数据库。我想插入手动值并从数据库中选择表名。但它显示一个错误。我使用以下代码。我的行值是第一个是 aString并且以下是整数(这里=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

4 回答 4

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();}'
于 2013-08-14T11:19:01.577 回答
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)"; 
于 2013-08-14T11:12:27.370 回答
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)";
于 2013-08-14T11:12:59.160 回答
0

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

有关示例,请参见http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/

于 2013-08-14T11:06:15.610 回答