Java SQL error 1064
I'm having trouble with some simple code but can't figure out what exactly I am doing wrong. Following is my code:
String userName = "username";
String password = "password";
String url = "jdbc:mysql://computername/proejct";
Connection conn=null;
String value="some_value";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
String query1= "SELECT * FROM table WHERE column_Name=?";
PreparedStatement psmt1 = conn.prepareStatement(query1);
psmt1.setString(1, value);
Boolean count=psmt1.execute(query1);
if(count==false)
{
//do something
}
psmt1.close();
}
catch (Exception e)
{
e.printStackTrace();
}
And this is the error I am getting:
com.mysql.jdbc.JDBC4PreparedStatement@5f989f84: SELECT * FROM source2 WHERE File_Name='Messages.java'
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.GeneratedConstructorAccessor3.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:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2512)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:781)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:624)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:98)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveUpdate(SearchAndUpdate.java:115)
at SearchAndUpdate.recursiveTraversal(SearchAnd开发者_C百科Update.java:55)
at SearchAndUpdate.main(SearchAndUpdate.java:40)
Am I doing something wrong with the preparedstements ?
I printed out the prepare-statement, this is what it looks like: SELECT * FROM source2 WHERE column_Name='Messages.java'
Based on this line:
check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
it looks like you statement is not being prepared. The ?
is being sent to MySQL.
I think this:
Boolean count=psmt1.execute(query1);
should be (edit)
ResultSet count=psmt1.executeQuery();
精彩评论