java mysql编写的声明

时间:2014-03-07 21:17:14

标签: java mysql sql

我正在尝试使用java进行数据库的简单插入,并告诉我我的sql语法已关闭。但是当我复制我打印出的字符串并将其放入phpmyadmin的sql命令时,它正确地执行命令,我似乎无法弄清楚java中的字符串查询是否有错误。

java.sql.PreparedStatement statement;

String physicanInsertQuery = "INSERT INTO physician_phi VALUES(?,?,?,?,?,?,?,?)";
statement = ddp.getConnect().prepareStatement(physicanInsertQuery);

statement.setString(1, "id");
statement.setString(2, "first");
statement.setString(3, "last");
statement.setString(4, "middle");
statement.setString(5, "male");
statement.setString(6, "8179999999");
statement.setString(7, "test@gmail.com");
statement.setString(8, "1991-5-15");
System.out.println(statement.toString());
statement.executeUpdate(physicanInsertQuery);
statement.close();

这是在我执行之前打印出来的查询。

INSERT INTO physician_phi VALUES('FML123','FirstName','LastName','MiddleName','Male','8179999999','john@server.department.company.com','1991-2-7')

这是它在控制台中显示的错误

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
    at sun.reflect.GeneratedConstructorAccessor26.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at command.up.DoctorStore.exec(DoctorStore.java:65)

....

这是db中的表。 enter image description here

2 个答案:

答案 0 :(得分:5)

您正在调用基本Statement.executeUpdate(String)方法,该方法与PreparedStatement无关,只是执行原始SQL字符串。

您需要在没有参数的情况下调用statement.executeUpdate()来使用派生方法n PreparedStatement

答案 1 :(得分:-1)

你不需要终止;在你的SQL语句上。此外,您应该查看JPA,它将执行所有此数据库查询本身