JdbcUtils.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.mes.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  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_measure_data();//初始化测试数据表
  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("表创建成功!");
  33. // statement.executeUpdate("INSERT INTO tree_structure VALUES (1, '第一电厂', '第一电厂', '', 1, 0,'','','','','','')");//向数据库中插入数据
  34. // statement.executeUpdate("INSERT INTO tree_structure VALUES (2, '第一电厂-设备管理部', '第一电厂-设备管理部', '第一电厂', 2, 0,'','','','','','')");//向数据库中插入数据
  35. // statement.executeUpdate("INSERT INTO tree_structure VALUES (3, '第一电厂-设备管理部-电机1', '第一电厂-设备管理部-电机1', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  36. // statement.executeUpdate("INSERT INTO tree_structure VALUES (4, '第一电厂-设备管理部-电机2', '第一电厂-设备管理部-电机2', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  37. // statement.executeUpdate("INSERT INTO tree_structure VALUES (5, '第一电厂-设备管理部-电机3', '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  38. // statement.executeUpdate("INSERT INTO tree_structure VALUES (6, '第一电厂-设备管理部-电机4', '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  39. // statement.executeUpdate("INSERT INTO tree_structure VALUES (7, '第一电厂-设备管理部-电机5', '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部',3, 1,'','','','','','')");//向数据库中插入数据
  40. // statement.executeUpdate("INSERT INTO tree_structure VALUES (9, '第一电厂-设备管理部-电机6', '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部',3, 1,'','','','','','')");//向数据库中插入数据
  41. // statement.executeUpdate("INSERT INTO tree_structure VALUES (10, '第二电厂', '第二电厂', '', 1, 0,'','','','','','')");//向数据库中插入数据
  42. // statement.executeUpdate("INSERT INTO tree_structure VALUES (11, '第二电厂-设备管理部', '第二电厂-设备管理部', '第二电厂', 2, 0,'','','','','','')");//向数据库中插入数据
  43. // statement.executeUpdate("INSERT INTO tree_structure VALUES (12, '第二电厂-设备管理部-电机1', '第二电厂-设备管理部-电机1', '第二电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  44. // statement.executeUpdate("INSERT INTO tree_structure VALUES (13, '第二电厂-设备管理部-电机2', '第二电厂-设备管理部-电机2', '第二电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
  45. // System.out.println(sqlEquipment);
  46. // ResultSet rSet=statement.executeQuery("select * from bw_record");//搜索数据库,将搜索的放入数据集ResultSet中
  47. // while (rSet.next()) { //遍历这个数据集
  48. // System.out.println("gw:"+rSet.getString(1));//依次输出 也可以这样写 rSet.getString("name")
  49. // //System.out.println("密码:"+rSet.getString("pwd"));
  50. // }
  51. // rSet.close();//关闭数据集
  52. statement.close();
  53. }
  54. //插入数据
  55. public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
  56. boolean ret = false;
  57. String record_time = DateLocalUtils.getCurrentTime();
  58. if(conn==null) {
  59. ret = false;
  60. }else {
  61. try {
  62. //创建连接对象,是Java的一个操作数据库的重要接口
  63. Statement statement=conn.createStatement();
  64. statement.executeUpdate("INSERT INTO bw_record (gw,gy,bw,record_time,message_type,sn) VALUES"
  65. + " ('"+gw+"', '"+gy+"', '"+bw+"', '"+record_time+"','"+message_type+"','"+sn+"')");//向数据库中插入数据
  66. statement.close();
  67. ret = true;
  68. } catch (SQLException e) {
  69. // TODO Auto-generated catch block
  70. //e.printStackTrace();
  71. ret = false;
  72. }
  73. }
  74. return ret;
  75. }
  76. public static void create_measure_data() throws SQLException {
  77. Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
  78. //statement.executeUpdate("drop table if exists measure_data");//判断是否有表tables的存在。有则删除
  79. //设备结构数据库
  80. String sqlEquipment = "CREATE TABLE if not exists measure_data("
  81. + "id INTEGER PRIMARY KEY AUTOINCREMENT,function_location VARCHAR(200),description VARCHAR(200),"
  82. + "data_item VARCHAR(100),measure_time DATETIME,data_type INT(3)," //data_type=1电阻校准数据,2电感校准数据,3绝缘电阻校准数据
  83. + "data_01 double(30),data_02 double(30),data_03 double(30),data_04 double(30),data_05 double(30),"
  84. + "data_06 double(30),data_07 double(30),data_08 double(30),data_09 double(30),data_10 double(30),"
  85. + "data_11 double(30),data_12 double(30),data_13 double(30),data_14 double(30),data_15 double(30),"
  86. + "data_16 double(30),data_17 double(30),data_18 double(30),data_19 double(30),data_20 double(30)"
  87. + ")";
  88. statement.executeUpdate(sqlEquipment);
  89. // statement.executeUpdate("INSERT INTO measure_data VALUES (1, '第一电厂-设备管理部-电机1', '第一电厂-设备管理部-电机1', '2022-03-22 15:16:18', '2022-03-22 15:16:18',1,"
  90. // + "0.05, 0.82, 3, 80, 130, 180, 402, 502, 809, 1103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)");//向数据库中插入数据
  91. // ResultSet rSet=statement.executeQuery("select * from measure_data");//搜索数据库,将搜索的放入数据集ResultSet中
  92. // while (rSet.next()) { //遍历这个数据集
  93. // System.out.println("function_location:"+rSet.getString(2));//依次输出 也可以这样写 rSet.getString(“name”)
  94. // System.out.println("data_01:"+rSet.getString(4));
  95. // }
  96. // rSet.close();//关闭数据集
  97. statement.close();
  98. }
  99. public static void close(){
  100. System.out.println("SQLite数据库连接关闭!");
  101. try {
  102. if(conn!=null) {
  103. conn.close();
  104. }
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. //设备结构数据库
  110. // 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)," +
  111. // "isleaf INT(3))";
  112. // statement.executeUpdate("drop table if exists tree_structure");//判断是否有表tables的存在。有则删除
  113. // statement.executeUpdate(sqlEquipment);
  114. // statement.executeUpdate("INSERT INTO tree_structure VALUES (1, '第一电厂', '第一电厂', NULL, 1, 0)");//向数据库中插入数据
  115. // statement.executeUpdate("INSERT INTO tree_structure VALUES (2, '第一电厂-设备管理部', '第一电厂-设备管理部', '第一电厂', 2, 0)");//向数据库中插入数据
  116. // statement.executeUpdate("INSERT INTO tree_structure VALUES (3, '第一电厂-设备管理部-发电机组', '第一电厂-设备管理部-发电机组', '第一电厂-设备管理部', 3, 0)");//向数据库中插入数据
  117. // statement.executeUpdate("INSERT INTO tree_structure VALUES (4, '第一电厂-设备管理部-发电机组-电机1', '第一电厂-设备管理部-发电机组-电机1', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  118. // statement.executeUpdate("INSERT INTO tree_structure VALUES (5, '第一电厂-设备管理部-发电机组-电机2', '第一电厂-设备管理部-发电机组-电机2', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  119. // statement.executeUpdate("INSERT INTO tree_structure VALUES (6, '第二电厂', '第二电厂', NULL, 1, 0)");//向数据库中插入数据
  120. // statement.executeUpdate("INSERT INTO tree_structure VALUES (7, '第二电厂-设备管理部', '第二电厂-设备管理部', '第二电厂', 2, 0)");//向数据库中插入数据
  121. // statement.executeUpdate("INSERT INTO tree_structure VALUES (8, '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  122. // statement.executeUpdate("INSERT INTO tree_structure VALUES (10, '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  123. // statement.executeUpdate("INSERT INTO tree_structure VALUES (11, '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  124. // statement.executeUpdate("INSERT INTO tree_structure VALUES (12, '第一电厂-设备管理部-发电机组-电机8', '第一电厂-设备管理部-发电机组-电机8', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  125. // statement.executeUpdate("INSERT INTO tree_structure VALUES (13, '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  126. // statement.executeUpdate("INSERT INTO tree_structure VALUES (14, '第一电厂-设备管理部-发电机组-电机7', '第一电厂-设备管理部-发电机组-电机7', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  127. // statement.executeUpdate("INSERT INTO tree_structure VALUES (15, '第一电厂-设备管理部-发电机组-电机9', '第一电厂-设备管理部-发电机组-电机9', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  128. // statement.executeUpdate("INSERT INTO tree_structure VALUES (16, '第一电厂-设备管理部-发电机组-电机10', '第一电厂-设备管理部-发电机组-电机10', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
  129. // ResultSet rSet=statement.executeQuery("select * from tree_structure");//搜索数据库,将搜索的放入数据集ResultSet中
  130. // /**
  131. // * 通过用户名到数据库中获取凭证密码
  132. // * @param userName
  133. // * @return
  134. // */
  135. // private static String getPasswordByUserName(String userName) {
  136. // //SQL语句
  137. // String sql = "select password from users where username = " +"'" + userName+"'";
  138. // Connection conn = JdbcUtils.getConn();
  139. // Statement stmt=null;
  140. // ResultSet ret = null;
  141. // String password=null;
  142. // try {
  143. // stmt = conn.createStatement();
  144. // //执行语句,得到结果集
  145. // ret = stmt.executeQuery(sql);
  146. // while (ret.next()) {
  147. // //这里只查询的密码
  148. // password = ret.getString(1);
  149. // }
  150. // ret.close();
  151. // conn.close();//关闭连接
  152. // } catch (SQLException e1) {
  153. // e1.printStackTrace();
  154. // }
  155. // return password;
  156. // }
  157. }