JdbcUtils.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.mes.util;
  2. import com.mes.ui.ProdReq;
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class JdbcUtils {
  7. //通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查
  8. public static Connection conn = null;
  9. public static String Drivde="org.sqlite.JDBC";
  10. public static Connection getConn(){
  11. try {
  12. Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
  13. conn = DriverManager.getConnection("jdbc:sqlite:mes_db.db");//连接数据库zhou.db,不存在则创建
  14. System.out.println("连接到SQLite数据库成功!");
  15. create_bw_record();//初始化结构表
  16. create_bw_prod();
  17. } catch (Exception e) {
  18. // TODO Auto-generated catch block
  19. close();//关闭数据库连接
  20. e.printStackTrace();
  21. }
  22. return conn;
  23. }
  24. public static void create_bw_record() throws SQLException {
  25. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  26. //设备结构数据库
  27. String sqlEquipment = "CREATE TABLE if not exists bw_record("
  28. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),gy VARCHAR(20),message_type VARCHAR(20),sn VARCHAR(48),bw VARCHAR(1000),record_time DATETIME,"
  29. + "info_01 VARCHAR(200),info_02 VARCHAR(200),info_03 VARCHAR(200))";
  30. // statement.executeUpdate("drop table if exists bw_record");//判断是否有表tables的存在。有则删除
  31. statement.executeUpdate(sqlEquipment);
  32. System.out.println("表record创建成功!");
  33. statement.close();
  34. }
  35. //插入数据
  36. public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
  37. boolean ret = false;
  38. String record_time = DateLocalUtils.getCurrentTime();
  39. if(conn==null) {
  40. ret = false;
  41. }else {
  42. try {
  43. //创建连接对象,是Java的一个操作数据库的重要接口
  44. Statement statement=conn.createStatement();
  45. statement.executeUpdate("INSERT INTO bw_record (gw,gy,bw,record_time,message_type,sn) VALUES"
  46. + " ('"+gw+"', '"+gy+"', '"+bw+"', '"+record_time+"','"+message_type+"','"+sn+"')");//向数据库中插入数据
  47. statement.close();
  48. ret = true;
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. //e.printStackTrace();
  52. ret = false;
  53. }
  54. }
  55. return ret;
  56. }
  57. public static void create_bw_prod() throws SQLException {
  58. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  59. //设备结构数据库
  60. String sqlEquipment = "CREATE TABLE if not exists bw_prod("
  61. + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),line_sn VARCHAR(20),type VARCHAR(20),sn VARCHAR(48),"
  62. + "fout VARCHAR(20),sout VARCHAR(20),fmin VARCHAR(20),smin VARCHAR(20),fmax VARCHAR(20),smax VARCHAR(20),"
  63. + "qty VARCHAR(5),serial_number VARCHAR(50),sync int(10) NULL DEFAULT 0,record_time DATETIME,ucode VARCHAR(50))"; // 0=未同步到MES 1=已同步到MES
  64. //statement.executeUpdate("drop table if exists bw_prod");//判断是否有表tables的存在。有则删除
  65. statement.executeUpdate(sqlEquipment);
  66. System.out.println("表prod创建成功!");
  67. statement.close();
  68. }
  69. public static void close(){
  70. System.out.println("SQLite数据库连接关闭!");
  71. try {
  72. if(conn!=null) {
  73. conn.close();
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. //插入拉铆数据
  80. 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) {
  81. boolean ret = false;
  82. String record_time = DateLocalUtils.getCurrentTime();
  83. JdbcUtils.getConn();
  84. if(conn==null) {
  85. ret = false;
  86. }else {
  87. try {
  88. //创建连接对象,是Java的一个操作数据库的重要接口
  89. Statement statement=conn.createStatement();
  90. statement.executeUpdate("INSERT INTO bw_prod (gw,line_sn,type,sn,fout,sout,fmin,smin,fmax,smax,qty,serial_number,ucode,record_time) VALUES"
  91. + " ('"+gw+"', '"+lineSn+"', '"+type+"', '"+sn+"', '"+fout+"', '"+sout+"', '"+fmin+"', '"+smin+"', '"+fmax+"', '"+smax+"', '"+qty+"','"+serial_number+"','"+ucode+"','"+record_time+"')");//向数据库中插入数据
  92. statement.close();
  93. ret = true;
  94. } catch (SQLException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. ret = false;
  98. }
  99. }
  100. return ret;
  101. }
  102. public static void updateProdSync(Integer id,Integer sync) throws SQLException {
  103. Connection conn = JdbcUtils.getConn();
  104. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  105. statement.executeUpdate("update bw_prod set sync = "+sync + " where id = "+id);
  106. statement.close();
  107. }
  108. public static List<ProdReq> getProds() {
  109. //SQL语句
  110. 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";
  111. Connection conn = JdbcUtils.getConn();
  112. Statement stmt=null;
  113. ResultSet ret = null;
  114. String password=null;
  115. List<ProdReq> prods = new ArrayList<>();
  116. try {
  117. stmt = conn.createStatement();
  118. //执行语句,得到结果集
  119. ret = stmt.executeQuery(sql);
  120. System.out.println("sql:"+ sql);
  121. while (ret.next()) {
  122. ProdReq prodReq = new ProdReq();
  123. prodReq.setId(ret.getInt(1));
  124. prodReq.setGw(ret.getString(2));
  125. prodReq.setLineSn(ret.getString(3));
  126. prodReq.setType(ret.getString(4));
  127. prodReq.setSn(ret.getString(5));
  128. prodReq.setFout(ret.getString(6));
  129. prodReq.setSout(ret.getString(7));
  130. prodReq.setFmin(ret.getString(8));
  131. prodReq.setSmin(ret.getString(9));
  132. prodReq.setFmax(ret.getString(10));
  133. prodReq.setSmax(ret.getString(11));
  134. prodReq.setQty(ret.getString(12));
  135. prodReq.setSerialNumber(ret.getString(13));
  136. prodReq.setSync(ret.getInt(14));
  137. prodReq.setRecordTime(ret.getString(15));
  138. prodReq.setUcode(ret.getString(16));
  139. prods.add(prodReq);
  140. }
  141. ret.close();
  142. conn.close();//关闭连接
  143. } catch (SQLException e1) {
  144. e1.printStackTrace();
  145. }
  146. return prods;
  147. }
  148. }