Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problems inserting data into a postgres table  RSS feed

 
Moezz Hasnaoui
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problems inserting data into a postgres table created using java. The created table part of the code works fine, its only when I am inserting values into the table that nothing happens. The code I am using to populate the table records is:
//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();
}
}
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is very hard to read because it's not indented or formatted in any way. In future would you please UseCodeTags (<-- that's a link, follow it and read the page there)?

So I didn't really read the code much. Let me just guess and ask whether your database is configured in such a way that you have to commit transactions? I notice that you didn't do that, so if it's a necessity that would explain why nothing happened. Or you could perhaps do some debugging to see whether the INSERT code is ever called.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!