import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
public class Registration extends JFrame implements ActionListener
{
JLabel[] lbl_obj = new JLabel[20];
JTextField[] tfld_obj = new JTextField[20];
String[] fld_value = new String[20];
JButton[] btn_obj = new JButton[8];
JLabel l1;
JLabel[] btn_name = new JLabel[8];
//JPanel jp1, jp2, jp3;
ResultSet rs;
JTable table;
Registration()
{
table = new JTable();
l1 = new JLabel("Employee Details:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
lbl_obj[0] = new JLabel("First Name:");
lbl_obj[1] = new JLabel("Last Name:");
lbl_obj[2] = new JLabel("Email");
lbl_obj[3] = new JLabel("phone number");
lbl_obj[4] = new JLabel("Hire Date :");
lbl_obj[5] = new JLabel("Job ID:");
lbl_obj[6] = new JLabel("Department Name:");
btn_obj[0] = new JButton("Query");
btn_obj[1] = new JButton("Clear");
btn_obj[2] = new JButton("Next");
btn_obj[3] = new JButton("Previous");
btn_obj[4] = new JButton("Multiple");
btn_obj[5] = new JButton("Done");
btn_obj[6] = new JButton("Last");
for (int i = 0; i < 7; i++)
{
tfld_obj[i] = new JTextField();
}
for (int i = 0; i < 7; i++) btn_obj[i].addActionListener(this);
l1.setBounds(100, 30, 400, 30);
int j = 70;
for (int i = 0; i < 7; i++)
{
lbl_obj[i].setBounds(80, j, 200, 30);
tfld_obj[i].setBounds(300, j, 200, 30);
j = j + 40;
}
j = 50;
for (int i = 0; i < 7; i++)
{
btn_obj[i].setBounds(j, 400, 100, 30);
j = j + 120;
}
for (int i = 0; i < 7; i++)
{
add(lbl_obj[i]);
add(tfld_obj[i]);
add(btn_obj[i]);
}
table.setBounds(0, 700, 600, 700);
add(table);
table.setVisible(false);
setExtendedState(JFrame.MAXIMIZED_BOTH);
//setSize(1200, 800);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Employee Details");
setVisible(true);
System.out.println("Completed initialisation");
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn_obj[0])
{
for (int i = 0; i < 7; i++)
{
fld_value[i] = tfld_obj[i].getText();
}
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "oracle");
StringBuilder sqlStatement = new StringBuilder("select e.FIRST_NAME,e.last_name,e.email,e.phone_number,to_char(e.hire_date,'DD-Mon-YYYY') hire_date,e.job_id, d.department_name from employees e,departments d where e.department_id=d.department_id");
int flag = 0;
int j = 1;
for (j = 0; j < 7; j++)
{
if (!fld_value[j].isEmpty())
{
flag = 1;
break;
}
}
if (flag == 1)
{
if (!fld_value[0].isEmpty())
{
sqlStatement.append(" AND First_Name =\'").append(fld_value[0]).append("\'");
}
if (!fld_value[1].isEmpty())
{
sqlStatement.append(" AND last_Name = \'").append(fld_value[1]).append("\'");
}
if (!fld_value[2].isEmpty())
{
sqlStatement.append(" AND Email = \'").append(fld_value[2]).append("\'");
}
if (!fld_value[3].isEmpty())
{
sqlStatement.append(" AND phone_number = \'").append(fld_value[3]).append("\'");
}
if (!fld_value[4].isEmpty())
{
sqlStatement.append(" AND hire_date = \'").append(fld_value[4]).append("\'");
}
if (!fld_value[5].isEmpty())
{
sqlStatement.append(" AND job_id = \'").append(fld_value[4]).append("\'");
}
if (!fld_value[6].isEmpty())
{
sqlStatement.append(" AND department_name = \'").append(fld_value[4]).append("\'");
}
}
String sqlStmt = new String();
sqlStmt = sqlStatement.toString();
System.out.println("Sql statement generated:" + sqlStmt);
PreparedStatement ps = con.prepareStatement(sqlStmt, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
if (rs != null)
{
rs.next();
for (int i = 0; i < 7; i++) tfld_obj[i].setText(rs.getString(i + 1));
System.out.println("First Instance");
}
System.out.println("Resulset obtained");
}
catch (Exception ex)
{
System.out.println(ex);
}
}
if (e.getSource() == btn_obj[1])
{
for (int i = 0; i < 7; i++) tfld_obj[i].setText("");
}
if (e.getSource() == btn_obj[2])
{
try
{
if (!btn_obj[3].isEnabled()) btn_obj[3].setEnabled(true);
rs.next();
{
for (int i = 0; i < 7; i++) tfld_obj[i].setText(rs.getString(i + 1));
}
if (rs.isLast()) btn_obj[2].setEnabled(false);
}
catch (Exception ex)
{
System.out.println("This is the last record");
}
}
if (e.getSource() == btn_obj[3])
{
try
{
if (!btn_obj[2].isEnabled()) btn_obj[2].setEnabled(true);
rs.previous();
{
for (int i = 0; i < 7; i++) tfld_obj[i].setText(rs.getString(i + 1));
}
if (rs.isFirst()) btn_obj[3].setEnabled(false);
}
catch (Exception ex)
{
System.out.println("This is the first record");
}
}
if (e.getSource() == btn_obj[4])
{
try
{
System.out.println("In multiple mode");
if (btn_obj[4].getText().equals("Multiple"))
{
System.out.println("trying to initialise Jtable");
table = new JTable(buildTableModel(rs));
table.setVisible(true);
btn_obj[4].setText("Single");
}
else
{
table.setVisible(false);
btn_obj[4].setText("Multiple");
}
}
catch (Exception ex)
{
System.out.println("Error while initialising table");
}
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException
{
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector < String > columnNames = new Vector < String > ();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++)
{
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector < Vector < Object >> data = new Vector < Vector < Object >> ();
while (rs.next())
{
Vector < Object > vector = new Vector < Object > ();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++)
{
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
public static void main(String args[])
{
System.out.println("Program started");
new Registration();
}
}This code snippet for Swing Form with Query Clear Next Previous Operations on database.