String sql = "insert into t (name,photo) values('jack',?')"; PrepareStatement pstmt = conn.createPrepareStatement(sql); sql.setByte(1,photoBytesArray,byteArrayLength); //or //sql.setInputStream(1,photoFileStream,length); pstmt.executeUpdate();
我试过了有错 "upImage.java": Error #: 300 : method setInputStream(int, java.io.FileInputStream, int) not found in interface java.sql.PreparedStatement at line 24, column 12 请在帮忙
Connection conn = null; try{ DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver()); conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://IP:1433;DataBaseName=Moftec","sa","mymm"); } catch(Exception e ) { //捕捉异常并抛出IOException out.println("Can not connec to the database! The exception is " + e.toString()); } FileInputStream fis=null; File file = new File("E:/java/chart.jpg"); try{ fis = new FileInputStream(file); }catch(FileNotFoundException e){ out.println("Not find file!"); } PreparedStatement ps = conn.prepareStatement("Insert into gs_img (lei,years,img) values (?,?,?)"); ps.setString(1,"ivan"); ps.setInt(2,4); ps.setBinaryStream(3,fis,(int)file.length()); ps.executeUpdate(); ps.close(); try{ fis.close(); out.println("写进去了!"); }catch(IOException e){ out.println("fis cann't cloase!"); } 保证正确。
PrepareStatement pstmt = conn.createPrepareStatement(sql);
sql.setByte(1,photoBytesArray,byteArrayLength);
//or
//sql.setInputStream(1,photoFileStream,length);
pstmt.executeUpdate();
"upImage.java": Error #: 300 : method setInputStream(int, java.io.FileInputStream, int) not found in interface java.sql.PreparedStatement at line 24, column 12
请在帮忙
try{
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://IP:1433;DataBaseName=Moftec","sa","mymm");
}
catch(Exception e )
{
//捕捉异常并抛出IOException
out.println("Can not connec to the database! The exception is " + e.toString());
}
FileInputStream fis=null;
File file = new File("E:/java/chart.jpg");
try{
fis = new FileInputStream(file);
}catch(FileNotFoundException e){
out.println("Not find file!");
}
PreparedStatement ps = conn.prepareStatement("Insert into gs_img (lei,years,img) values (?,?,?)");
ps.setString(1,"ivan");
ps.setInt(2,4);
ps.setBinaryStream(3,fis,(int)file.length());
ps.executeUpdate();
ps.close();
try{
fis.close();
out.println("写进去了!");
}catch(IOException e){
out.println("fis cann't cloase!");
}
保证正确。
是因为装载驱动程序不一样的原因吧??
还是因为我数据库表的字段类型的不同,
我的数据库中用data存储图信息,类型为binary!!
真的很感谢你再给我解释一下
import java.sql.*;
import java.io.*;
public class upImage {
public static void main(String args[]) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection ("jdbc:odbc:SQLname","sa","");
File files = new File("d:\\saint.jpg");
FileInputStream fis=new FileInputStream(files);
PreparedStatement ps = conn.prepareStatement("INSERT INTO Image (id,name,data) VALUES (?,?,?)");
ps.setInt(1,4);
ps.setString(2, files.getName());
ps.setInputStream(3,fis,(int)files.length());
ps.executeUpdate();
ps.executeUpdate();
fis.close();
ps.close(); }catch (Exception e) {
System.out.println(e.toString());
} }}