A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hacket 高级黑马   /  2013-4-5 11:29  /  2055 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static  void write(){
  2.                 Connection conn = null;
  3.                 PreparedStatement pstmt = null;
  4.                 try {
  5. //                         加载驱动
  6.                         Class.forName("com.mysql.jdbc.Driver");
  7. //                        建立连接
  8.                         String url ="jdbc:mysql://localhost:3306/mydb2";
  9.                         String user ="root";
  10.                         String password = "123456";
  11.                         conn = DriverManager.getConnection(url, user, password);
  12. //                        创建sql语句
  13.                         //String sql = "insert into test_blob(id,content) values(?,?)";     
  14.                         String sql = "insert into test_blob(id,content) values(?,?)";
  15.                         pstmt = conn.prepareStatement(sql);
  16.                         pstmt.setString(1, UUID.randomUUID().toString());
  17. //                        类加载器加载资源
  18.                         URL uri = BlobTest.class.getClassLoader().getResource("cn/zengfansheng/blob/money.jpg");
  19.                         File file = new File(URLDecoder.decode(uri.getPath(), "utf-8"));
  20.                         InputStream is = new FileInputStream(file);
  21.                         pstmt.setBinaryStream(2, is, (int)file.length());
  22. //                        执行sql语句
  23.                         int i = pstmt.executeUpdate();
  24.                         System.out.println(i>0?"数据写入数据库成功":"数据写入数据库失败");
  25.                 } catch (ClassNotFoundException e) {
  26.                         e.printStackTrace();
  27.                 } catch (SQLException e) {
  28.                         e.printStackTrace();
  29.                 } catch (FileNotFoundException e) {
  30.                         e.printStackTrace();
  31.                 } catch (UnsupportedEncodingException e) {
  32.                         e.printStackTrace();
  33.                 } finally{
  34. //                        关闭资源,从里到外
  35.                         try {
  36.                                 if(pstmt!=null)
  37.                                         pstmt.close();
  38.                         } catch (SQLException e) {
  39.                                 e.printStackTrace();
  40.                         }
  41.                         try {
  42.                                 if(conn!=null)
  43.                                         conn.close();
  44.                         } catch (SQLException e) {
  45.                                 e.printStackTrace();
  46.                         }
  47.                 }
  48.         }
复制代码
为什么会报这个错误,前面我存Clob时候代码也差不多,但是没有报错。
  1. 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 '????—??”????è????????é??é??Rè??U???[????—?é…??‰?.q???V??°8RMFè¤?????è?????p
  2. 7' at line 1
  3.         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
  4.         at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
  5.         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
  6.         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
  7.         at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
  8.         at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
  9.         at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
  10.         at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
  11.         at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
  12.         at cn.zengfansheng.blob.BlobTest.write(BlobTest.java:50)
  13.         at cn.zengfansheng.blob.BlobTest.main(BlobTest.java:20)
复制代码

评分

参与人数 2技术分 +2 收起 理由
滔哥 + 1 论坛参与分
陈丽莉 + 1

查看全部评分

5 个回复

