7

我尝试使用以下方法使用 JDBC 使用 PreparedStatement 插入 userId (int) 和 userName(String):

public boolean saveUser(int userId, String userName){
    boolean saveStatus = false;
    try {
        connection.setAutoCommit(true);
        String sql = "insert into testtable values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, userId);
        statement.setString(2, userName);
        saveStatus  = statement.execute(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }finally{
        Connector.closeConnections();
    }
    return saveStatus;
}

我得到以下堆栈跟踪:

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 '?,?)' at line 1

我究竟做错了什么?

4

2 回答 2

13

试试这个应该可以:

    try {
        connection.setAutoCommit(true);
        String sql = "insert into testtable values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, userId);
        statement.setString(2, userName);
        saveStatus  = statement.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return saveStatus;
}

PreparedStatement 是预编译语句。您不必在执行时提供 sql 字符串。

于 2013-09-08T05:08:59.213 回答
5

您的代码中有一个根本性错误。

PreparedStatement创建完成后,我们需要在不preparedStatement.executeUpdate();传递 SQL 的情况下再次调用

这是您问题的完整代码。

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class SampleMysql {

    public static Connection getConnection() throws SQLException {
        Driver drv = new com.mysql.jdbc.Driver();
        DriverManager.registerDriver(drv);
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","sample", "sample");
        return con;
    }

    public static int saveUser(int userId, String userName){
        int saveStatus = 0;
        Connection connection = null;
        try {
            connection = getConnection();
            connection.setAutoCommit(true);
            String sql = "INSERT INTO testtable VALUES(?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            preparedStatement.setString(2, userName);
            saveStatus  = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
        }
        return saveStatus;
    }

    public static void main(String[] a) {
        System.out.println(saveUser(1, "sample"));
    }
}

下面给出了表格脚本。

CREATE TABLE testtable(user_ID INT(10), user_name VARCHAR(10));
于 2013-09-08T05:21:54.617 回答