Populate Data From Database Into JTable in Netbeans

download Populate Data From Database Into JTable in Netbeans

of 13

Transcript of Populate Data From Database Into JTable in Netbeans

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    1/13

    Populate data from database into JTable in Netbeans

    Bychang November 18, 2008 Email This Post Print This PostPost a comment

    How to populate data from database into JTable in Netbeans?

    Suppose we had a table called Employee in our MS Access database and we want to retrieve and

    display all the data in a JTable component using NetBeans IDE. How to do that and what are thecodes that we need to modify?

    In this example, i am using an MS Access database and created a table called employee. This

    table contains fileds empid, name, position and department. Some sample data was added too.

    http://chang.advits.com/author/changhttp://chang.advits.com/author/changhttp://chang.advits.com/author/changhttp://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/print/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/print/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans#commentshttp://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans#commentshttp://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans#commentshttp://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/print/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/print/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/print/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans#commentshttp://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/print/http://chang.advits.com/populate-data-from-database-into-jtable-in-netbeans/email/http://chang.advits.com/author/chang
  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    2/13

    Lets follow the steps to do it:

    Step 1: Create the table in MS Access and make sure you put the database file inside yourNetBeans Project Folder.

    Step 2: Create database engine to retrieve data from the Access table:Create a class called DBEngine to retrieve the data from Access (Make sure you had created a

    new NetBeans Project first for this tutorial) Note: i usedproject.engine package for example

    only. You are free to create yours.

    The following code is done in the DBEngine.java. Basically its a class to retrieve all the data

    from Employee table using JDBC.

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    3/13

    package project.engine;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.util.Vector;

    /**

    *

    * @author cgg

    */

    public class DBEngine

    {

    /**

    * Connect to database

    * @return Connection to database

    * @throws java.lang.Exception

    */

    public Connection dbConnection()throws Exception

    {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    String myDB ="jdbc:odbc:Driver={Microsoft Access Driver

    (*.mdb)};DBQ=pay.MDB";

    return DriverManager.getConnection(myDB,"","");

    }

    /**

    * This method will load vector of vector of string and load all the data in

    * the vector

    * @return vector of vector of string

    * @throws java.lang.Exception

    */

    public Vector getEmployee()throws Exception

    {

    Vector employeeVector = new Vector();

    Connection conn = dbConnection();

    PreparedStatement pre = conn.prepareStatement("select * from employee");

    ResultSet rs = pre.executeQuery();

    while(rs.next())

    {

    Vector employee = new Vector();

    employee.add(rs.getString(1)); //Empid

    employee.add(rs.getString(2)); //name

    employee.add(rs.getString(3)); //positionemployee.add(rs.getString(4)); //department

    employeeVector.add(employee);

    }

    /*Close the connection after use (MUST)*/

    if(conn!=null)

    conn.close();

    return employeeVector;

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    4/13

    }

    }

    Step 3: Now create the GUI with a JTable component inside it.

    Create a new JFrame Form

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    5/13

    I named the class TableExample and created underproject.gui package.

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    6/13

    Add a JTable component onto the Frame.

    Step 4: After inserting the JTable component, switch to Source view, we need to insert coding to

    retrieve data from database.

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    7/13

    Add a JTable component onto the Frame.

    Step 5: Add the following codes as shown below.Code you need to add are:

    line 03 and 04 (import packages)

    line 12 and 13 (instantiate two Vector objects, to hold table data and table header)

    line 16 (add throws Exception)line 19 to 27 (to populate data from database into the Vector object and also initialize the table

    header Vector object)

    package project.gui;

    import java.util.Vector;

    import project.engine.DBEngine;

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    8/13

    /**

    *

    * @author cgg

    */

    public class TableExamples extends javax.swing.JFrame {

    private Vector data; //used for data from database

    private Vector header; //used to store data header

    /** Creates new form TableExamples */

    public TableExamples() throws Exception{

    //get data from database

    DBEngine dbengine = new DBEngine();

    data = dbengine.getEmployee();

    //create header for the table

    header = new Vector();

    header.add("EmpID"); //Empid

    header.add("Name"); // employee name

    header.add("Position"); // employee positionheader.add("Department"); // employee department

    initComponents();

    }

    .

    .

    .

    .

    Step 6: switch back to design view and right click on the JTable component, then select theCustomize Code button.

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    9/13

    Choose customize code option to customize the codes.

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    10/13

    The customize code page will be shown as follow:

    Customize code page

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    11/13

    Step 7: change the default code into custom property and change the code as follow:

    change the codes to let the table to populate the Vector object we created just now.

    Step 8: you need to update the main method to handle exception using try catch block

    Search for the main method in the code view and modify it with adding trycatch block as

    shown follow:

    /**

    * @param args the command line arguments

    */

    public static void main(String args[]) {

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

    public void run() {

    try

    {

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    12/13

    new TableExamples().setVisible(true);

    }catch(Exception e){e.printStackTrace();}

    }

    });

    }

    Step 9: now build and run the project or the file, you should be able to get the following output:

    Output:

    Table populated with data from database.

    Thats it. Give it a try.Note: in this example, what i had done is just to populate the data from database into JTable

    component through Netbeans IDE. As u can see, it deals with purpose of displaying data only. if

  • 7/23/2019 Populate Data From Database Into JTable in Netbeans

    13/13

    you want more actions to be done such as update, delete, etc, you need to use proper data binding

    techniques in Netbeans.