java mysql存blob,将Java对象存储为BLOB在MySQL中:奇怪的错误

got a little problem im hoping someone can enlighten me on.

Trying to serialise a Java Object of my own creation which is made up of other Java Objects (Again of my own creation). It was running fine until I get this error when the jdbc attempts to run the PreparedStatement storing the Object as a blob.

Im running MySQL as the database and have checked that all my objects which im attempting to store in the blob field are defined as "implements Serializable". I have attempted to try a normal BLOB datatype in MySQL and also MedBlob. Im running MySQL client version: 5.1.41 via Xampp.

Code is below:

LinkedList

//Ignore these two variables

String tName = "";

int modelCode = 0;

for (int i = 0; i< tt.size();i++){

tName = t.get(i).getTableName();

modelCode = session.getCurrentModel();

try {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(baos);

oos.writeObject(t.get(i));

byte[] tableAsBytes = baos.toByteArray();

fyProject.connectionController sg = new fyProject.connectionController(1);

PreparedStatement pstmt =

sg.prepareSQL

("INSERT INTO tableList (table) VALUES(?);");

ByteArrayInputStream bais =

new ByteArrayInputStream(tableAsBytes);

pstmt.setBinaryStream(1,bais, (long) tableAsBytes.length);

System.out.println(pstmt.toString());

//pstmt.setString(2, tName);

//pstmt.setInt(3, modelCode);

pstmt.executeUpdate();

sg.conn.commit();

pstmt.close();

conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

Error stack is below:

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 'table) VALUES(_binary'¬í\0sr\0fyProject.TableÊl;EÞ›\0L\0

attributest\0Lja' at line 1

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

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:1052)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)

at fyProject.connectionController.insertTable(connectionController.java:368)

at fyProject.connectionController.createTables(connectionController.java:292)

at fyProject.Main.main(Main.java:40)

Thanks for the help.

Lewis

EDIT: Fixed code is below.

public void insertTable() {

LinkedList

//Ignore these two variables

String tName = "";

int modelCode = 0;

for (int i = 0; i< tt.size();i++){

tName = tt.get(i).getTableName();

modelCode = session.getCurrentModel();

try {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(baos);

//Test a = new Test(1);

oos.writeObject(tt.get(i));

byte[] tableAsBytes = baos.toByteArray();

fyProject.connectionController sg = new fyProject.connectionController(1);

PreparedStatement pstmt =

sg.prepareSQL

("INSERT INTO tableList (tableField) VALUES(?);");

ByteArrayInputStream bais =

new ByteArrayInputStream(tableAsBytes);

pstmt.setBinaryStream(1,bais, (long) tableAsBytes.length);

System.out.println(pstmt.toString());

//pstmt.setString(2, tName);

//pstmt.setInt(3, modelCode);

pstmt.executeUpdate();

//sg.conn.commit();

pstmt.close();

conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}

解决方案

TABLE is a MySQL keyword. Since you use it as a name for your column, you need to escape the name like this:

INSERT INTO tableList (`table`) VALUES(?);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值