0% found this document useful (0 votes)
5 views

Class 12th Project on Java & MySQL Connectiviy

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Class 12th Project on Java & MySQL Connectiviy

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Mahesh Memorial Public School , Bagh

Project
On
Java and MySql Connectivity

Project work submitted in partial fulfillment


of the requirement for the All India Senior
Secondary Certificate Exam (AISSCE) Session
2024-25

INFORMATION TECHNOLGY (802)

Student Name Teacher’s Name


_______________ ________________

CERTIFICATE

This is hereby to certify that original


and genuine work has been carried to work on
the matter and the related data collection and
investigation has been completed slowly,
sincerely and satisfactorily by
____________________of class XII
‘___________’ of Sankalp International School,
Kukshi regarding project title “Java And MySql
Connectivity”.

Internal Examiner Signature Principal’s Signature


External Examiner Signature

ACKNOWLEDGEMENT

I am very thankful and expressing my deep


sense of gratitude towards _______________,
who inspired me to work on this topic. I
benefited a lot with discussing with them on
this topic. I am also thankful to my parents
who provided me such an opportunity, for
their inspiring words. I am also thankful to all
my colleagues and all those who helped me in
completing this project.

Contents
Objectives of the project.............................

INTRODUCTION TO MySQL..........................

Features of Java: ...........................................

Java and MySql Connectivity Demonstration……

Screenshot of MySQL....................................

Bibliography...................................................

Objectives of The Project


The objective of the software project is to develop a
Java and MySql Connectivity. The proposed software
system is expected to do the following functionality-To
provide a user friendly, Graphical User Interface (GUI)
based integrated and centralized environment. The
proposed system should maintain all the records and
should generate the required reports and information
when required. To provide user-friendly interface to
interact with a centralized database based on client-
server architecture. In its current scope, the software
enables user to retrieve and update the information
from centralized database designed with MySQL.
During the development of Java and MySql
Connectivity project, Java NetBeans IDE, a powerful,
open source event-driven form-based development
environment is used for modular design and future
expandability of the system.

INTRODUCTION TO JAVA
Java is a computer programming language that is
concurrent, class-based, object-oriented And
specifically designed to have as few implementation
dependencies as possible. It is intended to let
application developers”write once, run anywhere"
(WORA),meaning that Code that runs on one platform
does not need to be recompiled to run on another. Java
applications are typically compiled to bytecode that can
run on any Java virtual
machine(JVM)regardlessof computer architecture.
Java is, as of 2015,one of the most
popular programming languages in use, particularly for
client-server web applications, with a reported 9
million developers. Java was originally developed
by James Gosling at Sun Microsystems(which has since
merged) and released in1995 as a core component of Sun
Microsystems platform. The language derives much of
its syntax from C and C++, but it has fewer Facilities
than either of them.

Features of Java
1. Simple

2. Object-Oriented

3. Platform Independent

4. Secured

5. Robust

6. Open product

7. Portable

8. WORA (Write once run anywhere)


MainUI.java

public class MainUI extends javax.swing.JFrame {


public MainUI() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
AboutMe.setVisible(false);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpAddUI().setVisible(true);
new EmpAddUI().getAlignmentX();
}

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


new EmpDelUI().setVisible(true);
}

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


new EmpEditUI().setVisible(true);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
AboutMe.setVisible(true);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpNavUI().setVisible(true);
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpSearchUI().setVisible(true);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpTable().setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainUI().setVisible(true);

}
});
}
// Variables declaration - do not modify
private javax.swing.JDialog AboutMe;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration

}
EmpAddUI.java
import java.sql.*;
import javax.swing.JOptionPane;
public class EmpAddUI extends javax.swing.JFrame {
public EmpAddUI() {
initComponents();
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
new MainUI().setVisible(true);
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


txtNo.setEditable(false);

// Deactivate the Save button when form loads


cmdSave.setEnabled(false);
}

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


// Activate the Save button when New button clicked
cmdSave.setEnabled(true);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null; // ResultSet for publisher table.
String SQL = "SELECT * FROM emp";
stmt = con.createStatement(); // Connection string for ResultSet - rs.
rs = stmt.executeQuery(SQL);
int pno = 1;
int PID=0;
while (rs.next()) {
PID = rs.getInt("empid");
pno++;
}
PID++;
pno = PID;
txtNo.setText(Integer.toString(pno));
txtPName.setFocusable(true);
con.close();
rs.close();
stmt.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());
e.printStackTrace();
}
}
private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int no = Integer.parseInt(txtNo.getText());
String nam = txtPName.getText();
String city = txtCity.getText();
String ph = txtPhone.getText();
char sex='Y';
if (jRadioButton1.isSelected())
sex = 'M';
else
sex='F';
int code = JOptionPane.showConfirmDialog(this, "Are you sure to add?", "Confirmation Dialog Box",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String strSQL = "INSERT INTO emp(empID, EmpName, EmpCity, EmpPhone, EmpSex) VALUES ("+(no)
+", '"+(nam)+"', '"+(city)+"', '"+(ph)+"', '"+(sex)+"')";
stmt.executeUpdate(strSQL);
JOptionPane.showMessageDialog(this, "Record added successfully into Employee table");
}
con.close();
stmt.close();
rs.close();
cmdSave.setEnabled(false);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}

}
private void cmdClearActionPerformed(java.awt.event.ActionEvent evt) {
txtNo.setText("");
txtPName.setText("");
txtCity.setText("");
txtPhone.setText("");
cmdSave.setEnabled(false);
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdClear;
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdNew;
private javax.swing.JButton cmdSave;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPName;
private javax.swing.JTextField txtPhone;
// End of variables declaration
}

