-1

I am working with a java, SQL, swing JFrame project. I am new to programming and I've been trying to solve this problem for hours without any success. The program I am trying to create is supposed to connect to my database (which is working) and then display the data from SQL database into a jtable - which is not working 100%. Right now, the only thing that prints on the Jtable are the number "0". If I create a "Sout" the right info is printed in the normal NetBeans output.

I don't get an error, so the problem is quite complex. Thanks for your time=) My first thread here! Sorry for the mess.

THE PROBLEM: I don't get the correct output from my database to my Jtable. The right output should be USER related output e.g Name, telephone number - Not "0 0 0"

DATABASE

        public ArrayList<Kund> kundlista()throws ClassNotFoundException, 
        SQLException{
        ArrayList<Kund> kundlista11 = new ArrayList<>();

   try{


  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
  System.out.println("Connecting to database...");
  conn2 = DriverManager.getConnection("jdbc:sqlserver://milledb.database.windows.net:1433;database=Javamille;user=mille@milledb;password={Jagheter12};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");


  stmt2 = conn2.createStatement();
  String sql2;
  sql2 = "SELECT * FROM Kund";
  ResultSet rs2 = stmt2.executeQuery(sql2);
  Kund kund;
  //STEP 5: Extract data from result set
     while(rs2.next()){

     int Kund_TeleNr = rs2.getInt("Kund_TeleNr");
     int Kund_Pnr  = rs2.getInt("Kund_Pnr");
     String Kund_Fnamn = rs2.getString("Kund_Fnamn");
     String Kund_Enamn = rs2.getString("Kund_Enamn");

     System.out.print("Kundens telenr: " + Kund_TeleNr);

     System.out.println("Kundens efternamn: " + Kund_Pnr);

     kund=new Kund(rs2.getInt("Kund_Telenr"), rs2.getInt("Kund_Pnr"), rs2.getString("Kund_Fnamn"), rs2.getString("Kund_Enamn") );         
     kundlista11.add(kund);
     }


              //STEP 6: Clean-up environment
             rs2.close();
              stmt2.close();
           conn2.close();
               }catch(SQLException se){
         //Handle errors for JDBC
            se.printStackTrace();
           }catch(Exception e){


           }finally{
       try{
     if(stmt!=null)
        stmt.close();
          }catch(SQLException se2){
         }// nothing we can do
          try{
     if(conn!=null)
        conn.close();
  }catch(SQLException se){
     se.printStackTrace();
  }//end finally try

     }
      return kundlista11;

     }
    }    

SWING/UI class

 public class KundUI extends javax.swing.JFrame {

/**
 * Creates new form KundUI
 */
public KundUI() {
    initComponents();

}
public ArrayList<Kund> lista;
public ArrayList<Kund> kundlista() throws ClassNotFoundException{
      ArrayList<Kund> kundlistan = new ArrayList<>();



  try{

  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  Connection conn2 = null;
  Statement stmt2 = null; 

  System.out.println("123.");

  System.out.println("Connecting..");
  //STEP 3: Open a connection
  System.out.println("Connecting to database...");
  conn2 = 

DriverManager.getConnection("jdbc:sqlserver://milledb.database.windows.net:1433;database=Javamille;user=mille@milledb;password={Jagheter12};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");

  //STEP 4: Execute a query
  System.out.println("Hämtar kunder...");
  stmt2 = conn2.createStatement();
  String sql2;
  sql2 = "SELECT * FROM Kund";
  ResultSet rs2 = stmt2.executeQuery(sql2);
  Kund kund;
  //STEP 5: Extract data from result set
     while(rs2.next()){
     //Retrieve by column name
     int Kund_TeleNr = rs2.getInt("Kund_TeleNr");
     int Kund_Pnr  = rs2.getInt("Kund_Pnr");
     String Kund_Fnamn = rs2.getString("Kund_Fnamn");
     String Kund_Enamn = rs2.getString("Kund_Enamn");

     System.out.print("Kundens telenr: " + Kund_TeleNr);
     System.out.print("Kundens förfanmn: " + Kund_Fnamn);
     System.out.println("Kundens efternamn: " + Kund_Enamn);
     System.out.println("Kundens efternamn: " + Kund_Pnr);

     kund=new Kund(rs2.getInt("Kund_Telenr"), rs2.getInt("Kund_Pnr"), rs2.getString("Kund_Fnamn"), rs2.getString("Kund_Enamn") );         
     kundlistan.add(kund);
     }


  //STEP 6: Clean-up environment
  rs2.close();
  stmt2.close();
  conn2.close();
  }catch(SQLException e){
  //Handle errors for JDBC
 JOptionPane.showMessageDialog(null, e);
  }
       return kundlistan;

     }


    public void visa_kunder() throws ClassNotFoundException{


   DefaultTableModel model = (DefaultTableModel)KundInfoUI.getModel();
   Object[] row = new Object[4];
   for(int i=0;i<lista.size();i++){
       row[0]=lista.get(i).getTelenr();
       row[1]=lista.get(i).getPnr();   
       row[2]=lista.get(i).getFnamn();    
       row[3]=lista.get(i).getEnamn();
       model.addRow(row);


   }

}

MAIN

package javaprojekt;

 import java.sql.SQLException;
  import java.util.ArrayList;


 public class JavaProjekt {


public static void main(String[] args) throws ClassNotFoundException, 
    SQLException {
    Databas data = new Databas();
    BabbeJonas loginGui = new BabbeJonas();
    loginGui.setVisible(true);
    KundUI kundUiT = new KundUI();


      while (loginGui.password1234 == false){

     System.out.println("Hej");
    }
       if(data.login(loginGui.getUserName(), loginGui.getPassWord()) != 
       true){

    // databasen hämtar kunder
   ArrayList<Kund> kundlist; // Lista som finns i MAIN
   kundUiT.lista= (ArrayList<Kund>)data.kundlista().clone(); 
   kundUiT.setVisible(true);
   for(int i = 0; i < kundUiT.lista.size(); i++) {
        System.out.println(kundUiT.lista.get(i).getPnr());
    }




   while(true){







}
    }
}

}

  • What is the problem here ? – Youssef NAIT May 18 '17 at 08:46
  • I don't get the correct output from my database to my Jtable. The right output should be USER related output e.g Name, telephone number - Not "0 0 0" like right now. – Mille Gloerfelt Tarp May 18 '17 at 08:53
  • `while (loginGui.password1234 == false){` is not how you wait for user response in a GUI – MadProgrammer May 18 '17 at 08:57
  • That is for my login which is working correctly at the moment – Mille Gloerfelt Tarp May 18 '17 at 08:58
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 4) Don't ignore exceptions! They inform us exactly what went wrong. .. – Andrew Thompson May 18 '17 at 08:58
  • .. Unless logging is implemented, at least call `Throwable.printStackTrace()` – Andrew Thompson May 18 '17 at 09:00
  • 1
    I see only one row added to the `TableModel`, and I'm suspicious of `clone()`, which "Returns a shallow copy of this `ArrayList` instance." Try this complete [example](http://stackoverflow.com/a/34742409/230513) to study the problem in isolation. – trashgod May 18 '17 at 09:29

1 Answers1

0

you can fill your table with this line of code:

    KundInfoUI.setModel(DbUtils.resultSetToTableModel(rs2));
Nasser Hajlawi
  • 313
  • 2
  • 15