0

there's my code, it doesn't work because shows emty table without any rows ir columns can someone tel where is problem

public class test extends JFrame {

    private final JButton pridetiNaujaButton = new JButton();
    private final JButton pasalintiButton = new JButton();
    private final JButton redagotiButton = new JButton();
    private final JButton paieskaButton = new JButton();
    final GtFromDb baz=new GtFromDb();
    private Vector <Vector<String>> data;
    private Vector<String> header;
    private final JScrollPane scrollPane = new JScrollPane();
    private final JTable table = new JTable(data,header);
    public static void main(String args[]) {
        try {
            test frame = new test();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Create the frame
     */
    public test() {
        super();
        setBounds(100, 100, 781, 412);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            jbInit();
        } catch (Throwable e) {
            e.printStackTrace();
        }


            GtFromDb db=new GtFromDb();
            try {
                data=db.getClient();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
            header=new Vector<String>();
            header.add("Imones pavadinimas");
            header.add("vardas");
            header.add("pavarde");
            header.add("salis");
            header.add("Adresas");
            header.add("telefonas");
        }


    private void jbInit() throws Exception {
        getContentPane().setLayout(null);

        getContentPane().add(pridetiNaujaButton);
        pridetiNaujaButton.setText("Prideti nauja");
        pridetiNaujaButton.setBounds(0, 0, 106, 26);

        getContentPane().add(pasalintiButton);
        pasalintiButton.setText("Pasalinti");
        pasalintiButton.setBounds(112, 0, 106, 26);

        getContentPane().add(redagotiButton);
        redagotiButton.setText("Redagoti");
        redagotiButton.setBounds(224, 0, 106, 26);

        getContentPane().add(paieskaButton);
        paieskaButton.setText("Paieska");
        paieskaButton.setBounds(336, 0, 106, 26);

        getContentPane().add(scrollPane);
        scrollPane.setBounds(10, 48, 745, 316);     
        scrollPane.setViewportView(table);

    }
    public class GtFromDb
    { Connection connect;
      Statement zadanie;
      String sql;
      ResultSet dane;
    //  Map<String,String> carmap =new HashMap<String,String>();
    //  Map<String,String> ownmap =new HashMap<String,String>();
    void DbConnection() throws SQLException
    {
        String baza="jdbc:odbc:dielektric_repair";
        try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch(Exception e){System.out.println("Connection error");}
        connect = DriverManager.getConnection(baza,"","");
    }
    public Vector getClient() throws Exception
    {
    Vector <Vector<String>> clientVector=new Vector<Vector<String>>();  
    try
    {DbConnection();
    PreparedStatement zadanie = connect.prepareStatement("SELECT * FROM Clients");

    dane = zadanie.executeQuery();
    while(dane.next()) {
    Vector <String> client=new Vector<String>();
     String imonesPav=dane.getString("Imones_pavadinimas");
     String vardas = dane.getString("Vardas");
     String pavarde = dane.getString("Pavarde");
     String salis = dane.getString("Salis");
     String adresas=dane.getString("Adresas");
     String telefonas = dane.getString("Telefonas");
     if (imonesPav != null) {imonesPav = imonesPav.trim();
     client.add(imonesPav);}
     if (vardas != null) {vardas = vardas.trim();
     client.add(vardas);}
     if (pavarde != null) {pavarde = pavarde.trim();
     client.add(pavarde);}
     if (salis != null) {salis =salis.trim();
     client.add(salis);}
     if (adresas != null) {adresas =adresas.trim();
     client.add(adresas);}
     if (telefonas != null) {telefonas = telefonas.trim();
     client.add(telefonas);}
    }zadanie.close();
    }catch(SQLException e){}
    return clientVector;    
    }
    }
    public JScrollPane getScrollPane() {
        return scrollPane;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Edgar Buchvalov
  • 257
  • 2
  • 12
  • 22
  • What are you doing with the data obtained from the DB? On a side note, extending JFrame, using Vectors are not recommended practices. – asgs Jul 18 '11 at 10:35

1 Answers1

1

There are a few issues:

1) You are not adding the client objects to the clientVector. Use clientVector.add(client) to add the client after each iteration of the ResultSet.

2) You have initialised the table with null data and header variables. First, initialise the data and header variables and then the table, like this:

private final JTable table ;

public test() {
    super();
    setBounds(100, 100, 781, 412);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    header=new Vector<String>();
    header.add("Imones pavadinimas");
    header.add("vardas");
    header.add("pavarde");
    header.add("salis");
    header.add("Adresas");
    header.add("telefonas");

    GtFromDb db=new GtFromDb();
    try {
        data=db.getClient();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
    table = new JTable(data,header);    

    try {
        jbInit();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

3) You are catching a SQLException and doing nothing with it. So if there is an error running the query, you will not know about it. Try printing out the stack trace:

try{
   ...
   dane = zadanie.executeQuery();
}catch(SQLException e) {
   e.printStackTrace();
} finally {
   zadanie.close();
}
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • OP's was noticed about that in your "same question" http://stackoverflow.com/questions/6730701/java-set-data-to-jtable-from-database +1 – mKorbel Jul 18 '11 at 11:02
  • and please amend `clientVector.add(client)` to `data.add(client)` – mKorbel Jul 18 '11 at 11:05
  • Are you sure that your query is returning data and that the clientVector is being populated correctly? Try printing out the clientVector to check. – dogbane Jul 18 '11 at 12:07
  • checked, prints out all data well – Edgar Buchvalov Jul 18 '11 at 12:30
  • i get an error in this line zadanie.close(); java.lang.NullPointerException at test$GtFromDb.getClient(test.java:134) – Edgar Buchvalov Jul 18 '11 at 12:31
  • that wrong as I mentioned, if you and OP adds `Vector client` to the `TableModel`, then row is visible immediatelly into `JTable`, really why there are exist two `Vector> clientVector` and another `Vector` for same funcioanlities and linked from `TableModel` - `Vector> data`, anyway just initialized both 2D `Vectors` to avoiding `NPE` – mKorbel Jul 18 '11 at 12:35
  • The OP is getting a NPE on `zadanie.close()`. This has nothing to do with the vectors. Both vectors have been initialised. Also, there is nothing wrong with having two vectors. One of them is an instance variable and the other is a local variable being returned from a method. – dogbane Jul 18 '11 at 12:44
  • @edgar-buchvalov you need to check if zadanie is null. It would also be nice if you could update the question with your new code. – dogbane Jul 18 '11 at 12:52
  • I don't like to complicated very simple things, that not good and correct direction, why to reinvent the wheel – mKorbel Jul 18 '11 at 12:57