//先查询出来(for update是重点),然后再插入插入照片
String sqlzp="select zp from T_EXAM_PROCESS where id='"+examProcess.getId()+"' for update";
if(!examProcess.getZp().equals("")&&examProcess.getZp()!=null) {
List<String> keyList2 = new ArrayList<String>();
keyList2.add("zp");
oracleUtil.searchSql6(sqlzp, newdburl,newdbname,newdbpassword,keyList2,examProcess.getZp());
}
public static void searchSql6(String sqlzp,String newdburl, String newdbname, String newdbpassword,List<String> keyList2,String zpStr) {
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
System.out.println("开始尝试连接数据库!");
String url = "jdbc:oracle:" + newdburl;// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
String user = newdbname;// 用户名,系统默认的账户名
String password = newdbpassword;// 你安装时选设置的密码
con = DriverManager.getConnection(url, user, password);// 获取连接
System.out.println("连接成功!");
pre = con.prepareStatement(sqlzp);// 实例化预编译语句
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
while (result.next()) {
for (String key:keyList2) {
if (key.equals("zp")) {
Blob imageBlob=result.getBlob("zp");
String name = UUID.randomUUID().toString();
OutputStream os = imageBlob.setBinaryStream(1);
String filepath = "D:/tempZp/" +name + ".jpg";
//将Base64转成照片
ImageBase64.generateImage(zpStr,filepath);
InputStream ist = new FileInputStream(filepath);
int i = 0;
while ((i = ist.read()) != -1) {
os.write(i); //Blob的输入流,相当于输入到数据库中
}
os.flush();
os.close();
con.commit();
con.setAutoCommit(true);// 恢复现场
File file = new File(filepath);
file.delete();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}