Introduction
This article explains how to display Employee information from a database in a new Frame. The NetBeans IDE is used for creating this application.
How to Display Emp Information in a New Frame
For creating this app we need the following files.
- Java file
- ojdbc.jar file
- NetBeans IDE
- SQL table
1. Java File
This Java contains the programming code. In this file we use Swing components to display the emp data in a new frame after the employee name selection.
What we can do
A. Import several packages
First we need to import the following packages:
javax.swing.*;
java.awt.*;
java.awt.event.*;
java.sql.*;
java.util.Vector;
The Swing package is used for the swing components. All swing components are defined within this package. The AWT package provides the event handling mechanism, in other words it deals with events like "button-click". The SQL package creates the JDBC connection.
B. Extend the JFrame components and implements the ActionListener
Extends the JFrame components and implement the ActionListener as in the following:
class EmpSearchApp extends JFrame implements ActionListener
C. Declare components
Now declare the following components:
JLabel l, l1, l2, l3, l4,l5;
JButton b;
JTextField tf1, tf2, tf3, tf4;
JComboBox bx;
String str;
D. Declare Frame components
Now declare Frame components in a default constructor as in the following:
Syntax
EmpSearchApp()
{
......
......
try{
//JDBC CODE
}Catch(Exception ex)
{
System.out.println(ex)
}
......
}
Note: In the dotted part we declare and add various components of Swing; in this part the JDBC code is also used to get an Emp name from the database table that can used in JComboBox. The full code I'll show you below here; I'll only summarize for you what I can do.
D. Add a ActionListener
Add a ActionListener for the button clicked event as in the following:
public void actionPerformed(ActionEvent e) {
showData();
}
Note: If we have multiple buttons then we can use "if (e.getSource() == buttonName)". But in this app I can use only a single button "Submit" so there is no need to use "e.getSource". I used a method "showData()". In that method I wrote new frame code.
E. Create a new Frame
Create a new Frame in the showData() method as in the following:
public void showData() {
.........
try{
//JDBC CODE
}Catch(Exception ex)
{
System.out.println(ex)
}
.........
}
F. Create a main method and run the constructor
Finally, create a main method and run the constructor as in the following:
public static void main(String arr[]) {
new EmpSearchApp();
}
2. ojdbc.jar file
This JAR file provides a way to set up a Java connection with an Oracle Database. Since the JDBC connection is provided by the Oracle Server vendor we need to import this JAR file in our library folder.
3. NetBeans IDE
This IDE is used to create this application. Since we have a choice we can simply create this app with any text editor, like Notepad, Notepad++, etcetera. But by using NetBeans we can create the Frame and add components like "button", "label", etcetera directly without writing its code. I explained the advantages of the IDE in my previous article "Advantages Of Netbeans IDE" through which you can see the difference.
4. emp.sql table
For fetching records we need a database table; for that we create an "emp" table in our "sandeep" database.
Syntax
emp.sql
create table emp
(
uname varchar2(20), umail varchar2(30),
upass varchar2(20), ucountry varchar2(20)
);
Insert some rows into it as in the following:
1. insert into emp values ('sandeep', '[email protected]', 'welcome', 'India');
2. 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.
Step 2
Choose "Java" -> "Java application" as shown below.
Step 3
Provide "EmpSearchApp" for your project name as in the following and click on "Finish".
Step 4
Create a new Java Class "EmpSearchApp" with the following.
EmpSearchApp.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
public class EmpSearchApp extends JFrame implements ActionListener {
JLabel l, l1, l2, l3, l4, l5;
JButton b;
JTextField tf1, tf2, tf3, tf4;
JComboBox bx;
String str;
EmpSearchApp() {
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JDBC DEMO");
l = new JLabel("Select Name:");
b = new JButton("Submit");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
l.setBounds(20, 20, 200, 20);
b.setBounds(50, 50, 150, 30);
add(l);
add(b);
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
tf4.setEditable(false);
b.addActionListener(this);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");
PreparedStatement ps = con.prepareStatement("select uname from emp");
ResultSet rs = ps.executeQuery();
Vector v = new Vector();
while (rs.next()) {
String s = rs.getString(1);
v.add(s);
}
bx = new JComboBox(v);
bx.setBounds(240, 20, 200, 20);
add(bx);
} catch (Exception ex) {
System.out.println(ex);
}
}
public void actionPerformed(ActionEvent e) {
showData();
}
public void showData() {
JFrame f1 = new JFrame();
f1.setVisible(true);
f1.setSize(500, 500);
f1.setLayout(null);
f1.setTitle("JDBC DEMO");
l5 = new JLabel("Displaying Emp Data:");
l5.setForeground(Color.red);
l5.setFont(new Font("Serif", Font.BOLD, 20));
l1 = new JLabel("Emp Name:");
l2 = new JLabel("Emp Email:");
l3 = new JLabel("Emp pass:");
l4 = new JLabel("Emp Country:");
l5.setBounds(100, 50, 300, 30);
l1.setBounds(20, 110, 200, 20);
l2.setBounds(20, 140, 200, 20);
l3.setBounds(20, 170, 200, 20);
l4.setBounds(20, 200, 200, 20);
tf1.setBounds(240, 110, 200, 20);
tf2.setBounds(240, 140, 200, 20);
tf3.setBounds(240, 170, 200, 20);
tf4.setBounds(240, 200, 200, 20);
f1.add(l5);
f1.add(l1);
f1.add(tf1);
f1.add(l2);
f1.add(tf2);
f1.add(l3);
f1.add(tf3);
f1.add(l4);
f1.add(tf4);
str = (String) bx.getSelectedItem();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");
PreparedStatement ps = con.prepareStatement("select * from emp where uname=?");
ps.setString(1, str);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
tf1.setText(rs.getString(1));
tf2.setText(rs.getString(2));
tf3.setText(rs.getString(3));
tf4.setText(rs.getString(4));
}
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void main(String arr[]) {
new EmpSearchApp();
}
}
Step 5
Now your project is ready to run.
Right-click on the project menu and choose "Run". The following output is generated.
Step 6
Now choose from the given names. I chose "sandeep" first.
Step 7
Now choose another name as "rahul".