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(); }