2016-05-01 42 views
0

我在将数据插入到使用java创建的postgres表中时遇到了问题。代码的创建表部分工作正常,只有当我将值插入到表中时才会发生任何事情。我使用的填充表记录的代码是:通过点击按钮不会插入数据库?

//the first class code 
     stmt = c.createStatement();  
    JavaApplication8 dc = new JavaApplication8(); 
      String sql = "INSERT INTO records (start_date,hour,cell_name,Erlang,ErlangU,cell_type,freq_type) VALUES (?,?,?,?,?,?,?)"; 
     PreparedStatement pst = c.prepareStatement(sql); 
      pst.setInt(1, dc.time()); 
      pst.setInt(2,heure); 
      pst.setString(3, "fgf"); 
      pst.setFloat(4, 84/10); 
      pst.setFloat(5,dc.Hourly_Traffic_900); 
      pst.setInt(6, 1); 
      pst.setInt(7, 900); 

     stmt.execute(sql); 
     stmt.close(); 
      c.commit(); 
      c.close(); 
        }} catch (ClassNotFoundException | SQLException e) { 
      System.err.println(e.getClass().getName()+": "+ e.getMessage()); 
      System.exit(0); 
      } 
      System.out.println("Records created successfully");} 

//the jframe code 
     Connection conn = new DBConnection().Connect(); 
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     try { 

      float bsc = Float.parseFloat(jTextField1.getText()); 
      float cellsbsc = Float.parseFloat(jTextField2.getText());   
       float days = Float.parseFloat(jTextField3.getText()); 
     int tot_dense =(int) (bsc*cellsbsc) ; 
      JavaApplication8 dc = new JavaApplication8(); 
      Menu mm = new Menu(); 
        Connection c = null; 
      Statement stmt = null; 

      Class.forName("org.postgresql.Driver"); 
      c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Records", "postgres", "21262050"); 
      c.setAutoCommit(false); 
      System.out.println("Opened database successfully"); 

     String sql = "INSERT INTO records (start_date,hour,cell_name,Erlang,ErlangU,cell_type,freq_type) VALUES (?,?,?,?,?,?,?)"; 
      PreparedStatement pst = c.prepareStatement(sql); 
       if (mm.jRadioButton1.isSelected()&& mm.jButton1.isSelected()){ 

        for(int i=1; i<tot_dense+1; i++) 
        { 
         for(int d=1; d<days+1; d++) 

         { 
          dc.Day_900(); 

      pst.setInt(1,dc.time()); 
      pst.setInt(2,dc.heure); 
      pst.setString(3, "fgf"); 
      pst.setFloat(4, 84/10); 
      pst.setFloat(5,dc.Hourly_Traffic_900); 
      pst.setInt(6, 1); 
      pst.setInt(7, 900); 

      pst.execute(); 
         } 
        } 
+0

1)使用的缩进代码的逻辑和一致的形式线和块。缩进旨在使代码的流程更易于遵循! 2)为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 –

+0

即使在上面的不可编译的代码片段中,该行'JavaApplication8 dc = new JavaApplication8();'也会出现两次。我怀疑这是问题的根源。为了交换对合理建议的怀疑,张贴该MCVE。 –

+0

'stmt'和'pst'之间有区别,请尝试执行'pst.executeUpdate' – MadProgrammer

回答

1

那么几件事情,立即站了出来:

  1. 你打电话Connection#setAutoCommit并将其设置为false,但从来没有叫Connection#commit
  2. 一般,您应该致电PreparedStatement#executeUpdate而不是execute。它不应该有重大区别,但executeUpdate实际上会返回受调用影响的行数,这可能有助于诊断问题。
  3. 有没有在你的代码没有任何迹象表明,你实际上关闭你的任何资源
  4. 你调用一个循环execute,这往往表明可能使用了批量更新