EmpDelUI.java

import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
public class EmpDelUI extends javax.swing.JFrame {

public EmpDelUI() {
initComponents();
}
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";

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


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int pno = Integer.parseInt(txtNo.getText().trim());
// Steps to confirm deletion
int opt = JOptionPane.showConfirmDialog(null, "Are you sure to delete this record ?");
if (opt == JOptionPane.YES_OPTION)
{
try {
String strSQL = "Delete from emp where empId = " + (pno);
int rowsEffected = stmt.executeUpdate(strSQL);
if (rowsEffected == 0)
JOptionPane.showMessageDialog(this, "Record does not exists");
else
{
JOptionPane.showMessageDialog(this,"Record Deleted");
// Text boxes cleared
txtNo.setText("");
txtName.setText("");
txtAdd.setText("");
txtPhone.setText("");
txtNo.setEditable(true);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Unable to delete");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

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


this.setVisible(false);
//new MainUI().setVisible(true);
}
private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
// getSelectedValue() method extracts the current cursor location value into a variable
String st = (String) jList1.getSelectedValue();
// Extract the first 4 characters as roll number into a variable
String Id =st.trim().substring(0, 3);
String query = "SELECT * FROM emp WHERE empId = " + Id + ";";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection)
DriverMnager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
// Create SQL statement and execute query.
stmt = con.createStatement();
rs = stmt.executeQuery(query);
if (rs.next()) {
String MName = rs.getString("empName");
String MCity = rs.getString("empCity");
String MPh = rs.getString("EmpPhone");
String MSex = rs.getString("EmpSex");
// Displaying the contents in respective text boxes.
txtNo.setText(Id);
txtName.setText(MName);
txtAdd.setText(MCity);
txtPhone.setText(MPh);
if (MSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
txtNo.setEditable(false);
} else {
JOptionPane.showMessageDialog(null, "Record does not found in Student table");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {
txtNo.setEditable(false);
// Creating a ListModel object dModel to perform DefaultListModel
DefaultListModel dModel = (DefaultListModel) jList1.getModel();
// Method to add elements into jList1 control
dModel.clear();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
String Pno = rs.getString("empId");
String PName = rs.getString("empName");
// To make the publisher no. as 4 digit because we will extract 4 digit from list value
// in mouse click event.
if (Pno.length() < 4)
{
int x = Pno.length();
int nl = 4 - x;
while (nl > 0){
Pno = Pno + " ";
nl--;
}
}
dModel.addElement(Pno + "- " + PName);
}
jList1.setModel(dModel);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());
e.printStackTrace();
}
}

// Variables declaration - do not modify


private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdDelete;
private javax.swing.JButton cmdExit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txtAdd;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPhone;
// End of variables declaration

EmpEditUI.java
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;

public class EmpEditUI extends javax.swing.JFrame {


public EmpEditUI() {
initComponents();
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


txtNo.setEditable(false);
DefaultListModel dModel = (DefaultListModel) jList1.getModel();
// Method to add elements into jList1 control
dModel.clear();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
String Pno = rs.getString("empid");
String PName = rs.getString("empname");
// To make the publisher no. as 4 digit because we will extract 4 digit from list value
// in mouse click event.
if (Pno.length() < 4)
{
int x = Pno.length();
int nl = 4 - x;
while (nl > 0){
Pno = Pno + " ";
nl--;
}
}
dModel.addElement(Pno + "- " + PName);
}
jList1.setModel(dModel);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());
e.printStackTrace();
}
}

