Java Database Connectivity : Searching Data

How to search for data on a database through Java GUI. We will be using a list to search for data in a table according to its primary key and showing its details on the text-fields.



1 Follow the 1-5 steps of Viewing Data in GUI.
2 In the design view,insert text-fields according to your database to show the details and a search button.
3 Insert a list swing control.Now do as directed to create a new list model – Right click on list » properties » click on the ellipsis button of model property » select Custom Code from the drop down menu » write new DefaultListModel () in the given space.
4 Under your package and before class starts write the following codes in the source tab.

  import java.sql.*;

 import javax.swing.JOptionPane;
 import javax.swing.DefaultListModel;
5 Write the coding on the button’s actionPerformed event for the connectivity and for populating the data in the list.
DefaultListModel list=(DefaultListModel) jList1.getModel();
    String sql=”Select * from library”;
    try {
        Connection con= (Connection) DriverManager.getConnection(“jdbc:mysql://localhost:3306/database1“,”root”,””);
        Statement stmt=con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
            String no=rs.getString(“no”);
    catch(Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
6 Now write these codes on the list’s mouseClicked event for making the data appear on text-fields when we select a row in the list.

try {
        Connection con= (Connection) DriverManager.getConnection(“jdbc:mysql://localhost:3306/database1“,”root”,””);
        Statement stmt=con.createStatement();
        String num  = (String) jList1.getSelectedValue();
        String sql1 = “Select * from library where no = ‘” + (num) + “‘”;
        ResultSet rs = stmt.executeQuery(sql1);
            while (
            String no = rs.getString(“NO”);
            String title= rs.getString(“TITLE”);
            String auth= rs.getString(“AUTHOR”);
    catch(Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());

7 Change the codes in blue according to your database. Finally, run the file(Shift +F6)

Run View

Add some labels and uncheck the editable property of text-fields to make the design look better.
After searching the data, you can allow users to update or delete rows.
// watch a video tutorial for java database connectivity on the videos page

19 thoughts on “Java Database Connectivity : Searching Data”

  1. @Anonymous(pls write ur name also)
    ☻check the coding again.
    its root and not Root
    ☻Write your database_name and not 'database1'
    ☻type the password in the last "" …if u have set in MySQl

  2. Iv Done All of this but i want to use a combo box list instead if just simple list what changes will i have to make, to this code, would really appreciate any feedback.

    Just Need to know the changes made on on step 5


  3. @khanny
    DefaultComboBoxModel model=(DefaultComboBoxModel) jComboBox1.getModel();
    String no=rs.getString("no");

  4. Can you do a tutorial on how to apply a auto-complete function onto a jtextfield , from a row of SQL, if you can, or can you refer me to any links, i would really appreciate any feedback I've been trying for 5 days now no look.


  5. @Anonymous for simplicity and for ensuring that user enters the correct date, use 3 text-fields differently for date-month-year.
    concat the 3 variables with '-' in between and then pass it through the sql query.

  6. hello
    I have a problem at the third point.Ive inserted a list swing control, but how can I create a new list model, I dont understand? I need urgent help.

  7. @nekken100 i don't why u are not able to do it, it's simple just Right-Click your list which u have dropped on the form. Now, go to properties and find the where model is written on the left-side. Click on the model and press ctrl+space, a dialog would open and now from the drop-down menu which is situated at top select custom-code. Finally, paste the code given in the article above.

  8. rohan tried it and it worked but the button's code when clicking it several times it displays alot of the same output

  9. cant compile, am getting this error.

    Updating property file:
    Compiling 1 source file to C:UsersBillionsDocumentsNetBeansProjectsOJDBCbuildclasses error: cannot find symbol
    symbol: method newDefaultListModel()
    location: class Workers
    Note: uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error

    what do i do?

Leave a Comment

Your email address will not be published. Required fields are marked *