I'm using NetBeans 5.5

First :I want to bind data from table from MS Access DB into JTable .

Second: Is there any other component in java to do the previous action(e.g. DataGrid )

Recommended Answers

All 3 Replies

There may be some custom open-source code available out on the net somewhere, but there is no native component to do what you want.

To get data into a JTable, you will need to establish a JDBC connection (with JDBC-ODBC bridge for Access), obtain a ResultSet via a query, and then build a table model from those results.

If you look in your JDK installation directory there is a directory called "demo". If you go to "demo\jfc\TableExample", there are several examples of using tables with database connections. Those should provide plenty of sample code to get you started.

Thanks so much for reply :) .

After searching , frankly I couldn't make my DB to show it's info. in JTable.
However , I've made my application to use some GUI but shows the data using
"System.out.printIn("") "

that's my code :

import javax.swing.*;
   import java.sql.*;
   import javax.swing.event.TableModelListener;
   import javax.swing.table.TableModel;


public class MyConnection {

    public MyConnection() {
    
    }
    
      public static Connection getConnection() throws Exception {
      Driver d = (Driver)Class.forName
     ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    Connection c = DriverManager.getConnection(
     "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Inventory.mdb"
      );
    return c;
       
    }
      
      //******************************************
      
      
      
      static Connection theConn;
     // public static String sql;
      public void retrieveData(String sql )
      {
          
                   try {
             
      // connection to an ACCESS MDB
      theConn = MyConnection.getConnection();

      ResultSet rs;
      Statement stmt;      
     
      stmt = theConn.createStatement();
      rs = stmt.executeQuery(sql);
      boolean FirstLoop=true;
      boolean DataExisted=false;
            
      while (rs.next())
      {
          if(FirstLoop==true)
          {
                 System.out.append("ID");
                 System.out.append("\t");                 
                          System.out.append("Product_Name");
                          System.out.append("\t");
                                    System.out.append("Product_Price");
                                    System.out.append("\t");
                                                System.out.append("Amount_In_Stock");
                                                System.out.append("\t");
                                                
                                                System.out.println("");
                                                FirstLoop=false;
                                                DataExisted=true;
          }
          
           System.out.append(rs.getString("ID"));
           System.out.append("\t\t");
           System.out.append(rs.getString("Product_Name"));
           System.out.append("\t\t");
           System.out.append(rs.getString("Product_Price"));
           System.out.append("\t\t");
           System.out.println(rs.getString("Amount_In_Stock"));
           
           
         
      }   
      
      if(DataExisted==false)
          System.out.println("No Result is found ");
      System.out.println("__________________________________________________________________");
      System.out.println("__________________________________________________________________");
      rs.close();
      stmt.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
      try {
        if (theConn != null) theConn.close();
      }
      catch (Exception e) {
      }
    }
      }
}

Did you look at the code in your demo folder? Getting the data into a JTable is really not too difficult and you should be able to modify those examples pretty easily to meet you needs.

You can fill a DefaultTableModel very easily by supplying a vector of the column names and a Vector of Vectors containing the data for each row. After the model is filled you call JTable.setModel() to make it use the model you generated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.