sqlite java查询不插入表

时间:2017-10-17 11:20:57

标签: java sqlite

拥有名为good_in的sqlite表,列in_idgood_codein_groupin_quantityin_VATPaid

这是我的表格示例 enter image description here

并有方法将记录插入其中

public static void inputGoods(GoodsInput goodsinput){
    String goodCode = goodsinput.getInGood().getGood_code();
    int goodBatch = goodsinput.getInGroup();
    int goodQuantity = goodsinput.getInQuantity();
    double goodVATPaid = goodsinput.getInVatPaid();
    String sqlInsert = "INSERT INTO good_in (good_code, in_group, in_quantity, in_VATPaid)"
            + " VALUES ('" + goodCode + "', " + "'" + goodBatch + "', " + "'" + goodQuantity
            + "', " + "'" +goodVATPaid + "');";
    System.out.print(sqlInsert);
    Connection conn = ConnectionFactory.ConnectDB();
    try{
        Statement statement = conn.createStatement();
        statement.executeUpdate(sqlInsert);
    }
    catch(SQLException e){
    }
}

连接类

        public static Connection ConnectDB(){ 
    try{ 
    Class.forName("org.sqlite.JDBC"); 
    Connection con = DriverManager.getConnection("jdbc:sqlite:kahuyq.db"); 
    return con; 
    } catch (HeadlessException | ClassNotFoundException | SQLException ex){ JOptionPane.showMessageDialog(null, ex); } 
    return null; 

}

当我将打印的查询复制到sqlite管理器时,它会添加行,但是从java结束程序时没有错误,但是没有向我的表添加行。 有什么问题?

还有其他方法可以检查表good_code中存在good的天气,其中只有idgood_code列,如果不存在,则添加它。从GoodsInput构造函数访问此方法。当我从构造函数中删除方法时,另一种方法可以正常工作。 这是方法

   public static void insertGoods(Good g){
    String sqlSelect = "Select * from good where good_code = '" 
            + g.getGood_code() + "'" ;
    String sqlInsert = "INSERT INTO good (good_code)"
            + "VALUES ('" + g.getGood_code() +"')";
    Connection conn = ConnectionFactory.ConnectDB();
    try{
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery(sqlSelect);


        while(!rs.next()){
            statement.executeUpdate(sqlInsert);
            break;
        }

    }
    catch(SQLException e){
    }
}

1 个答案:

答案 0 :(得分:0)

尝试添加

conn.commit();

执行你的陈述后。