Start New Discussion within our Software Development Community

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:

public void init() {
        try {


            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                    loadDB();
                }
            });

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void loadDB()
    {
           try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection c = DriverManager.getConnection("jdbc:odbc:testy1");
            Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select * from song_library");
            while (rs.next()) {
                jTable2.add(rs.getString(2),new Label()) ;
               
            }

        } catch (Exception e) {
            
        }

    }

A way to add rows to JTable :

// Header
			Object[] col = new Object[]{"Col1","Col2"};
			// Data
			Object[][] data = {}; // Blank
			
			DefaultTableModel model = new DefaultTableModel(data,col);
			
			JTable table = new JTable(model);
			// OR
			table.setModel(model);
			
			
			// Adding Rows
			model.addRow(new Object[]{"Data1","Data2"});

Hope this will help you.

This article has been dead for over six months. Start a new discussion instead.