请问一下有谁做过在数据库中存储图片的项目啊.以前我都是在数据库中保存图片的路径,但是现在想在数据库中直接以二进制留得形式保存图片,不知道怎么写代码啊.我自己写了一个,但是老是NULL报错,请给为看一下,写过的做好能提供一下源代码,谢谢...===========================下面的是我的源代码,但是老是报错======================================
我建的数据库表(SQLServer)create table picture
(
pid int identity(1,1),primary key,
pname nvarchar(20) not null,
pic image not null
)================================================================================================
package com.panmin.test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class SavePicToDB {
private static PreparedStatement pst =null;
private static Connection con = null;
private static File file = null;
private static FileInputStream in = null;
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;database=picture", "sa", "123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
file = new File("D:/1.jpg");
int length = (int)file.length();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[length];
in.read(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pst = con.prepareStatement("insert into picture values(?,?)");
pst.setString(1,"小雅");
pst.setBinaryStream(2,in,(int)file.length());
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
==============================下面是报错信息===============================com.microsoft.sqlserver.jdbc.SQLServerException: 不能将值 NULL 插入列 'pic',表 'picture.dbo.picture';列不允许有 Null 值。INSERT 失败.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:156)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1373)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:371)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:322)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4003)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1550)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:160)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:133)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:290)
at com.panmin.test.SavePicToDB.main(SavePicToDB.java:46)
Exception in thread "main" java.lang.NullPointerException
at com.panmin.test.SavePicToDB.main(SavePicToDB.java:54)
我建的数据库表(SQLServer)create table picture
(
pid int identity(1,1),primary key,
pname nvarchar(20) not null,
pic image not null
)================================================================================================
package com.panmin.test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class SavePicToDB {
private static PreparedStatement pst =null;
private static Connection con = null;
private static File file = null;
private static FileInputStream in = null;
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;database=picture", "sa", "123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
file = new File("D:/1.jpg");
int length = (int)file.length();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[length];
in.read(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pst = con.prepareStatement("insert into picture values(?,?)");
pst.setString(1,"小雅");
pst.setBinaryStream(2,in,(int)file.length());
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
==============================下面是报错信息===============================com.microsoft.sqlserver.jdbc.SQLServerException: 不能将值 NULL 插入列 'pic',表 'picture.dbo.picture';列不允许有 Null 值。INSERT 失败.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:156)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1373)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:371)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:322)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4003)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1550)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:160)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:133)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:290)
at com.panmin.test.SavePicToDB.main(SavePicToDB.java:46)
Exception in thread "main" java.lang.NullPointerException
at com.panmin.test.SavePicToDB.main(SavePicToDB.java:54)
private static PreparedStatement pst =null;
private static Connection con = null;
private static File file = null;
private static FileInputStream in = null;
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;database=picture", "sa", "123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
file = new File("D:/1.jpg");
FileInputStream in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pst = con.prepareStatement("insert into picture values(?,?)");
pst.setString(1,"小雅");
pst.setBinaryStream(2,in,in.available());
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但用MYsql存储过,和楼主的方式不太一样如果想要直接存图片到数据库,我觉得还是用二进制的代码写比较好。具体怎么写,我也记不清了。你网上搜搜。但好像得转换成字节或者是流的形式。
// Prepare a statement to insert a record
String sql = "INSERT INTO mysql_all_table("
+ "col_boolean,"
+ "col_byte,"
+ "col_short,"
+ "col_int,"
+ "col_long,"
+ "col_float,"
+ "col_double,"
+ "col_bigdecimal,"
+ "col_string,"
+ "col_date,"
+ "col_time,"
+ "col_timestamp,"
+ "col_asciistream,"
+ "col_binarystream,"
+ "col_blob) "
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql); // Set the values
pstmt.setBoolean(1, true);
pstmt.setByte(2, (byte)123);
pstmt.setShort(3, (short)123);
pstmt.setInt(4, 123);
pstmt.setLong(5, 123L);
pstmt.setFloat(6, 1.23F);
pstmt.setDouble(7, 1.23D);
pstmt.setBigDecimal(8, new BigDecimal(1.23));
pstmt.setString(9, "a string");
pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis()));
pstmt.setTime(11, new Time(System.currentTimeMillis()));
pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis())); // Set the ascii stream
File file = new File("infilename1");
FileInputStream is = new FileInputStream(file);
pstmt.setAsciiStream(13, is, (int)file.length()); // Set the binary stream
file = new File("infilename2");
is = new FileInputStream(file);
pstmt.setBinaryStream(14, is, (int)file.length()); // Set the blob
file = new File("infilename3");
is = new FileInputStream(file);
pstmt.setBinaryStream(15, is, (int)file.length()); // Insert the row
pstmt.executeUpdate();
} catch (SQLException e) {
} catch (FileNotFoundException e) {
}
===================================================
Exception in thread "main" java.lang.NullPointerException
at com.panmin.test.SavePicToDB.main(SavePicToDB.java:42)
public static void main(String[] args) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
"jdbc:sqlserver://127.0.0.1:1433;database=picture", "sa", "123");
file = new File("D:/1.jpg");
FileInputStream in = new FileInputStream(file);
pst = con.prepareStatement("insert into picture values(?,?)");
pst.setString(1, "小雅");
pst.setBinaryStream(2, in, in.available());
pst.executeUpdate();
pst.close();
in.close();
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}