我在将数据插入使用 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();
}
}
最佳答案
因此,有几件事立即脱颖而出:
- 您调用
Connection#setAutoCommit
并将其设置为false
,但从不调用Connection#commit
- 通常,您应该调用
PreparedStatement#executeUpdate
而不是execute
。它应该不会产生显着差异,但executeUpdate
实际上返回受调用影响的行数,这有助于诊断问题。 - 您的代码中没有任何迹象表明您实际上正在关闭任何资源
- 您正在循环中调用
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
是什么,但它让我担心,因为您依赖于该类的信息。
您同时使用了Statement
和PreparedStatement
,但是您执行
仅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");
例如
关于java - 单击按钮不会插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36961675/