我在将数据插入到使用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