完全跟着教程做的,一字不差,就是出错,代码如下:static void create() throws SQLException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
String sql = "insert into blob_test(big_bit) values (?) ";
ps = conn.prepareStatement(sql);
File file = new File("c:/Bluehills.rar");
InputStream in = new BufferedInputStream(new FileInputStream(file)); ps.setBinaryStream(1, in, (int) file.length());
// 4.执行语句
int i = ps.executeUpdate();
in.close(); System.out.println("i=" + i);
} finally {
JdbcUtils.close(rs, ps, conn);
}
}错误信息如下:
Exception in thread "main" 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 '?U?????
??&?|?V]??_?fC;???<????@???÷-?[?
??à<?x???úá??7p2?qù????¨ì<?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2022)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
at cn.itcast.jdbc.CopyOfBlobTest.create(CopyOfBlobTest.java:87)
at cn.itcast.jdbc.CopyOfBlobTest.main(CopyOfBlobTest.java:32)
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
String sql = "insert into blob_test(big_bit) values (?) ";
ps = conn.prepareStatement(sql);
File file = new File("c:/Bluehills.rar");
InputStream in = new BufferedInputStream(new FileInputStream(file)); ps.setBinaryStream(1, in, (int) file.length());
// 4.执行语句
int i = ps.executeUpdate();
in.close(); System.out.println("i=" + i);
} finally {
JdbcUtils.close(rs, ps, conn);
}
}错误信息如下:
Exception in thread "main" 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 '?U?????
??&?|?V]??_?fC;???<????@???÷-?[?
??à<?x???úá??7p2?qù????¨ì<?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2022)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
at cn.itcast.jdbc.CopyOfBlobTest.create(CopyOfBlobTest.java:87)
at cn.itcast.jdbc.CopyOfBlobTest.main(CopyOfBlobTest.java:32)
ps.setBinaryStream(1, in, (int) file.length()[/color]); 看你这句话,
而setBinaryStream(int parameterIndex,InputStream x);
参数问题?我觉得如果是真样应该报编译错才对,你这是运行期异常那应该是sql的错误,把sql拿到查询分析器里面执行了再说吧
找到这个default-character-set=utf8,
下边应该是有2个这个属性的配置,一个是客户端的,一个是服务端,
你要是弄不清楚,就把2个全给修成utf8就成了
把编码设置成utf8,重新启动mysql服务,
重新建数据库就ok啦
谢谢各位,明天结贴给分