943,590 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1496
  • Java RSS
Aug 17th, 2009
0

Inserting into jtable

Expand Post »
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:

JAVA Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ashish2234 is offline Offline
28 posts
since Jul 2009
Aug 18th, 2009
0

Re: Inserting into jtable

A way to add rows to JTable :

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: any collections of how to BUILd OR MAKE JAVA applications
Next Thread in Java Forum Timeline: i need sollution





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC