|
|
@@ -11,7 +11,9 @@ import java.sql.*;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
public class JdbcUtils {
|
|
|
|
|
|
@@ -25,6 +27,7 @@ public class JdbcUtils {
|
|
|
try {
|
|
|
Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
|
|
|
conn = DriverManager.getConnection(DATABASE_URL);//连接数据库zhou.db,不存在则创建
|
|
|
+ configureConnection();
|
|
|
// log.info("连接到SQLite数据库成功!");
|
|
|
create_bw_record();//初始化结构表
|
|
|
//create_measure_data();//初始化测试数据表
|
|
|
@@ -40,6 +43,8 @@ public class JdbcUtils {
|
|
|
try {
|
|
|
Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
|
|
|
conn = DriverManager.getConnection(DATABASE_URL);
|
|
|
+ configureConnection();
|
|
|
+ create_bw_record();
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (SQLException e) {
|
|
|
@@ -57,11 +62,19 @@ public class JdbcUtils {
|
|
|
}
|
|
|
// 重新建立连接
|
|
|
conn = DriverManager.getConnection(DATABASE_URL);
|
|
|
+ configureConnection();
|
|
|
+ create_bw_record();
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
// 如果重连失败,可以进一步处理异常,比如记录日志、通知管理员等
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private static void configureConnection() throws SQLException {
|
|
|
+ try (Statement statement = conn.createStatement()) {
|
|
|
+ statement.execute("PRAGMA busy_timeout = 5000");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public static void create_bw_record() throws SQLException {
|
|
|
Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
|
|
|
@@ -91,6 +104,7 @@ public class JdbcUtils {
|
|
|
")";
|
|
|
|
|
|
statement.executeUpdate(testRecord);
|
|
|
+ ensureTestRecordColumns(statement);
|
|
|
|
|
|
String configRecord = "CREATE TABLE if not exists config(\n" +
|
|
|
" id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
|
|
|
@@ -100,6 +114,29 @@ public class JdbcUtils {
|
|
|
|
|
|
statement.close();
|
|
|
}
|
|
|
+
|
|
|
+ private static void ensureTestRecordColumns(Statement statement) throws SQLException {
|
|
|
+ Set<String> existingColumns = new HashSet<>();
|
|
|
+ try (ResultSet resultSet = statement.executeQuery("PRAGMA table_info(test_record)")) {
|
|
|
+ while (resultSet.next()) {
|
|
|
+ existingColumns.add(resultSet.getString("name").toLowerCase());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String[] columns = {
|
|
|
+ "ucode VARCHAR(48)", "line_sn VARCHAR(48)", "oprno VARCHAR(48)",
|
|
|
+ "oprno_title VARCHAR(48)", "create_time DATETIME", "param1 VARCHAR(48)",
|
|
|
+ "param2 VARCHAR(48)", "param3 VARCHAR(48)", "param4 VARCHAR(48)",
|
|
|
+ "param5 VARCHAR(48)", "result VARCHAR(48)", "remark VARCHAR(48)",
|
|
|
+ "device_type VARCHAR(48)"
|
|
|
+ };
|
|
|
+ for (String column : columns) {
|
|
|
+ String columnName = column.substring(0, column.indexOf(' ')).toLowerCase();
|
|
|
+ if (!existingColumns.contains(columnName)) {
|
|
|
+ statement.executeUpdate("ALTER TABLE test_record ADD COLUMN " + column);
|
|
|
+ log.info("已为test_record补充字段: {}", column);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//插入数据
|
|
|
public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
|
|
|
@@ -148,27 +185,43 @@ public class JdbcUtils {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- public static boolean insertTestRecord(TestParam testParam){
|
|
|
- boolean ret = false;
|
|
|
+ public static synchronized boolean insertTestRecord(TestParam testParam){
|
|
|
+ String insertSQL = "INSERT INTO test_record (oprno, oprno_title, ucode, sn, line_sn, create_time, "
|
|
|
+ + "param1, param2, param3, param4, param5, result, remark, device_type) "
|
|
|
+ + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
try {
|
|
|
// 确保连接已经打开
|
|
|
if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
|
|
|
JdbcUtils.openConnection();
|
|
|
}
|
|
|
- Statement statement=conn.createStatement();
|
|
|
- String insertSQL = "INSERT INTO test_record (oprno,oprno_title,ucode, sn, line_sn, create_time, param1,param2,param3,param4,param5,result,remark,device_type)" +
|
|
|
- "VALUES('"+testParam.getOprno()+"','" + testParam.getOprnoTitle() + "','"+testParam.getUcode()+"', '" + testParam.getSn() + "', '" + testParam.getLineSn() + "', '" + testParam.getCreateTime() + "', '" + testParam.getParam1() + "', '"
|
|
|
- +testParam.getParam2()+"','"+testParam.getParam3()+"','"+testParam.getParam4()+"','"+testParam.getParam5()+"','"+testParam.getResult()+"','"+testParam.getRemark()+"','"+testParam.getDeviceType()+"')";
|
|
|
- statement.executeUpdate(insertSQL);
|
|
|
- statement.close();
|
|
|
- ret = true;
|
|
|
- log.info("向test_record表插入数据成功: {}", insertSQL);
|
|
|
+ if (conn == null) {
|
|
|
+ throw new SQLException("SQLite连接不可用");
|
|
|
+ }
|
|
|
+ try (PreparedStatement statement = conn.prepareStatement(insertSQL)) {
|
|
|
+ statement.setString(1, testParam.getOprno());
|
|
|
+ statement.setString(2, testParam.getOprnoTitle());
|
|
|
+ statement.setString(3, testParam.getUcode());
|
|
|
+ statement.setString(4, testParam.getSn());
|
|
|
+ statement.setString(5, testParam.getLineSn());
|
|
|
+ statement.setString(6, testParam.getCreateTime());
|
|
|
+ statement.setString(7, testParam.getParam1());
|
|
|
+ statement.setString(8, testParam.getParam2());
|
|
|
+ statement.setString(9, testParam.getParam3());
|
|
|
+ statement.setString(10, testParam.getParam4());
|
|
|
+ statement.setString(11, testParam.getParam5());
|
|
|
+ statement.setString(12, testParam.getResult());
|
|
|
+ statement.setString(13, testParam.getRemark());
|
|
|
+ statement.setString(14, testParam.getDeviceType());
|
|
|
+ statement.executeUpdate();
|
|
|
+ }
|
|
|
+ testParam.localRecordSaved = true;
|
|
|
+ log.info("本地测试记录保存成功: sn={}, db={}", testParam.getSn(), DATABASE_URL);
|
|
|
+ return true;
|
|
|
} catch (SQLException e) {
|
|
|
- ret = false;
|
|
|
- log.error("向test_record表插入数据失败: {}", e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
+ testParam.localRecordSaved = false;
|
|
|
+ log.error("本地测试记录保存失败: sn={}, db={}", testParam.getSn(), DATABASE_URL, e);
|
|
|
+ return false;
|
|
|
}
|
|
|
- return ret;
|
|
|
}
|
|
|
|
|
|
public static int checkSnTimer(String sn,ConfigParam configParam){
|
|
|
@@ -198,12 +251,12 @@ public class JdbcUtils {
|
|
|
int ngIndex = 0;
|
|
|
String firstTime = "";
|
|
|
while (ret.next()) {
|
|
|
- String result = ret.getString(9);
|
|
|
+ String result = ret.getString("result");
|
|
|
if(rowIndex == 0 && result.equals("OK")){
|
|
|
return 0;
|
|
|
}
|
|
|
if(rowIndex == 0){
|
|
|
- firstTime = ret.getString(4);
|
|
|
+ firstTime = ret.getString("create_time");
|
|
|
}
|
|
|
if(!result.equals("OK")){
|
|
|
ngIndex++;
|
|
|
@@ -304,13 +357,11 @@ public class JdbcUtils {
|
|
|
|
|
|
|
|
|
while (ret.next()) {
|
|
|
- MesClient.rowData[rowIndex][0] = ret.getString(2); // sn (工件码)
|
|
|
- MesClient.rowData[rowIndex][1] = ret.getString(7); // create_time (测试日期)
|
|
|
- MesClient.rowData[rowIndex][2] = ret.getString(8); // param1 (压力)
|
|
|
- MesClient.rowData[rowIndex][3] = ret.getString(10); // param3 (泄漏值)
|
|
|
- MesClient.rowData[rowIndex][4] = ret.getString(13); // result (结果)
|
|
|
-// MesClient.rowData[rowIndex][5] = ret.getString(8);
|
|
|
-// MesClient.rowData[rowIndex][6] = ret.getString(9);
|
|
|
+ MesClient.rowData[rowIndex][0] = ret.getString("sn");
|
|
|
+ MesClient.rowData[rowIndex][1] = ret.getString("create_time");
|
|
|
+ MesClient.rowData[rowIndex][2] = getDisplayPressure(ret);
|
|
|
+ MesClient.rowData[rowIndex][3] = getDisplayLeakValue(ret);
|
|
|
+ MesClient.rowData[rowIndex][4] = ret.getString("result");
|
|
|
rowIndex++;
|
|
|
}
|
|
|
|
|
|
@@ -362,11 +413,11 @@ public class JdbcUtils {
|
|
|
|
|
|
while (ret.next()) {
|
|
|
QmData qmData = new QmData();
|
|
|
- qmData.setSn(ret.getString(2));
|
|
|
- qmData.setCreateTime(ret.getString(4));
|
|
|
- qmData.setParam1(ret.getString(5));
|
|
|
- qmData.setParam2(ret.getString(6));
|
|
|
- qmData.setResult(ret.getString(9));
|
|
|
+ qmData.setSn(ret.getString("sn"));
|
|
|
+ qmData.setCreateTime(ret.getString("create_time"));
|
|
|
+ qmData.setParam1(getDisplayPressure(ret));
|
|
|
+ qmData.setParam2(getDisplayLeakValue(ret));
|
|
|
+ qmData.setResult(ret.getString("result"));
|
|
|
lists.add(qmData);
|
|
|
rowIndex++;
|
|
|
}
|
|
|
@@ -380,6 +431,37 @@ public class JdbcUtils {
|
|
|
return lists;
|
|
|
}
|
|
|
|
|
|
+ private static String getDisplayPressure(ResultSet resultSet) throws SQLException {
|
|
|
+ if (isLegacyTestRecord(resultSet)) {
|
|
|
+ return resultSet.getString("param1");
|
|
|
+ }
|
|
|
+ return joinValueAndUnit(resultSet.getString("param1"), resultSet.getString("param2"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getDisplayLeakValue(ResultSet resultSet) throws SQLException {
|
|
|
+ String leakValue = resultSet.getString("param3");
|
|
|
+ if (isLegacyTestRecord(resultSet)) {
|
|
|
+ // 历史库把泄漏值(含单位)保存在 param2。
|
|
|
+ return resultSet.getString("param2");
|
|
|
+ }
|
|
|
+ return joinValueAndUnit(leakValue, resultSet.getString("param4"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isLegacyTestRecord(ResultSet resultSet) throws SQLException {
|
|
|
+ String leakValue = resultSet.getString("param3");
|
|
|
+ return leakValue == null || leakValue.trim().isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String joinValueAndUnit(String value, String unit) {
|
|
|
+ if (value == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (unit == null || unit.trim().isEmpty()) {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ return value + unit;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
public static void close(){
|