所以不是,你可以不喜欢......

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

    float bsc = Float.parseFloat(jTextField1.getText()); 
    float cellsbsc = Float.parseFloat(jTextField2.getText()); 
    float days = Float.parseFloat(jTextField3.getText()); 
    int tot_dense = (int) (bsc * cellsbsc); 
    JavaApplication8 dc = new JavaApplication8(); 
    Menu mm = new Menu(); 

    try { 
     Class.forName("org.postgresql.Driver"); 
     try (Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Records", "postgres", "21262050")) { 
      c.setAutoCommit(false); 
      System.out.println("Opened database successfully"); 

      String sql = "INSERT INTO records (start_date,hour,cell_name,Erlang,ErlangU,cell_type,freq_type) VALUES (?,?,?,?,?,?,?)"; 
      try (PreparedStatement pst = c.prepareStatement(sql)) { 
       if (mm.jRadioButton1.isSelected() && mm.jButton1.isSelected()) { 

        for (int i = 1; i < tot_dense + 1; i++) { 
         for (int d = 1; d < days + 1; d++) { 
          dc.Day_900(); 

          pst.setInt(1, dc.time()); 
          pst.setInt(2, dc.heure); 
          pst.setString(3, "fgf"); 
          pst.setFloat(4, 84/10); 
          pst.setFloat(5, dc.Hourly_Traffic_900); 
          pst.setInt(6, 1); 
          pst.setInt(7, 900); 

          pst.addBatch(); 
         } 
        } 
        pst.executeLargeBatch(); 
        //pst.executeBatch(); 
        c.commit(); 
       } 

      } 
     } catch (SQLException exp) { 
      exp.printStackTrace();Ï 
     } 
    } catch (ClassNotFoundException exp) { 
     exp.printStackTrace(); 
    } 
} 

看一看The try-with-resources Statement一些更多的细节。

说了这么多,那跳出来的是接下来的事情......

JavaApplication8 dc = new JavaApplication8(); 
Menu mm = new Menu(); 

您使用mm检查的一些单选按钮的状态,因此,除非Menu是一些模态对话框种类或单选按钮的默认选择状态设置为true,这是不可能的更新代码将永远执行

但它并没有停止在那里,看看第一个代码片段引发一些额外的问题.. 。

try { 
    stmt = c.createStatement(); 
    JavaApplication8 dc = new JavaApplication8(); 
    String sql = "INSERT INTO records (start_date,hour,cell_name,Erlang,ErlangU,cell_type,freq_type) VALUES (?,?,?,?,?,?,?)"; 
    PreparedStatement pst = c.prepareStatement(sql); 
    pst.setInt(1, dc.time()); 
    pst.setInt(2, heure); 
    pst.setString(3, "fgf"); 
    pst.setFloat(4, 84/10); 
    pst.setFloat(5, dc.Hourly_Traffic_900); 
    pst.setInt(6, 1); 
    pst.setInt(7, 900); 

    stmt.execute(sql); 
    stmt.close(); 
    c.commit(); 
    c.close(); 
} catch (ClassNotFoundException | SQLException e) { 
    System.err.println(e.getClass().getName() + ": " + e.getMessage()); 
    System.exit(0); 
} 

我不知道JavaApplication8是什么,但它令我担心,因为您依赖于该类的信息。

您同时使用StatementPreparedStatement,但你execute只有Statement这是没有意义的查询是建立一个PreparedStatement,所以这只是一个混乱的烂摊子。

如果一切都成功了,你也只关闭资源,如果查询失败,会发生什么情况,SQLException?这些资源保持开放!见The try-with-resources Statement一个更好的解决方案

所以,相反,代码“可能”看起来更多的东西一样......

try (Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Records", "postgres", "21262050")) { 
    JavaApplication8 dc = new JavaApplication8(); 
    String sql = "INSERT INTO records (start_date,hour,cell_name,Erlang,ErlangU,cell_type,freq_type) VALUES (?,?,?,?,?,?,?)"; 
    try (PreparedStatement pst = c.prepareStatement(sql)) { 
     pst.setInt(1, dc.time()); 
     pst.setInt(2, heure); 
     pst.setString(3, "fgf"); 
     pst.setFloat(4, 84/10); 
     pst.setFloat(5, dc.Hourly_Traffic_900); 
     pst.setInt(6, 1); 
     pst.setInt(7, 900); 
     pst.executeUpdate(); 
    } 
} catch (SQLException e) { 
    System.err.println(e.getClass().getName() + ": " + e.getMessage()); 
    System.exit(0); 
} 
System.out.println("Records created successfully"); 

例如