开发者

Java set data to JTable from database

i try to insert data from database to Jtable there is my code:

private Vector <Vector<String>> data;
private Vector<String> header;
table.setModel(new javax.swing.table.DefaultTableModel(
data,header     
));

GtFromDb db=new GtFromDb();
data=db.ge开发者_高级运维tClient();

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

public Vector getClient() 
{
    Vector <Vector<String>> clientVector=new Vector<Vector<String>>();  
    try
    {
         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;    
    }

it thorows me error:

java.lang.NullPointerException
at test$GtFromDb.getClient(test.java:108)
at test.<init>(test.java:49)
at test.main(test.java:24)

where is problem?


you forgot to added and another ...

1) client=new Vector<String>(); to the DefaultTableModel inside loop while(dane.next()) {...}

2) public Vector getClient() must be initialized return null

3) change public Vector getClient() to public void getClient(), remove return statement

4) PreparedStatement zadanie must be closed in finally block try- catch - finally

5) dane = zadanie.executeQuery(); looks like as Resultset, and same as PreparedStatement zadanie must be closed in finally block

6) JTable and Vector can hold null value

7) if you are only to want to display data from DbTable to the JTable then look for ResultsetTableModel, TableFromDatabase, you can search for that on this forum too


Try these Codes Structure But in yours :

DefaultTableModel dtm = new DefaultTableModel();
dtm.setColumnCount(2);
dtm.setRowCount(2);
dtm.setValueAt("Imones pavadinimas", 1, 1);
dtm.setValueAt("Imones vardas", 2, 1);
dtm.setValueAt("Pavarde", 1, 2);
dtm.setValueAt("Salis", 2, 2);
 ...
jTable1.setModel(dtm);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