JdbcUtils.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package com.mes.util;
  2. import com.mes.ygsl.MesProductBs;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class JdbcUtils {
  9. public static final Logger log = LoggerFactory.getLogger(JdbcUtils.class);
  10. //通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查
  11. public static Connection conn = null;
  12. public static String Drivde="org.sqlite.JDBC";
  13. public static String DATABASE_URL="jdbc:sqlite:mes_db.db";
  14. public static Connection getConn(){
  15. try {
  16. Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
  17. conn = DriverManager.getConnection("jdbc:sqlite:mes_db.db");//连接数据库zhou.db,不存在则创建
  18. log.info("连接到SQLite数据库成功!");
  19. create_bw_record();//初始化结构表
  20. create_bw_prod_bs();
  21. //create_measure_data();//初始化测试数据表
  22. } catch (Exception e) {
  23. // TODO Auto-generated catch block
  24. close();//关闭数据库连接
  25. e.printStackTrace();
  26. }
  27. return conn;
  28. }
  29. public static void create_bw_record() throws SQLException {
  30. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  31. //设备结构数据库
  32. String sqlEquipment = "CREATE TABLE if not exists bw_record("
  33. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),gy VARCHAR(20),message_type VARCHAR(20),sn VARCHAR(48),bw VARCHAR(1000),record_time DATETIME,"
  34. + "info_01 VARCHAR(200),info_02 VARCHAR(200),info_03 VARCHAR(200))";
  35. // statement.executeUpdate("drop table if exists bw_record");//判断是否有表tables的存在。有则删除
  36. statement.executeUpdate(sqlEquipment);
  37. // 创建 提交记录表
  38. String submitRecord = "CREATE TABLE if not exists submit_record(\n" +
  39. " id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
  40. " oprno VARCHAR(20), -- 工位号 \n" +
  41. " sn VARCHAR(48), -- 二维码\n" +
  42. " bw VARCHAR(1000), -- 报文 \n" +
  43. " record_time DATETIME, -- 记录时间\n" +
  44. " state CHAR(1) -- 状态(0 ->未提交, 1 ->已提交)\n" +
  45. ")";
  46. statement.executeUpdate(submitRecord);
  47. log.info("表创建成功!");
  48. // statement.executeUpdate("INSERT INTO tree_structure VALUES (1, '第一电厂', '第一电厂', '', 1, 0,'','','','','','')");//向数据库中插入数据
  49. // statement.executeUpdate("INSERT INTO tree_structure VALUES (2, '第一电厂-设备管理部', '第一电厂-设备管理部', '第一电厂', 2, 0,'','','','','','')");//向数据库中插入数据
  50. // statement.executeUpdate("INSERT INTO tree_structure VALUES (3, '第一电厂-设备管理部-电机1', '第一电厂-设备管理部-电机1', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  51. // statement.executeUpdate("INSERT INTO tree_structure VALUES (4, '第一电厂-设备管理部-电机2', '第一电厂-设备管理部-电机2', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  52. // statement.executeUpdate("INSERT INTO tree_structure VALUES (5, '第一电厂-设备管理部-电机3', '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  53. // statement.executeUpdate("INSERT INTO tree_structure VALUES (6, '第一电厂-设备管理部-电机4', '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  54. // statement.executeUpdate("INSERT INTO tree_structure VALUES (7, '第一电厂-设备管理部-电机5', '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部',3, 1,'','','','','','')");//向数据库中插入数据
  55. // statement.executeUpdate("INSERT INTO tree_structure VALUES (9, '第一电厂-设备管理部-电机6', '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部',3, 1,'','','','','','')");//向数据库中插入数据
  56. // statement.executeUpdate("INSERT INTO tree_structure VALUES (10, '第二电厂', '第二电厂', '', 1, 0,'','','','','','')");//向数据库中插入数据
  57. // statement.executeUpdate("INSERT INTO tree_structure VALUES (11, '第二电厂-设备管理部', '第二电厂-设备管理部', '第二电厂', 2, 0,'','','','','','')");//向数据库中插入数据
  58. // statement.executeUpdate("INSERT INTO tree_structure VALUES (12, '第二电厂-设备管理部-电机1', '第二电厂-设备管理部-电机1', '第二电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  59. // statement.executeUpdate("INSERT INTO tree_structure VALUES (13, '第二电厂-设备管理部-电机2', '第二电厂-设备管理部-电机2', '第二电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  60. // log.info(sqlEquipment);
  61. // ResultSet rSet=statement.executeQuery("select * from bw_record");//搜索数据库,将搜索的放入数据集ResultSet中
  62. // while (rSet.next()) { //遍历这个数据集
  63. // log.info("gw:"+rSet.getString(1));//依次输出 也可以这样写 rSet.getString("name")
  64. // //log.info("密码:"+rSet.getString("pwd"));
  65. // }
  66. // rSet.close();//关闭数据集
  67. statement.close();
  68. }
  69. //插入数据
  70. public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
  71. boolean ret = false;
  72. String record_time = DateLocalUtils.getCurrentTime();
  73. if(conn==null) {
  74. ret = false;
  75. }else {
  76. try {
  77. //创建连接对象,是Java的一个操作数据库的重要接口
  78. Statement statement=conn.createStatement();
  79. statement.executeUpdate("INSERT INTO bw_record (gw,gy,bw,record_time,message_type,sn) VALUES"
  80. + " ('"+gw+"', '"+gy+"', '"+bw+"', '"+record_time+"','"+message_type+"','"+sn+"')");//向数据库中插入数据
  81. statement.close();
  82. ret = true;
  83. } catch (SQLException e) {
  84. // TODO Auto-generated catch block
  85. //e.printStackTrace();
  86. ret = false;
  87. }
  88. }
  89. return ret;
  90. }
  91. // 向 submit_record表 插入提交记录数据
  92. public static boolean insertSubmitRecord(String oprno, String sn, String bw){
  93. boolean ret = false;
  94. String record_time = DateLocalUtils.getCurrentTime();
  95. if(conn==null) {
  96. ret = false;
  97. }else {
  98. try {
  99. Statement statement=conn.createStatement();
  100. String insertSQL = "INSERT INTO submit_record (oprno, sn, bw, record_time, state)" +
  101. "VALUES('" + oprno + "', '" + sn + "', '" + bw + "', '" + record_time + "', '0')";
  102. statement.executeUpdate(insertSQL);
  103. statement.close();
  104. ret = true;
  105. log.info("向submit_record表插入数据成功: " + insertSQL);
  106. } catch (SQLException e) {
  107. ret = false;
  108. log.info("向submit_record表插入数据失败");
  109. }
  110. }
  111. return ret;
  112. }
  113. public static void create_measure_data() throws SQLException {
  114. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  115. //statement.executeUpdate("drop table if exists measure_data");//判断是否有表tables的存在。有则删除
  116. //设备结构数据库
  117. String sqlEquipment = "CREATE TABLE if not exists measure_data("
  118. + "id INTEGER PRIMARY KEY AUTOINCREMENT,function_location VARCHAR(200),description VARCHAR(200),"
  119. + "data_item VARCHAR(100),measure_time DATETIME,data_type INT(3)," //data_type=1电阻校准数据,2电感校准数据,3绝缘电阻校准数据
  120. + "data_01 double(30),data_02 double(30),data_03 double(30),data_04 double(30),data_05 double(30),"
  121. + "data_06 double(30),data_07 double(30),data_08 double(30),data_09 double(30),data_10 double(30),"
  122. + "data_11 double(30),data_12 double(30),data_13 double(30),data_14 double(30),data_15 double(30),"
  123. + "data_16 double(30),data_17 double(30),data_18 double(30),data_19 double(30),data_20 double(30)"
  124. + ")";
  125. statement.executeUpdate(sqlEquipment);
  126. // statement.executeUpdate("INSERT INTO measure_data VALUES (1, '第一电厂-设备管理部-电机1', '第一电厂-设备管理部-电机1', '2022-03-22 15:16:18', '2022-03-22 15:16:18',1,"
  127. // + "0.05, 0.82, 3, 80, 130, 180, 402, 502, 809, 1103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)");//向数据库中插入数据
  128. // ResultSet rSet=statement.executeQuery("select * from measure_data");//搜索数据库,将搜索的放入数据集ResultSet中
  129. // while (rSet.next()) { //遍历这个数据集
  130. // log.info("function_location:"+rSet.getString(2));//依次输出 也可以这样写 rSet.getString(“name”)
  131. // log.info("data_01:"+rSet.getString(4));
  132. // }
  133. // rSet.close();//关闭数据集
  134. statement.close();
  135. }
  136. public static void close(){
  137. log.info("SQLite数据库连接关闭!");
  138. try {
  139. if(conn!=null) {
  140. conn.close();
  141. }
  142. } catch (SQLException e) {
  143. e.printStackTrace();
  144. }
  145. }
  146. //设备结构数据库
  147. // String sqlEquipment = "CREATE TABLE if not exists tree_structure(id INTEGER PRIMARY KEY AUTOINCREMENT,function_location VARCHAR(200),description VARCHAR(200),sup_fl VARCHAR(200),level INT(3)," +
  148. // "isleaf INT(3))";
  149. // statement.executeUpdate("drop table if exists tree_structure");//判断是否有表tables的存在。有则删除
  150. // statement.executeUpdate(sqlEquipment);
  151. // statement.executeUpdate("INSERT INTO tree_structure VALUES (1, '第一电厂', '第一电厂', NULL, 1, 0)");//向数据库中插入数据
  152. // statement.executeUpdate("INSERT INTO tree_structure VALUES (2, '第一电厂-设备管理部', '第一电厂-设备管理部', '第一电厂', 2, 0)");//向数据库中插入数据
  153. // statement.executeUpdate("INSERT INTO tree_structure VALUES (3, '第一电厂-设备管理部-发电机组', '第一电厂-设备管理部-发电机组', '第一电厂-设备管理部', 3, 0)");//向数据库中插入数据
  154. // statement.executeUpdate("INSERT INTO tree_structure VALUES (4, '第一电厂-设备管理部-发电机组-电机1', '第一电厂-设备管理部-发电机组-电机1', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  155. // statement.executeUpdate("INSERT INTO tree_structure VALUES (5, '第一电厂-设备管理部-发电机组-电机2', '第一电厂-设备管理部-发电机组-电机2', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  156. // statement.executeUpdate("INSERT INTO tree_structure VALUES (6, '第二电厂', '第二电厂', NULL, 1, 0)");//向数据库中插入数据
  157. // statement.executeUpdate("INSERT INTO tree_structure VALUES (7, '第二电厂-设备管理部', '第二电厂-设备管理部', '第二电厂', 2, 0)");//向数据库中插入数据
  158. // statement.executeUpdate("INSERT INTO tree_structure VALUES (8, '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  159. // statement.executeUpdate("INSERT INTO tree_structure VALUES (10, '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  160. // statement.executeUpdate("INSERT INTO tree_structure VALUES (11, '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  161. // statement.executeUpdate("INSERT INTO tree_structure VALUES (12, '第一电厂-设备管理部-发电机组-电机8', '第一电厂-设备管理部-发电机组-电机8', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  162. // statement.executeUpdate("INSERT INTO tree_structure VALUES (13, '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  163. // statement.executeUpdate("INSERT INTO tree_structure VALUES (14, '第一电厂-设备管理部-发电机组-电机7', '第一电厂-设备管理部-发电机组-电机7', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  164. // statement.executeUpdate("INSERT INTO tree_structure VALUES (15, '第一电厂-设备管理部-发电机组-电机9', '第一电厂-设备管理部-发电机组-电机9', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  165. // statement.executeUpdate("INSERT INTO tree_structure VALUES (16, '第一电厂-设备管理部-发电机组-电机10', '第一电厂-设备管理部-发电机组-电机10', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  166. // ResultSet rSet=statement.executeQuery("select * from tree_structure");//搜索数据库,将搜索的放入数据集ResultSet中
  167. // /**
  168. // * 通过用户名到数据库中获取凭证密码
  169. // * @param userName
  170. // * @return
  171. // */
  172. // private static String getPasswordByUserName(String userName) {
  173. // //SQL语句
  174. // String sql = "select password from users where username = " +"'" + userName+"'";
  175. // Connection conn = JdbcUtils.getConn();
  176. // Statement stmt=null;
  177. // ResultSet ret = null;
  178. // String password=null;
  179. // try {
  180. // stmt = conn.createStatement();
  181. // //执行语句,得到结果集
  182. // ret = stmt.executeQuery(sql);
  183. // while (ret.next()) {
  184. // //这里只查询的密码
  185. // password = ret.getString(1);
  186. // }
  187. // ret.close();
  188. // conn.close();//关闭连接
  189. // } catch (SQLException e1) {
  190. // e1.printStackTrace();
  191. // }
  192. // return password;
  193. // }
  194. //创建扭力扳手数据表
  195. public static void create_bw_prod_bs() throws SQLException {
  196. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  197. //设备结构数据库
  198. String sqlEquipment = "CREATE TABLE if not exists bw_prod_bs("
  199. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),line_sn VARCHAR(20),sn VARCHAR(48),"
  200. + "torque VARCHAR(20),result VARCHAR(20),sync int(10) NULL DEFAULT 0,"
  201. + "ucode VARCHAR(50),record_time DATETIME,tighteningID VARCHAR(30),jobID VARCHAR(30))";
  202. //statement.executeUpdate("drop table if exists bw_prod");//判断是否有表tables的存在。有则删除
  203. statement.executeUpdate(sqlEquipment);
  204. System.out.println("表bw_prod_bs创建成功!");
  205. statement.close();
  206. }
  207. //插入扭力扳手数据
  208. public static boolean insertProdData(String gw, String lineSn, String sn, String torque, String result, String ucode,String tighteningID,String jobID) {
  209. boolean ret = false;
  210. String record_time = DateLocalUtils.getCurrentTime();
  211. JdbcUtils.getConn();
  212. if(conn==null) {
  213. ret = false;
  214. }else {
  215. try {
  216. //创建连接对象,是Java的一个操作数据库的重要接口
  217. Statement statement=conn.createStatement();
  218. statement.executeUpdate("INSERT INTO bw_prod_bs (gw,line_sn,sn,torque,result,ucode,record_time,tighteningID,jobID) VALUES"
  219. + " ('"+gw+"', '"+lineSn+"', '"+sn+"', '"+torque+"', '"+result+"', '"+ucode+"', '"+record_time+"', '"+tighteningID+"','"+jobID+"')");//向数据库中插入数据
  220. statement.close();
  221. ret = true;
  222. } catch (SQLException e) {
  223. // TODO Auto-generated catch block
  224. e.printStackTrace();
  225. ret = false;
  226. }
  227. }
  228. return ret;
  229. }
  230. public static void openConnection() {
  231. try {
  232. Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
  233. conn = DriverManager.getConnection(DATABASE_URL);
  234. } catch (ClassNotFoundException e) {
  235. e.printStackTrace();
  236. } catch (SQLException e) {
  237. e.printStackTrace();
  238. // 如果连接失败,尝试重连
  239. reconnect();
  240. }
  241. }
  242. private static void reconnect() {
  243. try {
  244. // 关闭旧连接
  245. if (conn != null && !conn.isClosed()) {
  246. conn.close();
  247. }
  248. // 重新建立连接
  249. conn = DriverManager.getConnection(DATABASE_URL);
  250. } catch (SQLException e) {
  251. e.printStackTrace();
  252. // 如果重连失败,可以进一步处理异常,比如记录日志、通知管理员等
  253. }
  254. }
  255. //查询数据是否存在
  256. public static boolean checkTighteningById(String tighteningId,String jobID,String sn) {
  257. boolean ret = false;
  258. try{
  259. // 确保连接已经打开
  260. if (conn == null || conn.isClosed()) {
  261. openConnection();
  262. }
  263. Statement statement=conn.createStatement();
  264. ResultSet retset = statement.executeQuery("SELECT count(*) FROM bw_prod_bs WHERE tighteningID = '"+tighteningId+"' AND jobID = '"+jobID+"' AND sn = '"+sn+"' LIMIT 1");//向数据库中插入数据
  265. Integer count = 0;
  266. while (retset.next()) {
  267. count = retset.getInt(1);
  268. }
  269. statement.close();
  270. if(count > 0){
  271. ret = true;
  272. }
  273. }catch (Exception e){
  274. e.printStackTrace();
  275. ret = false;
  276. }
  277. return ret;
  278. }
  279. public static void updateProdSync(Integer id,Integer sync) throws SQLException {
  280. Connection conn = JdbcUtils.getConn();
  281. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  282. statement.executeUpdate("update bw_prod_bs set sync = "+sync + " where id = "+id);
  283. statement.close();
  284. }
  285. public static List<MesProductBs> getProds() {
  286. //SQL语句
  287. String sql = "select id,gw,line_sn,sn,torque,result,ucode,record_time from bw_prod_bs where sync = 0 order by id asc limit 100";
  288. Connection conn = JdbcUtils.getConn();
  289. Statement stmt=null;
  290. ResultSet ret = null;
  291. String password=null;
  292. List<MesProductBs> prods = new ArrayList<>();
  293. try {
  294. stmt = conn.createStatement();
  295. //执行语句,得到结果集
  296. ret = stmt.executeQuery(sql);
  297. System.out.println("sql:"+ sql);
  298. while (ret.next()) {
  299. MesProductBs prodReq = new MesProductBs();
  300. prodReq.setId(ret.getInt(1));
  301. prodReq.setOprno(ret.getString(2));
  302. prodReq.setLineSn(ret.getString(3));
  303. prodReq.setSn(ret.getString(4));
  304. prodReq.setTorque(ret.getString(5));
  305. prodReq.setResult(ret.getString(6));
  306. prodReq.setCreateBy(ret.getString(7));
  307. prodReq.setCreateDate(ret.getString(8));
  308. prods.add(prodReq);
  309. }
  310. ret.close();
  311. conn.close();//关闭连接
  312. } catch (SQLException e1) {
  313. e1.printStackTrace();
  314. }
  315. return prods;
  316. }
  317. }