在MySQL中将Java对象存储为BLOB:奇怪的错误
有点小问题,我希望有人能给我一些启发 尝试序列化我自己创建的Java对象,它由其他Java对象组成(同样是我自己创建的)。在jdbc试图运行将对象存储为blob的PreparedStatement之前,它一直运行良好,直到我遇到这个错误 我运行MySQL作为数据库,并检查了我试图存储在blob字段中的所有对象是否定义为“implements Serializable”。我尝试在MySQL和MedBlob中使用普通BLOB数据类型。Im通过Xampp运行MySQL客户端版本:5.1.41 代码如下:在MySQL中将Java对象存储为BLOB:奇怪的错误,java,mysql,object,blob,serializable,Java,Mysql,Object,Blob,Serializable,有点小问题,我希望有人能给我一些启发 尝试序列化我自己创建的Java对象,它由其他Java对象组成(同样是我自己创建的)。在jdbc试图运行将对象存储为blob的PreparedStatement之前,它一直运行良好,直到我遇到这个错误 我运行MySQL作为数据库,并检查了我试图存储在blob字段中的所有对象是否定义为“implements Serializable”。我尝试在MySQL和MedBlob中使用普通BLOB数据类型。Im通过Xampp运行MySQL客户端版本:5.1.41 代码如下
LinkedList<Table> tt = t;
//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();
}
}
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)
谢谢你的帮助
刘易斯
编辑:下面是固定代码
public void insertTable() {
LinkedList<Table> tt = t;
//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();
}
}
}
public void insertTable(){
LinkedList tt=t;
//忽略这两个变量
字符串tName=“”;
int modelCode=0;
对于(int i=0;i
表格是一个。由于您将其用作列的名称,因此需要如下转义名称:
INSERT INTO tableList (`table`) VALUES(?);
表
是一个。由于您将其用作列的名称,因此需要如下转义名称:
INSERT INTO tableList (`table`) VALUES(?);
谢谢你,菲德吉特!正如旁注一样,即使在使用上面的语句时,我也有一个问题,似乎表作为字段名不能在MySQL中使用,即使使用单引号括起来也是如此。我将字段名改为“tableField”(不带单引号),效果很好。我将为再次遇到此问题的任何人发布我的新java代码。实际上,您可以使用表作为列名,但需要使用倒勾(`)来转义查询中的表和列名。单引号用于字符串文本。真的吗?当我尝试插入tableList(表
)值(?)时;我得到了一个不同的错误,但是当我将字段名更改为tableField,然后将要读取的SQL插入tableList(tableField)值(?);我没有问题。奇怪的不管怎样,谢谢你的帮助已经让我抓狂了一天了。谢谢你!正如旁注一样,即使在使用上面的语句时,我也有一个问题,似乎表作为字段名不能在MySQL中使用,即使使用单引号括起来也是如此。我将字段名改为“tableField”(不带单引号),效果很好。我将为再次遇到此问题的任何人发布我的新java代码。实际上,您可以使用表作为列名,但需要使用倒勾(`)来转义查询中的表和列名。单引号用于字符串文本。真的吗?当我尝试插入tableList(表
)值(?)时;我得到了一个不同的错误,但是当我将字段名更改为tableField,然后将要读取的SQL插入tableList(tableField)值(?);我没有问题。奇怪的不管怎样,谢谢你的帮助,这件事让我挠头已经一天了。