倒序浏览
出错了要看报错的信息,根据这个去修改代码。在SQL 语句里有问题。应该不是数据库连接的问题。改下你的SQL代码、
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
回复 使用道具 举报
兄弟看看·我写的存入与读取
  1. public static void creat2() throws SQLException, IOException {
  2.                 Connection conn = null;
  3.                 PreparedStatement ps = null;
  4.                 ResultSet rs = null;
  5.                 InputStream in = null;
  6.                 try {
  7.                         //2.连接
  8.                         conn = (Connection) JdbcUtils.getConnection();
  9.                         //3.创建语句
  10.                         File file = new File("f:\\qq.jpg");
  11.                         in = new BufferedInputStream(new FileInputStream(file));
  12.                         String sql = "insert into blob_test(blob_tes) values(?)";
  13.                         ps = conn.prepareStatement(sql);
  14.                         ps.setBinaryStream(1, in, (int)file.length());
  15.                         //4.执行
  16.                         ps.executeUpdate();
  17.                         //5.处理结果
  18.                         System.out.println("插入成功!");
  19.                 } finally {
  20.                         in.close();
  21.                         JdbcUtils.free(rs, ps, conn);
  22.                 }
  23.         }
  24.         public static void read2() throws SQLException, IOException {
  25.                 Connection conn = null;
  26.                 PreparedStatement ps = null;
  27.                 ResultSet rs = null;
  28.                 InputStream in = null;
  29.                 try {
  30.                         //2.连接
  31.                         conn = (Connection) JdbcUtils.getConnection();
  32.                         //3.创建语句
  33.                         String sql = "select blob_tes from blob_test";
  34.                         ps = conn.prepareStatement(sql);
  35.                         //4.执行语句
  36.                         rs = ps.executeQuery();
  37.                         //5.处理
  38.                         FileOutputStream out = new FileOutputStream(new File("F:\\123.jpg"));
  39.                         while(rs.next()) {
  40.                                 //Clob clob = rs.getClob(1);
  41.                                 InputStream  ins = rs.getBinaryStream(1); //in --->read
  42.                                 byte buf[] = new byte[1024];
  43.                                
  44.                                 int len = ins.read(buf);
  45.                                 while(len != -1) {
  46.                                         out.write(buf, 0, len);
  47.                                         len = ins.read(buf);
  48.                                 }
  49.                                
  50.                                 out.close();
  51.                                 ins.close();
  52.                                 System.out.println("读取OK!");
  53.                         }
  54.                 } finally {
  55.                         JdbcUtils.free(rs, ps, conn);
  56.                 }
复制代码

评分

参与人数 2技术分 +2 收起 理由
滔哥 + 1 论坛参与分
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
冯超 发表于 2013-4-5 16:23
兄弟看看·我写的存入与读取

我的路径时中文路径,不一样的,你的是英文路径。
回复 使用道具 举报
原因就是路径中不能有中文,,你可以放在英文的目录下,这样问题可以解决的
回复 使用道具 举报
我也发一份代码给你看看把
  1. //将BLOB类型数据存入数据库
  2.         public static void write() {
  3.                 Connection conn = null;
  4.                 PreparedStatement pstmt = null;
  5.                 ResultSet rs = null;
  6.                 String sql = "insert into test_blob(id,content) values(?,?)";
  7.                 try {
  8.                         conn = JdbcUtil.getMySqlConnection();
  9.                         pstmt = conn.prepareStatement(sql);
  10.                         //绑定ID
  11.                         pstmt.setString(1,UUID.randomUUID().toString());
  12.                         //取得图片的路径
  13.                         URL url = Demo2.class.getClassLoader().getResource("cn/itcast/web/jdbc/config/d1.jpg");
  14.                         //封装成File对象
  15.                         File file = new File(url.getPath());
  16.                         //取得字节输入流
  17.                         InputStream is = new FileInputStream(file);
  18.                         //绑定CONTENT
  19.                         //参数1占位符的编号,从1开始
  20.                         //参数2文件字节输入流
  21.                         //参数3文件的大小
  22.                         pstmt.setBinaryStream(2,is,(int)file.length());
  23.                         int i = pstmt.executeUpdate();
  24.                         System.out.println(i>0?"成功":"失败");
  25.                 } catch (Exception e) {
  26.                 }finally{
  27.                         JdbcUtil.close(rs);
  28.                         JdbcUtil.close(pstmt);
  29.                         JdbcUtil.close(conn);
  30.                 }
  31.         }
  32.         //将BLOB类型数据从数据库中取出
  33.         public static void read() {
  34.                 Connection conn = null;
  35.                 PreparedStatement pstmt = null;
  36.                 ResultSet rs = null;
  37.                 String sql = "select * from test_blob";
  38.                 InputStream is = null;
  39.                 OutputStream os = null;
  40.                 try {
  41.                         conn = JdbcUtil.getMySqlConnection();
  42.                         pstmt = conn.prepareStatement(sql);
  43.                         rs = pstmt.executeQuery();
  44.                         if(rs.next()){
  45.                                 is = rs.getBinaryStream("content");
  46.                         }
  47.                 } catch (Exception e) {
  48.                 }finally{
  49.                         JdbcUtil.close(rs);
  50.                         JdbcUtil.close(pstmt);
  51.                         JdbcUtil.close(conn);
  52.                 }
  53.                 try {
  54.                         os = new FileOutputStream("d:\\d1.jpg");
  55.                         int len = 0;
  56.                         byte[] buf = new byte[1024];
  57.                         while( (len=is.read(buf))>0 ){
  58.                                 os.write(buf,0,len);
  59.                         }
  60.                 } catch (Exception e) {
  61.                 }finally{
  62.                         if(is!=null){
  63.                                 try {
  64.                                         is.close();
  65.                                 } catch (IOException e) {
  66.                                         e.printStackTrace();
  67.                                 }
  68.                         }
  69.                         if(os!=null){
  70.                                 try {
  71.                                         os.close();
  72.                                 } catch (IOException e) {
  73.                                         e.printStackTrace();
  74.                                 }
  75.                         }
  76.                 }
  77.         }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马