0

I am trying to add the databases to specific column,row the column always is the same but the row change. So I enter the variable and trying to add one to the number but not luck all is printing are 0 so is not adding any number to the variable for that reason my table stay in the same row and never change. I try i++; i=i+1; all I have is 0 on the println(). Im using Netbeans.

     Statement stmt = null;
      String sql="select * from gateway where date= "+id;
      try{
      Connect conn=new Connect();
      stmt = conn.makeStatement();        
       rs = stmt.executeQuery(sql);            
        while(rs.next()) {
            int i=0;
            Object ids = rs.getString("Business");
            Object items = rs.getString("GatewayJob");
            Object descriptions = rs.getString("Status");
            Object quantitys = rs.getString("Timework");
            Object price = rs.getString("Notes");      

            jTable1.getModel().setValueAt(ids,i, 0 );
            jTable1.getModel().setValueAt(items, i, 1);
            jTable1.getModel().setValueAt(descriptions, i, 2);
           jTable1.getModel().setValueAt(quantitys, i, 3);
          jTable1.getModel().setValueAt(price, i, 4);              
          System.out.println(i);
        i++;
        }
1
  • 1
    1) For better help sooner, post an SSCCE. 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. Commented Oct 4, 2013 at 1:58

4 Answers 4

2

In you code

while(rs.next()) {
            int i=0;

move the int i = 0; to before your while loop

this statement will reset i back to 0 everytime

Sign up to request clarification or add additional context in comments.

Comments

1

Don't use the setValueAt() method. This implies you have loaded the TableModel with a bunch of rows containing null values which is a bad design as you don't know how many rows the query will return.

Instead, you should be dynamically building the TableModel by adding a new row to the model in your loop.

See Table From Database, especially the Table From Database Example code for a simple solution that uses the DefaultTableModel.

2 Comments

This will pull the entire database column (no need it).
@gyuhh, Then don't use "select *", or only get the data from the columns that you need using the basic code your provided above. The point is to build the Vector and add the row to the model. Using setValueAt(...) is simply a terrible coding practice.
1

try this code. im using String connection. this codes is running.

String sql = "select * from tblstudent";

try{

database.Query(sql);

int inc=0;

while(database.dataTable.next()){

         Object a = database.dataTable.getInt("StudentID");
         Object b = database.dataTable.getString("Firstname");
         Object c = database.dataTable.getString("Lastname");
         Object d = database.dataTable.getString("Gender");
         Object e = database.dataTable.getInt("CourseID");

         tblStudent.getModel().setValueAt(a, inc, 0 );
         tblStudent.getModel().setValueAt(b, inc, 1);
         tblStudent.getModel().setValueAt(c, inc, 2);
         tblStudent.getModel().setValueAt(d, inc, 3);
         tblStudent.getModel().setValueAt(e, inc, 4);              

         inc++;
     }
 }
catch(SQLException ex){
     JOptionPane.showMessageDialog(null, "Error");
 }

}  

Comments

0

Because i is reset to 0 every while loop.
So you should put int i=0; before while().

int i=0;
while(rs.next()){
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.