我在將數據插入到使用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();
}
}
1)使用的縮進代碼的邏輯和一致的形式線和塊。縮進旨在使代碼的流程更易於遵循! 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –
即使在上面的不可編譯的代碼片段中,該行'JavaApplication8 dc = new JavaApplication8();'也會出現兩次。我懷疑這是問題的根源。爲了交換對合理建議的懷疑,張貼該MCVE。 –
'stmt'和'pst'之間有區別,請嘗試執行'pst.executeUpdate' – MadProgrammer