Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

swing - 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.getClient();

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...