Search Records From Database Using Swing GUI in Java

Introduction

 
This article explains how to search user records from a database by their name in the Swing GUI of Java. The NetBeans IDE is used to create the sample examples.
 

Searching Records using Swing GUI

 
For creating this app we need the following files:
  1. Java file (SwingSearchApp.java)
  2. SQL table (emp.sql)
1. SwingSearchApp.java
 
This Java file consists of the entire logic. First of all we initialize the JFrame components using a constructor then create a database connection and finally set the database value to the textfield. If the given name is not found in the database then it displays an error message and displays it by running the constructor.
 
2. emp.sql
 
For fetching records we need a database table; for that we create an "emp" table in our database.
 
emp.sql
  1. create table emp  
  2.  (  
  3.     uname varchar2(20), umail varchar2(30),  
  4.     upass varchar2(20), ucountry varchar2(20)  
  5.  );  
Insert some rows
 
The following SQL will insert some rows in it:
  1. insert into emp values ('sandeep''[email protected]''welcome''India');  
  2.   
  3. insert into emp values ('rahul''[email protected]' , '123''India');  
Now let's start creating this app. Use the following procedure to do that in the NetBeans IDE.
 
Step 1
 
Open the NetBeans IDE.
 
NetBeans
 
Step 2
 
Choose "Java" -> "Java application" as shown below.
 
Java Application
 
Step 3
 
Type your project name as "SwingSearchApp" as in the following.
 
SwingSearchApp
 
Step 4
 
Now write the following code in the "SwingSearchApp.java" file.
 
SwingSearchApp.java
  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. import java.awt.*;  
  4. import java.sql.*;  
  5. public class SwingSearchApp extends JFrame implements ActionListener {  
  6. //Initializing Components  
  7.     JLabel lb, lb1, lb2, lb3, lb4, lb5;  
  8.     JTextField tf1, tf2, tf3, tf4, tf5;  
  9.     JButton btn;  
  10.     //Creating Constructor for initializing JFrame components  
  11.     SwingSearchApp() {  
  12.         //Providing Title  
  13.         super("Fetching Student Information");  
  14.         lb5 = new JLabel("Enter Name:");  
  15.         lb5.setBounds(202010020);  
  16.         tf5 = new JTextField(20);  
  17.         tf5.setBounds(1302020020);  
  18.         btn = new JButton("Submit");  
  19.         btn.setBounds(505010020);  
  20.         btn.addActionListener(this);  
  21.         lb = new JLabel("Fetching Student Information From Database");  
  22.         lb.setBounds(308045030);  
  23.         lb.setForeground(Color.red);  
  24.         lb.setFont(new Font("Serif", Font.BOLD, 20));  
  25.         setVisible(true);  
  26.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  27.         setSize(500500);  
  28.         lb1 = new JLabel("U_Name:");  
  29.         lb1.setBounds(2012010020);  
  30.         tf1 = new JTextField(50);  
  31.         tf1.setBounds(13012020020);  
  32.         lb2 = new JLabel("U_Mail:");  
  33.         lb2.setBounds(2015010020);  
  34.         tf2 = new JTextField(100);  
  35.         tf2.setBounds(13015020020);  
  36.         lb3 = new JLabel("U_Pass:");  
  37.         lb3.setBounds(2018010020);  
  38.         tf3 = new JTextField(50);  
  39.         tf3.setBounds(13018020020);  
  40.         lb4 = new JLabel("U_Country:");  
  41.         lb4.setBounds(2021010020);  
  42.         tf4 = new JTextField(50);  
  43.         tf4.setBounds(13021010020);  
  44.         setLayout(null);  
  45.         //Add components to the JFrame  
  46.         add(lb5);  
  47.         add(tf5);  
  48.         add(btn);  
  49.         add(lb);  
  50.         add(lb1);  
  51.         add(tf1);  
  52.         add(lb2);  
  53.         add(tf2);  
  54.         add(lb3);  
  55.         add(tf3);  
  56.         add(lb4);  
  57.         add(tf4);  
  58.         //Set TextField Editable False  
  59.         tf1.setEditable(false);  
  60.         tf2.setEditable(false);  
  61.         tf3.setEditable(false);  
  62.         tf4.setEditable(false);  
  63.     }  
  64.     public void actionPerformed(ActionEvent e) {  
  65.         //Create DataBase Coonection and Fetching Records  
  66.         try {  
  67.             String str = tf5.getText();  
  68.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  69.             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521""sandeep""welcome");  
  70.             PreparedStatement st = con.prepareStatement("select * from emp where uname=?");  
  71.             st.setString(1, str);  
  72.             //Excuting Query  
  73.             ResultSet rs = st.executeQuery();  
  74.             if (rs.next()) {  
  75.                 String s = rs.getString(1);  
  76.                 String s1 = rs.getString(2);  
  77.                 String s2 = rs.getString(3);  
  78.                 String s3 = rs.getString(4);  
  79.                 //Sets Records in TextFields.  
  80.                 tf1.setText(s);  
  81.                 tf2.setText(s1);  
  82.                 tf3.setText(s2);  
  83.                 tf4.setText(s3);  
  84.             } else {  
  85.                 JOptionPane.showMessageDialog(null"Name not Found");  
  86.             }  
  87.             //Create Exception Handler  
  88.         } catch (Exception ex) {  
  89.             System.out.println(ex);  
  90.         }  
  91.     }  
  92.     //Running Constructor  
  93.     public static void main(String args[]) {  
  94.         new SwingSearchApp();  
  95.     }  
  96. }  
Step 5
 
Now our project is ready to run. Right-click on the project menu then choose "Run". The following output is generated.
 
Output
 
Step 6
 
Now type the names of students to search for.
 
Case 1
 
Enter an incorrect name as in the following and then click on "Submit".
 
Output
 
Showing error
 
Note:
  1. JOptionPane.showMessageDialog(null"Name not Found");  
We use this dialogue box for generating an error message. So a new dialogue box appears with the message.
 
Case 2
 
Searching records for "rahul".
 
Rahul provided in name part
 
rahul records
 
Case 3
 
Searching records for "sandeep".
 
sandeep records


Similar Articles