JdbcUtils.java 7.5 KB

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