Problems with PreparedStatement when select restrict are byte params
Hi,
i have a problem trying to select information when the "select" has a byte restrict.
The table in database is:
CREATE TABLE `positions` (
`PKID` int(10) unsigned NOT NULL auto_increment,
`POSCODE` varbinary(30) NOT NULL,
`ISWTURN` binary(1) NOT NULL,
`QTT_GAMES` int(10) unsigned NOT NULL default '1',
PRIMARY KEY (`PKID`),
UNIQUE KEY `UNIQ_POS` (`POSCODE`,`ISWTURN`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
And the test code to get the qtt_games is :
conn = DriverManager.getConnection (url,user,pwd);
byte bcode[] = poscode.getByteArrayCode();
// bcode is inserted ok in another preparedstatement...
String query = "SELECT qtt_games FROM positions "+
"WHERE poscode=? and iswturn=?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setBytes (1,bcode);
pstmt.setByte (2,poscode.getIsWhiteTurn()); //it returns a byte
ResultSet rs = pstmt.executeQuery (query);
When pstmt.executeQuery is reached, it's thrown the exception:
com.mysql.jdbc.exceptions.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 '=? and iswturn=?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3099)
at com.mysql.jdbc.Statement.execute(Statement.java:695)
at app.server.bbdd.MYSQLBDManager.getGamesBasicInfo(MYSQLBDManager.java:942)
at app.server.bbdd.MYSQLBDManager.main(MYSQLBDManager.java:1068)
Can anybody tell me what's wrong? I think the query is ok, but don't know what's happening with this...
Lots of thanks.
# 3
Hi,
sorry, i know i've post this same message by error in "new to java" thread.... (i'm new in the forum... can i delete the wrong comment?)
The SQLState is 42000 (syntax error), but it doesn't give me much information because i had already searched in google why can be the cause of it, but there are only a few comments about problems like this, without a solution...
The column poscode in the table positions contains an array of bytes that codify a position in a chess board. I've to use this in the WHERE clause because i'm developing a chess game consulting project where there are millions of different positions and i've to get the games that have the same position in it's position history.
The code to insert the positions is:
query = "INSERT INTO positions VALUES "+
"(null,?,?,default) "+
"ON DUPLICATE KEY UPDATE qtt_games=qtt_games+1";
pstmt = conn.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
pstmt.setBytes(1,bcode);
pstmt.setByte(2,poscode.getIsWhiteTurn());
pstmt.executeUpdate();
which works ok and positions can be seen from mysql browser, but i can't select them using the PreparedStatement..
I have been searching a lot of information about this problem with no results... this is the why i posted this...
Any help will be useful, thanks.
# 4
This code (yours) is wrong.
String query = "SELECT qtt_games FROM positions "+
"WHERE poscode=? and iswturn=?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setBytes (1,bcode);
pstmt.setByte (2,poscode.getIsWhiteTurn()); //it returns a byte
ResultSet rs = pstmt.executeQuery (query);
This is not the way to execute a PreparedStatement and the MySQL error message should have been a big clue when it said it was having trouble parsing the ?
Once the query is prepared (parsed) you bind the values and just call executeQuery(). Do NOT use pass your original query again.
So like this
String query = "SELECT qtt_games FROM positions "+
"WHERE poscode=? and iswturn=?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setBytes (1,bcode);
pstmt.setByte (2,poscode.getIsWhiteTurn()); //it returns a byte
ResultSet rs = pstmt.executeQuery ();// NOTE CHANGE HERE!