JdbcUtils.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.mes.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. public class JdbcUtils {
  14. public static final Logger log = LoggerFactory.getLogger(JdbcUtils.class);
  15. //通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查
  16. public static Connection conn = null;
  17. public static String Drivde="org.sqlite.JDBC";
  18. public static String DATABASE_URL="jdbc:sqlite:mes_db.db";
  19. public static Connection getConn(){
  20. try {
  21. Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
  22. conn = DriverManager.getConnection("jdbc:sqlite:mes_db.db");//连接数据库zhou.db,不存在则创建
  23. System.out.println("连接到SQLite数据库成功!");
  24. create_bw_record();//初始化结构表
  25. //create_measure_data();//初始化测试数据表
  26. } catch (Exception e) {
  27. // TODO Auto-generated catch block
  28. close();//关闭数据库连接
  29. e.printStackTrace();
  30. }
  31. return conn;
  32. }
  33. public static void openConnection() {
  34. try {
  35. Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
  36. conn = DriverManager.getConnection(DATABASE_URL);
  37. } catch (ClassNotFoundException e) {
  38. e.printStackTrace();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. // 如果连接失败,尝试重连
  42. reconnect();
  43. }
  44. }
  45. private static void reconnect() {
  46. try {
  47. // 关闭旧连接
  48. if (conn != null && !conn.isClosed()) {
  49. conn.close();
  50. }
  51. // 重新建立连接
  52. conn = DriverManager.getConnection(DATABASE_URL);
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. // 如果重连失败,可以进一步处理异常,比如记录日志、通知管理员等
  56. }
  57. }
  58. public static void create_bw_record() throws SQLException {
  59. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  60. //设备结构数据库
  61. String sqlEquipment = "CREATE TABLE if not exists bw_record("
  62. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),gy VARCHAR(20),message_type VARCHAR(20),sn VARCHAR(48),bw VARCHAR(1000),record_time DATETIME,"
  63. + "info_01 VARCHAR(200),info_02 VARCHAR(200),info_03 VARCHAR(200))";
  64. // statement.executeUpdate("drop table if exists bw_record");//判断是否有表tables的存在。有则删除
  65. statement.executeUpdate(sqlEquipment);
  66. // 创建 提交记录表
  67. String submitRecord = "CREATE TABLE if not exists submit_record(\n" +
  68. " id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
  69. " oprno VARCHAR(20), -- 工位号 \n" +
  70. " sn VARCHAR(48), -- 二维码\n" +
  71. " bw VARCHAR(1000), -- 报文 \n" +
  72. " record_time DATETIME, -- 记录时间\n" +
  73. " state CHAR(1) -- 状态(0 ->未提交, 1 ->已提交)\n" +
  74. ")";
  75. statement.executeUpdate(submitRecord);
  76. // 创建设备参数表
  77. String deviceParamTable = "CREATE TABLE if not exists bw_device_param(\n" +
  78. " id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
  79. " oprno VARCHAR(20), -- 工位号 \n" +
  80. " line_sn VARCHAR(20), -- 产线编号\n" +
  81. " sn VARCHAR(48), -- 产品序列号\n" +
  82. " content VARCHAR(5000), -- 存储内容(60条记录的JSON)\n" +
  83. " create_time DATETIME, -- 创建时间\n" +
  84. " sync INTEGER DEFAULT 0 -- 同步状态(0=未同步,1=已同步)\n" +
  85. ")";
  86. statement.executeUpdate(deviceParamTable);
  87. statement.close();
  88. }
  89. //插入数据
  90. public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
  91. boolean ret = false;
  92. String record_time = DateLocalUtils.getCurrentTime();
  93. try {
  94. // 确保连接已经打开
  95. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  96. JdbcUtils.openConnection();
  97. }
  98. //创建连接对象,是Java的一个操作数据库的重要接口
  99. Statement statement=conn.createStatement();
  100. statement.executeUpdate("INSERT INTO bw_record (gw,gy,bw,record_time,message_type,sn) VALUES"
  101. + " ('"+gw+"', '"+gy+"', '"+bw+"', '"+record_time+"','"+message_type+"','"+sn+"')");//向数据库中插入数据
  102. statement.close();
  103. ret = true;
  104. } catch (SQLException e) {
  105. // TODO Auto-generated catch block
  106. //e.printStackTrace();
  107. ret = false;
  108. }
  109. return ret;
  110. }
  111. // 向 submit_record表 插入提交记录数据
  112. public static boolean insertSubmitRecord(String oprno, String sn, String bw){
  113. boolean ret = false;
  114. String record_time = DateLocalUtils.getCurrentTime();
  115. try {
  116. // 确保连接已经打开
  117. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  118. JdbcUtils.openConnection();
  119. }
  120. Statement statement=conn.createStatement();
  121. String insertSQL = "INSERT INTO submit_record (oprno, sn, bw, record_time, state)" +
  122. "VALUES('" + oprno + "', '" + sn + "', '" + bw + "', '" + record_time + "', '0')";
  123. statement.executeUpdate(insertSQL);
  124. statement.close();
  125. ret = true;
  126. log.info("向submit_record表插入数据成功: {}", insertSQL);
  127. } catch (SQLException e) {
  128. ret = false;
  129. log.info("向submit_record表插入数据失败");
  130. }
  131. return ret;
  132. }
  133. // 向 bw_device_param表 插入设备参数数据
  134. public static boolean insertDeviceParamData(String oprno, String line_sn, String sn, String content){
  135. boolean ret = false;
  136. String record_time = DateLocalUtils.getCurrentTime();
  137. try {
  138. // 确保连接已经打开
  139. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  140. JdbcUtils.openConnection();
  141. }
  142. Statement statement=conn.createStatement();
  143. String insertSQL = "INSERT INTO bw_device_param (oprno, line_sn, sn, content, create_time, sync)" +
  144. "VALUES('" + oprno + "', '" + line_sn + "', '" + sn + "', '" + content + "', '" + record_time + "', 0)";
  145. statement.executeUpdate(insertSQL);
  146. statement.close();
  147. ret = true;
  148. log.info("向bw_device_param表插入数据成功");
  149. } catch (SQLException e) {
  150. ret = false;
  151. log.info("向bw_device_param表插入数据失败: " + e.getMessage());
  152. }
  153. return ret;
  154. }
  155. // 查询未同步的设备参数数据
  156. public static List<Map<String, Object>> getDeviceParams() {
  157. String sql = "select id,oprno,line_sn,sn,content,sync from bw_device_param where sync = 0 order by id asc limit 100";
  158. List<Map<String, Object>> prods = new ArrayList<>();
  159. try {
  160. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  161. JdbcUtils.openConnection();
  162. }
  163. Statement statement = conn.createStatement();
  164. ResultSet ret = statement.executeQuery(sql);
  165. while (ret.next()) {
  166. Map<String, Object> map = new HashMap<>();
  167. map.put("id", ret.getInt(1));
  168. map.put("oprno", ret.getString(2));
  169. map.put("lineSn", ret.getString(3));
  170. map.put("sn", ret.getString(4));
  171. map.put("content", ret.getString(5));
  172. map.put("sync", ret.getInt(6));
  173. prods.add(map);
  174. }
  175. ret.close();
  176. statement.close();
  177. } catch (SQLException e) {
  178. log.info("查询未同步设备参数数据失败: " + e.getMessage());
  179. }
  180. return prods;
  181. }
  182. // 更新设备参数同步状态
  183. public static void updateDeviceParamSync(Integer id, Integer sync) throws SQLException {
  184. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  185. JdbcUtils.openConnection();
  186. }
  187. Statement statement = conn.createStatement();
  188. statement.executeUpdate("update bw_device_param set sync = " + sync + " where id = " + id);
  189. statement.close();
  190. }
  191. public static void close(){
  192. try {
  193. if(conn!=null) {
  194. conn.close();
  195. }
  196. } catch (SQLException e) {
  197. e.printStackTrace();
  198. }
  199. }
  200. }