Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
File file = new File(fileName);
InputStream photoStream = new FileInputStream(file);
sql = "INSERT INTO picture VALUES (?)";
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int) file.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
我创建了一个表,就一列,字段属性为longblob,用上面的代码实现把文件读取成二进制流并存入数据库。以上的代码中有一个JFileChooser用来选择文件,fileName就是选择的文件名,现在的情况是:我可以把文本文档读取成二进制流并顺利存入数据库,可是读取图片时出现异常,查询语句无法执行,这是怎么回事呢,诸位大侠帮忙啊!!!!!!!!!!!!!!
String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
File file = new File(fileName);
InputStream photoStream = new FileInputStream(file);
sql = "INSERT INTO picture VALUES (?)";
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int) file.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
我创建了一个表,就一列,字段属性为longblob,用上面的代码实现把文件读取成二进制流并存入数据库。以上的代码中有一个JFileChooser用来选择文件,fileName就是选择的文件名,现在的情况是:我可以把文本文档读取成二进制流并顺利存入数据库,可是读取图片时出现异常,查询语句无法执行,这是怎么回事呢,诸位大侠帮忙啊!!!!!!!!!!!!!!