package com.demo.database;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;public class TestImage { public static Connection con = null; public static Statement stat = null; public static FileInputStream fs = null; public static PreparedStatement ps = null; public static File file = null; public static void main(String args[]) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;Database=MyDataBase";
con = DriverManager.getConnection(url, "sa", "");
stat = con.createStatement();
System.out.println("连接上.....");
file = new File("D:/struts-power.gif");
fs = new FileInputStream(file);
//  System.out.println(fs.available());
ps = con
.prepareStatement("insert into images values(?,?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fs,fs.available());
ps.executeUpdate();
System.out.println("成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
异常:"java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'images' 无效"