public class ImgTest {
public Connection getConnection() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ce) {
ce.printStackTrace();
}
try {
con = java.sql.DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=picture;",
"sa", "5587396lj");
} catch (SQLException se) {
se.printStackTrace();
}
return con;
}
public void getDate() {
try {
java.io.File f = new File("G:\\java\\imgTest\\test.jpg");
DataInputStream in = new DataInputStream(new BufferedInputStream(new
FileInputStream(f)));
int i = in.available();
byte[] buffer = new byte[i];
s = con.createStatement();
s.executeUpdate("INSERT INTO ImageTest values ('" + buffer + "')");
} catch (IOException se) {
System.out.println(se.getMessage());
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
public static void main(String[] args) {
ImgTest it = new ImgTest();
it.getConnection();
it.getDate();
}
private Connection con = null;
private Statement s = null;
private String str = null;
}
这个是把图片写进数据库的代码;
public class outImage {
public void getImage() {
ImgTest it = new ImgTest();
try {
Statement s = it.getConnection().createStatement();
ResultSet rs = s.executeQuery("select * from ImageTest");
while (rs.next()) {
buffer = rs.getBytes(1);
}
DataOutputStream out = new DataOutputStream(new
BufferedOutputStream(new FileOutputStream("2.jpg")));
out.write(buffer);
} catch (SQLException se) {
se.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
public static void main(String[] args)
{
outImage oi = new outImage();
oi.getImage();
}
private byte[] buffer;
}
这个是把图片从数据库中读出来的代码!读图片的操作读出来的图片什么都没有!谁能帮我改一下啊!
[此贴子已经被作者于2007-2-8 20:49:42编辑过]