java - JTable - same items displayed -
as can see when trying new items got repetition. should do?
i wrote jtable
redisplayer class. have add button table. when click button should exp addded 1. after add 2. should display 1 2 only. not 1 1 2 that.
jtable1.removeall(); jtable1.revalidate(); try{ class.forname("com.mysql.jdbc.driver"); } catch(classnotfoundexception e){ system.err.println("driver yok"); return; } connection con=null; try{ con=drivermanager.getconnection("jdbc:mysql://localhost:3306/kutuphane","root",""); system.out.println("veritabnı baglandıldı"); statement stmt=con.createstatement(); resultset rs=stmt.executequery("select * kisiler "); while(rs.next()){ model=(defaulttablemodel) jtable1.getmodel(); model.insertrow(i,new object[]{rs.getstring("adsoyad"),rs.getint("telefon"),rs.getint("tcno"),rs.getstring("adres") } ); i++; } jtable1.setmodel(model); jtable1.revalidate(); stmt.close(); rs.close(); } catch(sqlexception e){ system.out.println("veritabanı baglanmadi"); e.printstacktrace(); } finally{ if(con!=null) try{ con.close(); } catch(sqlexception e){ e.printstacktrace(); }
first, rid of line @ top of code
jtable1.removeall(); //delete line!!
you expecting remove lines table model, that's not does.
next, replace while
loop , 2 lines after with:
model = new defaulttablemodel(); while(rs.next()){ model.addrow(new object[]{ rs.getstring("adsoyad"), rs.getint("telefon"), rs.getint("tcno"), rs.getstring("adres")} ); } jtable1.setmodel(model);
note using addrow
instead of insertrow
, i
no longer necessary.
Comments
Post a Comment