simple setBytes() example with varbinary ?
Posted by: Colin Rosenthal
Date: July 11, 2006 12:04AM

I am trying to use setBytes() for a varbinary. My connector is 3.0.17-ga
and mysql is 5.0.22. Here is the code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.Random;

public class SetBytesTest {

private static final String CONNECTION_STRING = "jdbc:mysql://localhost:3306/test?user=root";
private static final String DROP = "DROP TABLE IF EXISTS tbl_test";
private static final String CREATE = "CREATE TABLE tbl_test (salt varbinary(40))";

private static final String SINGLE_INSERT = "INSERT INTO TABLE tbl_test (salt) VALUES (?);";


public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(CONNECTION_STRING);
Statement stmt = conn.createStatement();
stmt.execute(DROP);
stmt.execute(CREATE);

Random rand = new Random();
byte[] salt = new byte[5];
rand.nextBytes(salt);

PreparedStatement pstmt = conn.prepareStatement(SINGLE_INSERT);
pstmt.setBytes(1, salt);
pstmt.executeUpdate();

}
}

And here is the exception I get:

Exception in thread "main" java.sql.SQLException: Syntax error or access violation message from server: "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 'TABLE tbl_test (salt) VALUES (_binary'')' at line 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1168)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1279)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2281)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1825)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1667)
at SetBytesTest.main(SetBytesTest.java:58)

Any suggestions? Something needs to be set in my connection properties?
--
Colin Rosenthal
Denmark



Edited 1 time(s). Last edit at 07/12/2006 02:20PM by Markus Popp.

Options: ReplyQuote


Subject
Written By
Posted
simple setBytes() example with varbinary ?
July 11, 2006 12:04AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.