private void jList1MouseClicked(java.awt.event.MouseEvent evt) {


// getSelectedValue() method extracts the current cursor location value into a variable
String MID = (String) jList1.getSelectedValue();
// Extract the first 4 characters as publisher ID into a variable
String PN =MID.trim().substring(0, 3);
String query = "SELECT * FROM emp WHERE empId = " + PN + ";";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
// Create SQL statement and execute query.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
//PubN = rs.getString("pub_no");
String EName = rs.getString("empname");
String EAdd = rs.getString("empcity");
String EPh = rs.getString("empphone");
String ESex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(EName);
txtCity.setText(EAdd);
txtPh.setText(EPh);
if(ESex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
txtNo.setEditable(false);
// Close the operational object for Student
con.close();
stmt.close();
rs.close();
} else {
JOptionPane.showMessageDialog(null, "Record does not found in Employee table");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

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


this.setVisible(false);
//new MainUI().setVisible(true);
}

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


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int pno = Integer.parseInt(txtNo.getText().trim());
String EName = txtName.getText();
String ECty = txtCity.getText();
String EPh = txtPh.getText();
String PSex="M";
if(jRadioButton1.isSelected())
PSex = "M";
else
PSex="F";
String strSQL = "Update emp set empname ='"+(EName)+"',empcity = '"+(ECty)+"', empphone = '"+(EPh)
+"', empsex = '"+(PSex)+"' where empid = " + (pno);
int rowsEffected = stmt.executeUpdate(strSQL);
if (rowsEffected == 0)
JOptionPane.showMessageDialog(this, "Record does not exists");
else
JOptionPane.showMessageDialog(this,"Record modified");
con.close();
stmt.close();
rs.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

// Variables declaration - do not modify


private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtNo;

private javax.swing.JTextField txtPh;


// End of variables declaration
}

EmpNavUI.java

import java.sql.*;
import javax.swing.JOptionPane;

public class EmpNavUI extends javax.swing.JFrame {


/** Creates new form PubNavUI */
public EmpNavUI() {
initComponents();
}
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
public void disable_textfields() {
txtNo.setEditable(false);
txtName.setEditable(false);
txtAdd.setEditable(false);
txtPh.setEditable(false);
}

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


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
if (rs.first()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(false);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(true);
} else {
cmdFirst.setEnabled(false);
cmdNext.setEnabled(false);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(false);
JOptionPane.showMessageDialog(this, "Rhere is no record in table", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

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


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
if (rs.next()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(true);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(true);
cmdLast.setEnabled(true);
} else {
cmdNext.setEnabled(false);
JOptionPane.showMessageDialog(this, "You are at last record position", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

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


this.setVisible(false);
}

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


try {

if (rs.previous()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(true);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(true);
cmdLast.setEnabled(true);
} else {
cmdPrev.setEnabled(false);
JOptionPane.showMessageDialog(this, "You are at first position", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

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


try {
if (rs.last()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(true);
cmdNext.setEnabled(false);
cmdPrev.setEnabled(true);
cmdLast.setEnabled(false);
} else {
JOptionPane.showMessageDialog(this, "You are already at last record", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


disable_textfields();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
if (rs.first())
{
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(false);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(true);
}
else
{
cmdFirst.setEnabled(false);
cmdNext.setEnabled(false);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(false);
JOptionPane.showMessageDialog(this, "There is no record in table", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EmpNavUI().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdFirst;
private javax.swing.JButton cmdLast;
private javax.swing.JButton cmdNext;
private javax.swing.JButton cmdPrev;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField txtAdd;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPh;
// End of variables declaration
}

EmpSearchUI.java
import java.sql.*;
import javax.swing.JOptionPane;
public class EmpSearchUI extends javax.swing.JFrame {
public EmpSearchUI() {
initComponents();
}

Statement stmt = null;


ResultSet rs = null;
private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
String PN =TextSearch.getText().trim();
String SQL = "SELECT * FROM emp WHERE empname like '%"+(PN)+"%'";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
if (rs.next()!= rs.isAfterLast()) {
String PID = rs.getString("empID");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtPno.setText(PID);
txtPName.setText(PName);
txtPAdd.setText(PAdd);
txtPh1.setText(PPh);
if (PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
}
else
JOptionPane.showMessageDialog(null,"Search string "+PN+"..not found");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage()+"Unable to find Record");
}
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
//new MainUI().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JTextField TextSearch;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdDelete;
private javax.swing.JButton cmdExit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField txtPAdd;
private javax.swing.JTextField txtPName;
private javax.swing.JTextField txtPh1;
private javax.swing.JTextField txtPno;
// End of variables declaration
}
EmpTable.java (Display all records of Emp Table)
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class EmpTable extends javax.swing.JFrame {
/** Creates new form PubDelUI */
public EmpTable() {
initComponents();
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
//new MainUI().setVisible(true);
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
// Clear the existing table
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}
}
// SQL Query
String query = "SELECT * FROM emp";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// Iterate through the result and display on screen
while (rs.next()) {
String Mno = rs.getString("empId");
String MName = rs.getString("empname");
String MAdd = rs.getString("empcity");
String MPh1 = rs.getString("empphone");
String MDate = rs.getString("empsex");
model.addRow(new Object[] {Mno, MName, MAdd, MPh1, MDate});
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdExit;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
Bibliography
1. NetBeans IDE 7.1.2

2. Informatics Practices by Sumita Arora

3. www.icbse.com

4. www.wikipedia.org

You might also like