要向数据库写入图片,提示"PreparedStatement pst.setBinaryStream(1, isImage,(int)(fImage.length()));"这句出错,我的数据库类型是Blob的,另外,当读取图片时,也是提示"InputStream isImage = rs2.getBinaryStream("MSG_INFO1");"这句出错,请问是什么原因呢?不胜感激啊~~~
调试欢乐多
java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.Unused.unused(Unused.java:29)
at org.sqlite.Unused.setBinaryStream(Unused.java:58)以上就是出错信息了,谢谢!
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;public class ImaTest {
public static void insert(File file){
Connection conn = null;
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/test";
conn = DriverManager.getConnection(url,"root","123");
String sql = "insert into image values(?)";
ps = conn.prepareStatement(sql);
InputStream in = new FileInputStream(file);
ps.setBinaryStream(1, in,(int) file.length());
ps.execute();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(ps != null) ps.close();
if(conn !=null) conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static void find(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/test";
conn = DriverManager.getConnection(url,"root","123");
String sql = "select * from image";
ps = conn.prepareStatement(sql);
InputStream in = null;
rs = ps.executeQuery();
if(rs.next())
in = rs.getBinaryStream(1);
FileOutputStream fos = new FileOutputStream("a.gif");
int data;
while((data = in.read()) != -1){
fos.write(data);
}
in.close();
fos.close();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(ps != null) ps.close();
if(conn !=null) conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static void main(String[] args){
URL url = ClassLoader.getSystemResource("arrow_down.gif");
File f = null;
try {
f = new File(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
//插入
ImaTest.insert(f);
//读取
ImaTest.find();
}
}
package org.chy; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; public class ImaTest {
public static void insert(File file){
Connection conn = null;
PreparedStatement ps = null;
try{
Class.forName( "com.mysql.jdbc.Driver ");
String url = "jdbc:mysql://127.0.0.1:3306/test ";
conn = DriverManager.getConnection(url, "root ", "123 ");
String sql = "insert into image values(?) ";
ps = conn.prepareStatement(sql);
InputStream in = new FileInputStream(file);
ps.setBinaryStream(1, in,(int) file.length());
ps.execute();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(ps != null) ps.close();
if(conn !=null) conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
} public static void find(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
Class.forName( "com.mysql.jdbc.Driver ");
String url = "jdbc:mysql://127.0.0.1:3306/test ";
conn = DriverManager.getConnection(url, "root ", "123 ");
String sql = "select * from image ";
ps = conn.prepareStatement(sql);
InputStream in = null;
rs = ps.executeQuery();
if(rs.next())
in = rs.getBinaryStream(1);
FileOutputStream fos = new FileOutputStream( "a.gif ");
int data;
while((data = in.read()) != -1){
fos.write(data);
}
in.close();
fos.close();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(ps != null) ps.close();
if(conn !=null) conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
} public static void main(String[] args){
URL url = ClassLoader.getSystemResource( "arrow_down.gif ");
File f = null;
try {
f = new File(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
} //插入
ImaTest.insert(f);
//读取
ImaTest.find();
}
}