| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- package com.mes.util;
- import com.mes.ygsl.MesProductBs;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- public class JdbcUtils {
- public static final Logger log = LoggerFactory.getLogger(JdbcUtils.class);
- //通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查
- public static Connection conn = null;
- public static String Drivde="org.sqlite.JDBC";
- public static String DATABASE_URL="jdbc:sqlite:mes_db.db";
- public static Connection getConn(){
- try {
- Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
- conn = DriverManager.getConnection("jdbc:sqlite:mes_db.db");//连接数据库zhou.db,不存在则创建
- log.info("连接到SQLite数据库成功!");
- create_bw_record();//初始化结构表
- create_bw_prod_bs();
- //create_measure_data();//初始化测试数据表
- } catch (Exception e) {
- // TODO Auto-generated catch block
- close();//关闭数据库连接
- e.printStackTrace();
- }
- return conn;
- }
- public static void create_bw_record() throws SQLException {
- Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
- //设备结构数据库
- String sqlEquipment = "CREATE TABLE if not exists bw_record("
- + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),gy VARCHAR(20),message_type VARCHAR(20),sn VARCHAR(48),bw VARCHAR(1000),record_time DATETIME,"
- + "info_01 VARCHAR(200),info_02 VARCHAR(200),info_03 VARCHAR(200))";
- // statement.executeUpdate("drop table if exists bw_record");//判断是否有表tables的存在。有则删除
- statement.executeUpdate(sqlEquipment);
- // 创建 提交记录表
- String submitRecord = "CREATE TABLE if not exists submit_record(\n" +
- " id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
- " oprno VARCHAR(20), -- 工位号 \n" +
- " sn VARCHAR(48), -- 二维码\n" +
- " bw VARCHAR(1000), -- 报文 \n" +
- " record_time DATETIME, -- 记录时间\n" +
- " state CHAR(1) -- 状态(0 ->未提交, 1 ->已提交)\n" +
- ")";
- statement.executeUpdate(submitRecord);
- log.info("表创建成功!");
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (1, '第一电厂', '第一电厂', '', 1, 0,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (2, '第一电厂-设备管理部', '第一电厂-设备管理部', '第一电厂', 2, 0,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (3, '第一电厂-设备管理部-电机1', '第一电厂-设备管理部-电机1', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (4, '第一电厂-设备管理部-电机2', '第一电厂-设备管理部-电机2', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (5, '第一电厂-设备管理部-电机3', '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (6, '第一电厂-设备管理部-电机4', '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (7, '第一电厂-设备管理部-电机5', '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部',3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (9, '第一电厂-设备管理部-电机6', '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部',3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (10, '第二电厂', '第二电厂', '', 1, 0,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (11, '第二电厂-设备管理部', '第二电厂-设备管理部', '第二电厂', 2, 0,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (12, '第二电厂-设备管理部-电机1', '第二电厂-设备管理部-电机1', '第二电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (13, '第二电厂-设备管理部-电机2', '第二电厂-设备管理部-电机2', '第二电厂-设备管理部', 3, 1,'','','','','','')");//向数据库中插入数据
- // log.info(sqlEquipment);
- // ResultSet rSet=statement.executeQuery("select * from bw_record");//搜索数据库,将搜索的放入数据集ResultSet中
- // while (rSet.next()) { //遍历这个数据集
- // log.info("gw:"+rSet.getString(1));//依次输出 也可以这样写 rSet.getString("name")
- // //log.info("密码:"+rSet.getString("pwd"));
- // }
- // rSet.close();//关闭数据集
- statement.close();
- }
- //插入数据
- public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
- boolean ret = false;
- String record_time = DateLocalUtils.getCurrentTime();
- if(conn==null) {
- ret = false;
- }else {
- try {
- //创建连接对象,是Java的一个操作数据库的重要接口
- Statement statement=conn.createStatement();
- statement.executeUpdate("INSERT INTO bw_record (gw,gy,bw,record_time,message_type,sn) VALUES"
- + " ('"+gw+"', '"+gy+"', '"+bw+"', '"+record_time+"','"+message_type+"','"+sn+"')");//向数据库中插入数据
- statement.close();
- ret = true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- //e.printStackTrace();
- ret = false;
- }
- }
- return ret;
- }
- // 向 submit_record表 插入提交记录数据
- public static boolean insertSubmitRecord(String oprno, String sn, String bw){
- boolean ret = false;
- String record_time = DateLocalUtils.getCurrentTime();
- if(conn==null) {
- ret = false;
- }else {
- try {
- Statement statement=conn.createStatement();
- String insertSQL = "INSERT INTO submit_record (oprno, sn, bw, record_time, state)" +
- "VALUES('" + oprno + "', '" + sn + "', '" + bw + "', '" + record_time + "', '0')";
- statement.executeUpdate(insertSQL);
- statement.close();
- ret = true;
- log.info("向submit_record表插入数据成功: " + insertSQL);
- } catch (SQLException e) {
- ret = false;
- log.info("向submit_record表插入数据失败");
- }
- }
- return ret;
- }
- public static void create_measure_data() throws SQLException {
- Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
- //statement.executeUpdate("drop table if exists measure_data");//判断是否有表tables的存在。有则删除
- //设备结构数据库
- String sqlEquipment = "CREATE TABLE if not exists measure_data("
- + "id INTEGER PRIMARY KEY AUTOINCREMENT,function_location VARCHAR(200),description VARCHAR(200),"
- + "data_item VARCHAR(100),measure_time DATETIME,data_type INT(3)," //data_type=1电阻校准数据,2电感校准数据,3绝缘电阻校准数据
- + "data_01 double(30),data_02 double(30),data_03 double(30),data_04 double(30),data_05 double(30),"
- + "data_06 double(30),data_07 double(30),data_08 double(30),data_09 double(30),data_10 double(30),"
- + "data_11 double(30),data_12 double(30),data_13 double(30),data_14 double(30),data_15 double(30),"
- + "data_16 double(30),data_17 double(30),data_18 double(30),data_19 double(30),data_20 double(30)"
- + ")";
- statement.executeUpdate(sqlEquipment);
- // statement.executeUpdate("INSERT INTO measure_data VALUES (1, '第一电厂-设备管理部-电机1', '第一电厂-设备管理部-电机1', '2022-03-22 15:16:18', '2022-03-22 15:16:18',1,"
- // + "0.05, 0.82, 3, 80, 130, 180, 402, 502, 809, 1103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)");//向数据库中插入数据
- // ResultSet rSet=statement.executeQuery("select * from measure_data");//搜索数据库,将搜索的放入数据集ResultSet中
- // while (rSet.next()) { //遍历这个数据集
- // log.info("function_location:"+rSet.getString(2));//依次输出 也可以这样写 rSet.getString(“name”)
- // log.info("data_01:"+rSet.getString(4));
- // }
- // rSet.close();//关闭数据集
- statement.close();
- }
- public static void close(){
- log.info("SQLite数据库连接关闭!");
- try {
- if(conn!=null) {
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //设备结构数据库
- // 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)," +
- // "isleaf INT(3))";
- // statement.executeUpdate("drop table if exists tree_structure");//判断是否有表tables的存在。有则删除
- // statement.executeUpdate(sqlEquipment);
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (1, '第一电厂', '第一电厂', NULL, 1, 0)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (2, '第一电厂-设备管理部', '第一电厂-设备管理部', '第一电厂', 2, 0)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (3, '第一电厂-设备管理部-发电机组', '第一电厂-设备管理部-发电机组', '第一电厂-设备管理部', 3, 0)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (4, '第一电厂-设备管理部-发电机组-电机1', '第一电厂-设备管理部-发电机组-电机1', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (5, '第一电厂-设备管理部-发电机组-电机2', '第一电厂-设备管理部-发电机组-电机2', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (6, '第二电厂', '第二电厂', NULL, 1, 0)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (7, '第二电厂-设备管理部', '第二电厂-设备管理部', '第二电厂', 2, 0)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (8, '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部-发电机组-电机3', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (10, '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部-发电机组-电机4', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (11, '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部-发电机组-电机5', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (12, '第一电厂-设备管理部-发电机组-电机8', '第一电厂-设备管理部-发电机组-电机8', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (13, '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部-发电机组-电机6', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (14, '第一电厂-设备管理部-发电机组-电机7', '第一电厂-设备管理部-发电机组-电机7', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (15, '第一电厂-设备管理部-发电机组-电机9', '第一电厂-设备管理部-发电机组-电机9', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // statement.executeUpdate("INSERT INTO tree_structure VALUES (16, '第一电厂-设备管理部-发电机组-电机10', '第一电厂-设备管理部-发电机组-电机10', '第一电厂-设备管理部-发电机组', 4, 1)");//向数据库中插入数据
- // ResultSet rSet=statement.executeQuery("select * from tree_structure");//搜索数据库,将搜索的放入数据集ResultSet中
- // /**
- // * 通过用户名到数据库中获取凭证密码
- // * @param userName
- // * @return
- // */
- // private static String getPasswordByUserName(String userName) {
- // //SQL语句
- // String sql = "select password from users where username = " +"'" + userName+"'";
- // Connection conn = JdbcUtils.getConn();
- // Statement stmt=null;
- // ResultSet ret = null;
- // String password=null;
- // try {
- // stmt = conn.createStatement();
- // //执行语句,得到结果集
- // ret = stmt.executeQuery(sql);
- // while (ret.next()) {
- // //这里只查询的密码
- // password = ret.getString(1);
- // }
- // ret.close();
- // conn.close();//关闭连接
- // } catch (SQLException e1) {
- // e1.printStackTrace();
- // }
- // return password;
- // }
- //创建扭力扳手数据表
- public static void create_bw_prod_bs() throws SQLException {
- Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
- //设备结构数据库
- String sqlEquipment = "CREATE TABLE if not exists bw_prod_bs("
- + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),line_sn VARCHAR(20),sn VARCHAR(48),"
- + "torque VARCHAR(20),result VARCHAR(20),sync int(10) NULL DEFAULT 0,"
- + "ucode VARCHAR(50),record_time DATETIME,tighteningID VARCHAR(30),jobID VARCHAR(30))";
- //statement.executeUpdate("drop table if exists bw_prod");//判断是否有表tables的存在。有则删除
- statement.executeUpdate(sqlEquipment);
- System.out.println("表bw_prod_bs创建成功!");
- statement.close();
- }
- //插入扭力扳手数据
- public static boolean insertProdData(String gw, String lineSn, String sn, String torque, String result, String ucode,String tighteningID,String jobID) {
- boolean ret = false;
- String record_time = DateLocalUtils.getCurrentTime();
- JdbcUtils.getConn();
- if(conn==null) {
- ret = false;
- }else {
- try {
- //创建连接对象,是Java的一个操作数据库的重要接口
- Statement statement=conn.createStatement();
- statement.executeUpdate("INSERT INTO bw_prod_bs (gw,line_sn,sn,torque,result,ucode,record_time,tighteningID,jobID) VALUES"
- + " ('"+gw+"', '"+lineSn+"', '"+sn+"', '"+torque+"', '"+result+"', '"+ucode+"', '"+record_time+"', '"+tighteningID+"','"+jobID+"')");//向数据库中插入数据
- statement.close();
- ret = true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- ret = false;
- }
- }
- return ret;
- }
- public static void openConnection() {
- try {
- Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
- conn = DriverManager.getConnection(DATABASE_URL);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- // 如果连接失败,尝试重连
- reconnect();
- }
- }
- private static void reconnect() {
- try {
- // 关闭旧连接
- if (conn != null && !conn.isClosed()) {
- conn.close();
- }
- // 重新建立连接
- conn = DriverManager.getConnection(DATABASE_URL);
- } catch (SQLException e) {
- e.printStackTrace();
- // 如果重连失败,可以进一步处理异常,比如记录日志、通知管理员等
- }
- }
- //查询数据是否存在
- public static boolean checkTighteningById(String tighteningId,String jobID,String sn) {
- boolean ret = false;
- try{
- // 确保连接已经打开
- if (conn == null || conn.isClosed()) {
- openConnection();
- }
- Statement statement=conn.createStatement();
- ResultSet retset = statement.executeQuery("SELECT count(*) FROM bw_prod_bs WHERE tighteningID = '"+tighteningId+"' AND jobID = '"+jobID+"' AND sn = '"+sn+"' LIMIT 1");//向数据库中插入数据
- Integer count = 0;
- while (retset.next()) {
- count = retset.getInt(1);
- }
- statement.close();
- if(count > 0){
- ret = true;
- }
- }catch (Exception e){
- e.printStackTrace();
- ret = false;
- }
- return ret;
- }
- public static void updateProdSync(Integer id,Integer sync) throws SQLException {
- Connection conn = JdbcUtils.getConn();
- Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
- statement.executeUpdate("update bw_prod_bs set sync = "+sync + " where id = "+id);
- statement.close();
- }
- public static List<MesProductBs> getProds() {
- //SQL语句
- 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";
- Connection conn = JdbcUtils.getConn();
- Statement stmt=null;
- ResultSet ret = null;
- String password=null;
- List<MesProductBs> prods = new ArrayList<>();
- try {
- stmt = conn.createStatement();
- //执行语句,得到结果集
- ret = stmt.executeQuery(sql);
- System.out.println("sql:"+ sql);
- while (ret.next()) {
- MesProductBs prodReq = new MesProductBs();
- prodReq.setId(ret.getInt(1));
- prodReq.setOprno(ret.getString(2));
- prodReq.setLineSn(ret.getString(3));
- prodReq.setSn(ret.getString(4));
- prodReq.setTorque(ret.getString(5));
- prodReq.setResult(ret.getString(6));
- prodReq.setCreateBy(ret.getString(7));
- prodReq.setCreateDate(ret.getString(8));
- prods.add(prodReq);
- }
- ret.close();
- conn.close();//关闭连接
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- return prods;
- }
- }
|