java 数据库如何存取图片_图片存取数据库(java)

private String dbDriver;

private String dbURL;

private String dbUser;

private String dbPassword;

private Connection con;

private PreparedStatement ps;

private ResultSet rs;

dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

dbURL = "jdbc:sqlserver://192.168.99.18:1433;DatabaseName =pmmsysdb";

dbUser = "pmmteam1";

dbPassword = "123";

public void initDB() {//初始化数据库连接

try {

// Load Driver

Class.forName(dbDriver).newInstance();

// Get connection

con = DriverManager.getConnection(dbURL,dbUser, dbPassword);

}

catch (ClassNotFoundException e) {

System.out.println(e.getMessage());

}

catch (SQLException ex) {

// handle any errors

System.out.println("SQLException: " + ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("VendorError: " + ex.getErrorCode());

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

//图片保存到数据库

public boolean storeImg(String strFile,String orderID,String detailID){

boolean written = false;

if (con == null)

initDB();

else {

int id = 0;

File file = new File(strFile);

FileInputStream fis = null;

try {

fis = new FileInputStream(file);

ps = con.prepareStatement("update pmm_sofapopOrderDetail set picture=? where orderId=? and detailId=?");

ps.setBinaryStream(1, fis, (int) file.length());

ps.setString(2, orderID);

ps.setString(3, detailID);

ps.executeUpdate();

written = true;

}catch (SQLException e) {

written = false;

System.out.println("SQLException: "+ e.getMessage());

System.out.println("SQLState: "+ e.getSQLState());

System.out.println("VendorError: "+ e.getErrorCode());

e.printStackTrace();

}catch(IOException ioe){

ioe.printStackTrace();

}finally {

try {

ps.close();

}

catch (SQLException ex) {

}

try {

fis.close();

}

catch (IOException ex1) {

}

// close db con

try {

con.close();

}

catch (SQLException ex2) {

}

}

}

return written;

}

//从数据库下载图片

public InputStream showImg(String orderID,String detailID) {

InputStream in = null;

try{

if (con == null) initDB();

ps = con.prepareStatement(

"select picture from pmm_sofapopOrderDetail where orderid=? and detailid=?");

ps.setString(1, orderID);

ps.setString(2, detailID);

rs = ps.executeQuery();

rs.next();

in = rs.getBinaryStream("picture");

rs.close();

con.close();

}catch(SQLException s){

try {

rs.close();

con.close();

}

catch (SQLException ex) {

ex.printStackTrace();

}

s.printStackTrace();

}

return in;

}

InputStream is = showImg(dm.getString("orderid", i).trim(),                                 dm.getString("detailid", i)); //从数据库下载图片        File file = new File(fileName);        RandomAccessFile rFile = null;        try {          rFile = new RandomAccessFile(file, "rw");          rFile.seek(0);          byte bytes[] = new byte[1024];          int ch=0;          while((ch=is.read(bytes))!=-1)            rFile.write(bytes,0,ch);           rFile.close();         }        catch (Exception ex) {          ex.printStackTrace();        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值