JdbcUtils.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package com.mes.util;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.ui.ProdReq;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class JdbcUtils {
  8. //通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查
  9. public static Connection conn = null;
  10. public static String Drivde = "org.sqlite.JDBC";
  11. public static Connection getConn() {
  12. try {
  13. Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
  14. conn = DriverManager.getConnection("jdbc:sqlite:mes_db.db");//连接数据库zhou.db,不存在则创建
  15. System.out.println("连接到SQLite数据库成功!");
  16. create_bw_record();//初始化结构表
  17. create_bw_prod();
  18. create_config_table();
  19. } catch (Exception e) {
  20. // TODO Auto-generated catch block
  21. close();//关闭数据库连接
  22. e.printStackTrace();
  23. }
  24. return conn;
  25. }
  26. public static void create_config_table() throws SQLException {
  27. Statement statement = conn.createStatement();
  28. String sql = "CREATE TABLE if not exists bw_config("
  29. + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
  30. + "a_set_num SHORT, "
  31. + "b_set_num SHORT, "
  32. + "plc_ip_a VARCHAR(20), "
  33. + "plc_ip_b VARCHAR(20), "
  34. + "bolt_type_a VARCHAR(50), "
  35. + "bolt_type_b VARCHAR(50), "
  36. + "mes_gw VARCHAR(50), "
  37. + "mes_gw_des VARCHAR(100))";
  38. statement.executeUpdate(sql);
  39. // 检查并添加缺失的列(如果表已存在但没有新列)
  40. try {
  41. statement.executeUpdate("ALTER TABLE bw_config ADD COLUMN bolt_type_a VARCHAR(50)");
  42. } catch (SQLException e) {
  43. }
  44. try {
  45. statement.executeUpdate("ALTER TABLE bw_config ADD COLUMN bolt_type_b VARCHAR(50)");
  46. } catch (SQLException e) {
  47. }
  48. try {
  49. statement.executeUpdate("ALTER TABLE bw_config ADD COLUMN mes_gw VARCHAR(50)");
  50. } catch (SQLException e) {
  51. }
  52. try {
  53. statement.executeUpdate("ALTER TABLE bw_config ADD COLUMN mes_gw_des VARCHAR(100)");
  54. } catch (SQLException e) {
  55. }
  56. // 检查是否已有数据,若无则插入默认值
  57. ResultSet rs = statement.executeQuery("SELECT count(*) FROM bw_config");
  58. if (rs.next() && rs.getInt(1) == 0) {
  59. statement.executeUpdate("INSERT INTO bw_config (a_set_num, b_set_num, plc_ip_a, plc_ip_b, bolt_type_a, bolt_type_b, mes_gw, mes_gw_des) "
  60. + "VALUES (40, 0, '192.168.200.10', '', 'M6', '', 'OP300', '默认工位')");
  61. }
  62. rs.close();
  63. System.out.println("表config创建并初始化成功!");
  64. statement.close();
  65. }
  66. public static java.util.Map<String, Object> getConfig() {
  67. java.util.Map<String, Object> config = new java.util.HashMap<>();
  68. String sql = "SELECT a_set_num, b_set_num, plc_ip_a, plc_ip_b, bolt_type_a, bolt_type_b, mes_gw, mes_gw_des FROM bw_config LIMIT 1";
  69. Connection conn = JdbcUtils.getConn();
  70. try (Statement stmt = conn.createStatement();
  71. ResultSet rs = stmt.executeQuery(sql)) {
  72. if (rs.next()) {
  73. config.put("a_set_num", rs.getShort("a_set_num"));
  74. config.put("b_set_num", rs.getShort("b_set_num"));
  75. config.put("plc_ip_a", rs.getString("plc_ip_a"));
  76. config.put("plc_ip_b", rs.getString("plc_ip_b"));
  77. config.put("bolt_type_a", rs.getString("bolt_type_a") == null ? "M6" : rs.getString("bolt_type_a"));
  78. config.put("bolt_type_b", rs.getString("bolt_type_b") == null ? "M8" : rs.getString("bolt_type_b"));
  79. config.put("mes_gw", rs.getString("mes_gw") == null ? "OP300" : rs.getString("mes_gw"));
  80. config.put("mes_gw_des", rs.getString("mes_gw_des") == null ? "默认工位" : rs.getString("mes_gw_des"));
  81. }
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. }
  85. return config;
  86. }
  87. public static void updateConfig(short aSetNum, short bSetNum, String plcIpA, String plcIpB, String boltTypeA, String boltTypeB, String mesGw, String mesGwDes) {
  88. String sql = "UPDATE bw_config SET a_set_num = ?, b_set_num = ?, plc_ip_a = ?, plc_ip_b = ?, bolt_type_a = ?, bolt_type_b = ?, mes_gw = ?, mes_gw_des = ? WHERE id = 1";
  89. Connection conn = JdbcUtils.getConn();
  90. try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
  91. pstmt.setShort(1, aSetNum);
  92. pstmt.setShort(2, bSetNum);
  93. pstmt.setString(3, plcIpA);
  94. pstmt.setString(4, plcIpB);
  95. pstmt.setString(5, boltTypeA);
  96. pstmt.setString(6, boltTypeB);
  97. pstmt.setString(7, mesGw);
  98. pstmt.setString(8, mesGwDes);
  99. pstmt.executeUpdate();
  100. System.out.println("更新本地配置成功:aSetNum=" + aSetNum + ", bSetNum=" + bSetNum + ", IP_A=" + plcIpA + ", IP_B=" + plcIpB + ", TypeA=" + boltTypeA + ", TypeB=" + boltTypeB + ", GW=" + mesGw);
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. public static void saveStationConfig(String gw, String gwDes) {
  106. getConn();
  107. String sql = "UPDATE bw_config SET mes_gw = ?, mes_gw_des = ? WHERE id = 1";
  108. try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
  109. pstmt.setString(1, gw);
  110. pstmt.setString(2, gwDes);
  111. pstmt.executeUpdate();
  112. } catch (SQLException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. public static JSONObject getStationConfig() {
  117. getConn();
  118. String sql = "SELECT mes_gw, mes_gw_des FROM bw_config WHERE id = 1";
  119. JSONObject config = null;
  120. try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
  121. ResultSet rs = pstmt.executeQuery();
  122. if (rs.next()) {
  123. config = new JSONObject();
  124. config.put("mes_gw", rs.getString("mes_gw") == null ? "OP300" : rs.getString("mes_gw"));
  125. config.put("mes_gw_des", rs.getString("mes_gw_des") == null ? "默认工位" : rs.getString("mes_gw_des"));
  126. }
  127. } catch (SQLException e) {
  128. e.printStackTrace();
  129. }
  130. return config;
  131. }
  132. public static void saveGunConfig(String deviceId, String name, String ip, int port, int presetCount, String boltType) {
  133. getConn();
  134. String sql = "";
  135. if ("gunA".equals(deviceId)) {
  136. sql = "UPDATE bw_config SET plc_ip_a = ?, a_set_num = ?, bolt_type_a = ? WHERE id = 1";
  137. } else if ("gunB".equals(deviceId)) {
  138. sql = "UPDATE bw_config SET plc_ip_b = ?, b_set_num = ?, bolt_type_b = ? WHERE id = 1";
  139. }
  140. try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
  141. pstmt.setString(1, ip);
  142. pstmt.setInt(2, presetCount);
  143. pstmt.setString(3, boltType);
  144. pstmt.executeUpdate();
  145. } catch (SQLException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149. public static JSONObject getGunConfig(String deviceId) {
  150. getConn();
  151. String sql = "";
  152. if ("gunA".equals(deviceId)) {
  153. sql = "SELECT plc_ip_a as ip, a_set_num as preset_count, bolt_type_a as bolt_type FROM bw_config WHERE id = 1";
  154. } else if ("gunB".equals(deviceId)) {
  155. sql = "SELECT plc_ip_b as ip, b_set_num as preset_count, bolt_type_b as bolt_type FROM bw_config WHERE id = 1";
  156. }
  157. JSONObject config = null;
  158. try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
  159. ResultSet rs = pstmt.executeQuery();
  160. if (rs.next()) {
  161. config = new JSONObject();
  162. config.put("device_id", deviceId);
  163. config.put("device_name", "gunA".equals(deviceId) ? "拉铆枪A" : "拉铆枪B");
  164. config.put("ip", rs.getString("ip"));
  165. config.put("port", 2525);
  166. config.put("preset_count", rs.getInt("preset_count"));
  167. config.put("bolt_type", rs.getString("bolt_type"));
  168. }
  169. } catch (SQLException e) {
  170. e.printStackTrace();
  171. }
  172. return config;
  173. }
  174. public static void create_bw_record() throws SQLException {
  175. Statement statement = conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  176. //设备结构数据库
  177. String sqlEquipment = "CREATE TABLE if not exists bw_record("
  178. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),gy VARCHAR(20),message_type VARCHAR(20),sn VARCHAR(48),bw VARCHAR(1000),record_time DATETIME,"
  179. + "info_01 VARCHAR(200),info_02 VARCHAR(200),info_03 VARCHAR(200))";
  180. // statement.executeUpdate("drop table if exists bw_record");//判断是否有表tables的存在。有则删除
  181. statement.executeUpdate(sqlEquipment);
  182. String clientConfig = "CREATE TABLE if not exists client_config("
  183. + "config_key VARCHAR(100) PRIMARY KEY,"
  184. + "config_value VARCHAR(500),"
  185. + "update_time DATETIME"
  186. + ")";
  187. statement.executeUpdate(clientConfig);
  188. System.out.println("表record创建成功!");
  189. statement.close();
  190. }
  191. public static void saveClientConfig(String key, String value) {
  192. try {
  193. if (conn == null || conn.isClosed()) {
  194. getConn();
  195. }
  196. String sql = "INSERT INTO client_config (config_key, config_value, update_time) VALUES (?, ?, ?) "
  197. + "ON CONFLICT(config_key) DO UPDATE SET config_value = excluded.config_value, update_time = excluded.update_time";
  198. PreparedStatement ps = conn.prepareStatement(sql);
  199. ps.setString(1, key);
  200. ps.setString(2, value);
  201. ps.setString(3, DateLocalUtils.getCurrentTime());
  202. ps.executeUpdate();
  203. ps.close();
  204. } catch (SQLException e) {
  205. e.printStackTrace();
  206. }
  207. }
  208. public static String getClientConfig(String key, String defaultValue) {
  209. try {
  210. if (conn == null || conn.isClosed()) {
  211. getConn();
  212. }
  213. String sql = "SELECT config_value FROM client_config WHERE config_key = ?";
  214. PreparedStatement ps = conn.prepareStatement(sql);
  215. ps.setString(1, key);
  216. ResultSet rs = ps.executeQuery();
  217. if (rs.next()) {
  218. String value = rs.getString("config_value");
  219. rs.close();
  220. ps.close();
  221. return value;
  222. }
  223. rs.close();
  224. ps.close();
  225. } catch (SQLException e) {
  226. e.printStackTrace();
  227. }
  228. return defaultValue;
  229. }
  230. public static String getProductTypeConfigKey(String gw) {
  231. return "product_type_" + gw;
  232. }
  233. //插入数据
  234. public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
  235. boolean ret = false;
  236. String record_time = DateLocalUtils.getCurrentTime();
  237. if (conn == null) {
  238. ret = false;
  239. } else {
  240. try {
  241. //创建连接对象,是Java的一个操作数据库的重要接口
  242. Statement statement = conn.createStatement();
  243. statement.executeUpdate("INSERT INTO bw_record (gw,gy,bw,record_time,message_type,sn) VALUES"
  244. + " ('" + gw + "', '" + gy + "', '" + bw + "', '" + record_time + "','" + message_type + "','" + sn + "')");//向数据库中插入数据
  245. statement.close();
  246. ret = true;
  247. } catch (SQLException e) {
  248. // TODO Auto-generated catch block
  249. //e.printStackTrace();
  250. ret = false;
  251. }
  252. }
  253. return ret;
  254. }
  255. public static void create_bw_prod() throws SQLException {
  256. Statement statement = conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  257. //设备结构数据库
  258. String sqlEquipment = "CREATE TABLE if not exists bw_prod("
  259. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),line_sn VARCHAR(20),type VARCHAR(20),sn VARCHAR(48),"
  260. + "fout VARCHAR(20),sout VARCHAR(20),fmin VARCHAR(20),smin VARCHAR(20),fmax VARCHAR(20),smax VARCHAR(20),"
  261. + "qty VARCHAR(5),serial_number VARCHAR(50),sync int(10) NULL DEFAULT 0,record_time DATETIME,ucode VARCHAR(50))"; // 0=未同步到MES 1=已同步到MES
  262. //statement.executeUpdate("drop table if exists bw_prod");//判断是否有表tables的存在。有则删除
  263. statement.executeUpdate(sqlEquipment);
  264. System.out.println("表prod创建成功!");
  265. statement.close();
  266. }
  267. public static void close() {
  268. System.out.println("SQLite数据库连接关闭!");
  269. try {
  270. if (conn != null) {
  271. conn.close();
  272. }
  273. } catch (SQLException e) {
  274. e.printStackTrace();
  275. }
  276. }
  277. //插入拉铆数据
  278. public static boolean insertProdData(String gw, String lineSn, String sn, String type, String fout, String sout, String fmin, String smin, String fmax, String smax, String qty, String serial_number, String ucode) {
  279. boolean ret = false;
  280. String record_time = DateLocalUtils.getCurrentTime();
  281. JdbcUtils.getConn();
  282. if (conn == null) {
  283. ret = false;
  284. } else {
  285. try {
  286. //创建连接对象,是Java的一个操作数据库的重要接口
  287. Statement statement = conn.createStatement();
  288. statement.executeUpdate("INSERT INTO bw_prod (gw,line_sn,type,sn,fout,sout,fmin,smin,fmax,smax,qty,serial_number,ucode,record_time) VALUES"
  289. + " ('" + gw + "', '" + lineSn + "', '" + type + "', '" + sn + "', '" + fout + "', '" + sout + "', '" + fmin + "', '" + smin + "', '" + fmax + "', '" + smax + "', '" + qty + "','" + serial_number + "','" + ucode + "','" + record_time + "')");//向数据库中插入数据
  290. statement.close();
  291. if (!qty.equals("1")) {
  292. com.mes.ui.DataUtil.upAlarm(sn, ucode);
  293. }
  294. ret = true;
  295. } catch (SQLException e) {
  296. // TODO Auto-generated catch block
  297. e.printStackTrace();
  298. ret = false;
  299. }
  300. }
  301. return ret;
  302. }
  303. public static void updateProdSync(Integer id, Integer sync) throws SQLException {
  304. Connection conn = JdbcUtils.getConn();
  305. Statement statement = conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  306. statement.executeUpdate("update bw_prod set sync = " + sync + " where id = " + id);
  307. statement.close();
  308. }
  309. public static List<ProdReq> getProds() {
  310. //SQL语句
  311. String sql = "select id,gw,line_sn,type,sn,fout,sout,fmin,smin,fmax,smax,qty,serial_number,sync,record_time,ucode from bw_prod where sync = 0 order by id asc limit 100";
  312. Connection conn = JdbcUtils.getConn();
  313. Statement stmt = null;
  314. ResultSet ret = null;
  315. String password = null;
  316. List<ProdReq> prods = new ArrayList<>();
  317. try {
  318. stmt = conn.createStatement();
  319. //执行语句,得到结果集
  320. ret = stmt.executeQuery(sql);
  321. System.out.println("sql:" + sql);
  322. while (ret.next()) {
  323. ProdReq prodReq = new ProdReq();
  324. prodReq.setId(ret.getInt(1));
  325. prodReq.setGw(ret.getString(2));
  326. prodReq.setLineSn(ret.getString(3));
  327. prodReq.setType(ret.getString(4));
  328. prodReq.setSn(ret.getString(5));
  329. prodReq.setFout(ret.getString(6));
  330. prodReq.setSout(ret.getString(7));
  331. prodReq.setFmin(ret.getString(8));
  332. prodReq.setSmin(ret.getString(9));
  333. prodReq.setFmax(ret.getString(10));
  334. prodReq.setSmax(ret.getString(11));
  335. prodReq.setQty(ret.getString(12));
  336. prodReq.setSerialNumber(ret.getString(13));
  337. prodReq.setSync(ret.getInt(14));
  338. prodReq.setRecordTime(ret.getString(15));
  339. prodReq.setUcode(ret.getString(16));
  340. prods.add(prodReq);
  341. }
  342. ret.close();
  343. stmt.close();
  344. } catch (SQLException e1) {
  345. e1.printStackTrace();
  346. }
  347. return prods;
  348. }
  349. }