Inserting into jtable

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 28
Reputation: ashish2234 is an unknown quantity at this point 
Solved Threads: 0
ashish2234 ashish2234 is offline Offline
Light Poster

Inserting into jtable

 
0
  #1
Aug 17th, 2009
Actually , i must admit , i did not knew about connecting with database but i am trying to learn , i want to insert data into jtable from Ms acess database , i have made a table called song_library there and also created the dsn as testy1...Also in the in init function where the jTable is called i have written the code for connecting to the databse!! And i also know there is some problem i my code, i am posting that part of the code here:

  1. public void init() {
  2. try {
  3.  
  4.  
  5. java.awt.EventQueue.invokeAndWait(new Runnable() {
  6.  
  7. public void run() {
  8. initComponents();
  9. loadDB();
  10. }
  11. });
  12.  
  13. } catch (Exception ex) {
  14. ex.printStackTrace();
  15. }
  16. }
  17. public void loadDB()
  18. {
  19. try {
  20. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  21. Connection c = DriverManager.getConnection("jdbc:odbc:testy1");
  22. Statement s = c.createStatement();
  23. ResultSet rs = s.executeQuery("select * from song_library");
  24. while (rs.next()) {
  25. jTable2.add(rs.getString(2),new Label()) ;
  26.  
  27. }
  28.  
  29. } catch (Exception e) {
  30.  
  31. }
  32.  
  33. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Inserting into jtable

 
0
  #2
Aug 18th, 2009
A way to add rows to JTable :

  1. // Header
  2. Object[] col = new Object[]{"Col1","Col2"};
  3. // Data
  4. Object[][] data = {}; // Blank
  5.  
  6. DefaultTableModel model = new DefaultTableModel(data,col);
  7.  
  8. JTable table = new JTable(model);
  9. // OR
  10. table.setModel(model);
  11.  
  12.  
  13. // Adding Rows
  14. model.addRow(new Object[]{"Data1","Data2"});

Hope this will help you.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC