| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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<ProdReq> 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<ProdReq> 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;
- }
- }
-
|