Java GUI Programming

Search Data and Put in the JTextBoxes or JLabels

Most of students have problems with how to search data from the database and how display those data in a text fields. But don’t worry furthermore… It is very easy to do fetch and display that from a MySQL database. I will explain step by step.

First I have created a new project and named it as searchData. I think you already know how to do that. Ok let’s we deep in the project. I have a database called simple and there is a table called name. I have already created two rows. You can see in the project what the rows are. I have attached a text file and in that file I have given you how to create that database, table and rows. At the bottom of the post you have a download link.

Code explanation

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

public class connection {

public Connection creatConnection () throws ClassNotFoundException
{
Connection conn=null;

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();//call jdbc driver
String url = "jdbc:mysql://localhost/simple";//getting url
conn = DriverManager.getConnection(url,"root","1234");//create connection
}

catch (Exception e)
{
// JOptionPane.showMessageDialog(null,"Error occured!.."+ e.getLocalizedMessage());
JOptionPane.showMessageDialog(null, e, "Example",JOptionPane.ERROR_MESSAGE, null);//exeption hanling
}

return conn;//return connection to all
}

}

This class I have already explained.

import java.sql.Connection;
import java.sql.ResultSet;          // importing essential libries
import javax.swing.JOptionPane; 

public class search {

connection c = new connection();    // create  objects
Connection getcon=null;
ResultSet rs=null;

public ResultSet find(String radioValue,String textValue) // method find
{
try
{

getcon=c.creatConnection(); // getting connection
rs=getcon.createStatement().executeQuery("select * from name where "+ radioValue+"='" + textValue + "' ");

// search data and get the resultset

}
catch(Exception ex)
{

JOptionPane.showMessageDialog(null, ex);// exception handling
}

return rs; // return the ResultSet

}

}

 

 

Code explanation

public ResultSet find(String radioValue,String textValue){}

This method has ResultSet type return value. It means it will return result set in to other class. To get this return value you must have an object and it type should be ResultSet. And there are two parameters in the method and values from the JFrame class pass over to this method and those values will use inside the method body.

rs=getcon.createStatement().executeQuery("select * from name where "+ radioValue+"='" + textValue + "' ");

In this code it will search data from the database and those data will be added to ResultSet.

 return rs;

Now data in the ResultSet and this object must be returned.


You have to design GUI and above picture is the structure.You can see my GUI window and change variable names in appropriate names and also put one radio button always selected when you load the form. To do that you can use Property window. See the picture how to do that.See picture

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) { 

search s = new search();    // creating objects
ResultSet rs=null;
String a ="firstname"; // define variables
String b ="lastname";

if(rdoFname.isSelected()==true){ // if radio button has selected execute the if ststement

rs=s.find(a, txtSearch.getText().trim()); // get the returned ResultSet in to this ResultSet

}

else
{
rs=s.find(b, txtSearch.getText().trim());

}
try{

if(rs.next()){
lblFname.setText(rs.getString("firstname")); // getting the values from the database according to the rows
lblLname.setText(rs.getString("lastname")); // and display it
}
}

catch (Exception ex){

JOptionPane.showMessageDialog(null, ex); // exceptio handling

}

// TODO add your handling code here:
}

I have used my own style and you can use your own style to do codes. There are two variables to store the row names of the table and I used it for the purpose of ease to code. If the radio First Name selected we have to search according to the first name from the database. Otherwise we have to search from the last name.

if(rs.next()){
lblFname.setText(rs.getString("firstname")); // getting the values from the database according to the rows
lblLname.setText(rs.getString("lastname")); // and display it
}
}

This is the code to display results using ResultSet.Those results will be displayed in the JLabel.And also another important thing you want to know. Did you see when the project run, the main frame loaded into left side corner or center? But in previous examples it loaded into left side top of the monitor. Use method .setBounds() to set that. See the pictures to learn how to do that.

Final result should similar to this picture.


Download

the example from here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s