package com.fgkj.db;
|
|
import java.sql.Connection;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.Date;
|
|
import com.fgkj.actions.ActionUtil;
|
import com.fgkj.dao.DAOHelper;
|
|
public class Upload {
|
//文件上传用到
|
public static int getBattTestRecordCountNew(int bg_id, String table,Connection mysql_con)
|
{
|
String[] sql_strs = new String[4];
|
sql_strs[0] = "SELECT test_record_count_ex FROM " + table
|
+ " WHERE BattGroupId=" + bg_id + " FOR UPDATE";
|
sql_strs[1] = "UPDATE " + table + " SET test_record_count_ex=test_record_count+1"
|
+ " WHERE BattGroupId=" + bg_id;
|
sql_strs[2] = "SELECT MAX(test_record_count_ex) FROM " + table
|
+ " WHERE BattGroupId=" + bg_id;
|
sql_strs[3] = "INSERT INTO " + table + " (BattGroupId,test_record_count, test_record_count_ex) "
|
+ " VALUES (" + bg_id + "," + 1 + "," + 1 + ")";
|
return getIdNewByLock(sql_strs, 1 ,mysql_con);
|
}
|
|
//文件上传用到
|
public static int getIdNewByLock(String[] sql_strs, int id_init,Connection mysql_con)
|
{
|
|
if(sql_strs.length < 4) {
|
return 0;
|
}
|
int count_id = 0;
|
boolean res_exe = true;
|
String sql_str0 = sql_strs[0];
|
String sql_str1 = sql_strs[1];
|
String sql_str2 = sql_strs[2];
|
String sql_str3 = sql_strs[3];
|
PreparedStatement ps=null;
|
ResultSet res=null;
|
int judge=1;
|
|
try {
|
mysql_con.setAutoCommit(false);
|
ps= mysql_con.prepareStatement(sql_str0);
|
res=ps.executeQuery();
|
//ResultSet res = sqlMysqlQuery(sql_str0);
|
if(res.next()) {
|
ps=mysql_con.prepareStatement(sql_str1);
|
judge=ps.executeUpdate();
|
if(0 == judge) {
|
res_exe = false;
|
}
|
ps= mysql_con.prepareStatement(sql_str2);
|
res=ps.executeQuery();
|
//res = sqlMysqlQuery(sql_str2);
|
if(res.next()) {
|
count_id = res.getInt(1);
|
}
|
} else {
|
count_id = id_init;
|
ps=mysql_con.prepareStatement(sql_str3);
|
judge=ps.executeUpdate();
|
if(0 == judge) {
|
res_exe = false;
|
}
|
}
|
// try {
|
// System.out.println("开始休眠30分钟");
|
// Thread.sleep(20*1000);
|
// } catch (InterruptedException e) {
|
// e.printStackTrace();
|
// }
|
// System.out.println("休眠结束");
|
if(true == res_exe) {
|
mysql_con.commit();
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
res_exe = false;
|
} finally {
|
if(false == res_exe) {
|
try {
|
mysql_con.rollback();
|
mysql_con.setAutoCommit(true);
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return count_id;
|
}
|
|
}
|