package com.mes.util; import com.mes.ui.ProdReq; import java.sql.*; import java.util.ArrayList; import java.util.List; public class JdbcUtils { //通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查 public static Connection conn = null; public static String Drivde="org.sqlite.JDBC"; public static Connection getConn(){ try { Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc conn = DriverManager.getConnection("jdbc:sqlite:mes_db.db");//连接数据库zhou.db,不存在则创建 System.out.println("连接到SQLite数据库成功!"); create_bw_record();//初始化结构表 create_bw_prod(); } 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); System.out.println("表record创建成功!"); 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; } public static void create_bw_prod() throws SQLException { Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口 //设备结构数据库 String sqlEquipment = "CREATE TABLE if not exists bw_prod(" + "id INTEGER PRIMARY KEY AUTOINCREMENT,gw VARCHAR(20),line_sn VARCHAR(20),type VARCHAR(20),sn VARCHAR(48)," + "fout VARCHAR(20),sout VARCHAR(20),fmin VARCHAR(20),smin VARCHAR(20),fmax VARCHAR(20),smax VARCHAR(20)," + "qty VARCHAR(5),serial_number VARCHAR(50),sync int(10) NULL DEFAULT 0,record_time DATETIME,ucode VARCHAR(50))"; // 0=未同步到MES 1=已同步到MES //statement.executeUpdate("drop table if exists bw_prod");//判断是否有表tables的存在。有则删除 statement.executeUpdate(sqlEquipment); System.out.println("表prod创建成功!"); statement.close(); } public static void close(){ System.out.println("SQLite数据库连接关闭!"); try { if(conn!=null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } //插入拉铆数据 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) { 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 (gw,line_sn,type,sn,fout,sout,fmin,smin,fmax,smax,qty,serial_number,ucode,record_time) VALUES" + " ('"+gw+"', '"+lineSn+"', '"+type+"', '"+sn+"', '"+fout+"', '"+sout+"', '"+fmin+"', '"+smin+"', '"+fmax+"', '"+smax+"', '"+qty+"','"+serial_number+"','"+ucode+"','"+record_time+"')");//向数据库中插入数据 statement.close(); ret = true; } catch (SQLException e) { // TODO Auto-generated catch block 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 set sync = "+sync + " where id = "+id); statement.close(); } public static List getProds() { //SQL语句 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"; Connection conn = JdbcUtils.getConn(); Statement stmt=null; ResultSet ret = null; String password=null; List prods = new ArrayList<>(); try { stmt = conn.createStatement(); //执行语句,得到结果集 ret = stmt.executeQuery(sql); System.out.println("sql:"+ sql); while (ret.next()) { ProdReq prodReq = new ProdReq(); prodReq.setId(ret.getInt(1)); prodReq.setGw(ret.getString(2)); prodReq.setLineSn(ret.getString(3)); prodReq.setType(ret.getString(4)); prodReq.setSn(ret.getString(5)); prodReq.setFout(ret.getString(6)); prodReq.setSout(ret.getString(7)); prodReq.setFmin(ret.getString(8)); prodReq.setSmin(ret.getString(9)); prodReq.setFmax(ret.getString(10)); prodReq.setSmax(ret.getString(11)); prodReq.setQty(ret.getString(12)); prodReq.setSerialNumber(ret.getString(13)); prodReq.setSync(ret.getInt(14)); prodReq.setRecordTime(ret.getString(15)); prodReq.setUcode(ret.getString(16)); prods.add(prodReq); } ret.close(); conn.close();//关闭连接 } catch (SQLException e1) { e1.printStackTrace(); } return prods; } }