Points to be noted if error occurs-
1. please make sure you import the correct files i.e. only
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
other methods will definitely interfere with the code
2.please make sure that you add the ojdbc14.jar file from the property of project
to do this follow-
-right click on the project name
-goto last option PROPERTIES
-then click on LIBRARIES and ADD JAR
-specify the path to ojdbc14.jar file usually it is -C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
3. please make sure you make no stupid spelling mistakes (you are grown up, act like one).
4. please don't bother your friends rather share the link - LINK
Happy Coding !!
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class employee_management extends javax.swing.JFrame {
/**
* Creates new form employee_management
*/
Connection conn=null;
public employee_management() throws SQLException {
initComponents(); //THIS METHOD's BODY IS OF NO USE IGNORE
//here i have inserted a method to open a connection
initdb();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
empnumber = new javax.swing.JTextField();
empname = new javax.swing.JTextField();
salary = new javax.swing.JTextField();
deptnumber = new javax.swing.JTextField();
add = new javax.swing.JButton();
show = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
displaying_area = new javax.swing.JTextArea();
clear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setForeground(new java.awt.Color(240, 240, 240));
jLabel1.setText("EMPLOYEE MANAGEMENT APP");
jLabel2.setForeground(new java.awt.Color(240, 240, 240));
jLabel2.setText("EMP NUMBER");
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("EMP NAME");
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("SALARY");
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("DEPT NUMBER");
empnumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
empnumberActionPerformed(evt);
}
});
salary.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
salaryActionPerformed(evt);
}
});
deptnumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deptnumberActionPerformed(evt);
}
});
add.setText("Add employee");
add.setToolTipText("");
add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActionPerformed(evt);
}
});
show.setText("Employee Records");
show.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showActionPerformed(evt);
}
});
displaying_area.setColumns(20);
displaying_area.setRows(5);
jScrollPane1.setViewportView(displaying_area);
clear.setText("Clear");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(87, 87, 87)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(add)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(show)
.addGap(28, 28, 28)
.addComponent(clear)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel2))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(empnumber)
.addComponent(empname)
.addComponent(deptnumber, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
.addComponent(salary, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(163, 163, 163)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(41, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(empnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(empname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(salary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(deptnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(add)
.addComponent(show)
.addComponent(clear))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void empnumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void deptnumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void salaryActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//THIS IS WHERE WE START CODING-(ADD BUTTON)
private void addActionPerformed(java.awt.event.ActionEvent evt) {
//on pressing add button
int num=0,sal=0,dept=0;
String number=empnumber.getText();
String name=empname.getText();
String sala=salary.getText();
String deptn=deptnumber.getText();
//received the text in string form
try{
num=Integer.parseInt(number);
sal=Integer.parseInt(sala);
dept=Integer.parseInt(deptn);
//checking if any of the text contains string
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter digits only","Error",JOptionPane.ERROR_MESSAGE);
}
try {
String snum,ssal,sdept;
snum=Integer.toString(num);
ssal=Integer.toString(sal);
sdept=Integer.toString(dept);
PreparedStatement ps=conn.prepareStatement("insert into emp values(?,?,?,?)");
ps.setString(1,snum);
ps.setString(2,name);
ps.setString(3,ssal);
ps.setString(4,sdept);
int ans=ps.executeUpdate();
if(ans==0)
JOptionPane.showMessageDialog(null,"SQL Exception while input","Result",JOptionPane.NO_OPTION);
else
JOptionPane.showMessageDialog(null,ans+"rows updated" ,"Result",JOptionPane.NO_OPTION);
} catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQL Exception while input"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
}
private void showActionPerformed(java.awt.event.ActionEvent evt) {
displaying_area.setText(""); //making textbox empty before clicking
//initializing the string with null
String empno=null,name=null,sal=null,deptno=null;
try {
Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=st.executeQuery("select empno,empname,sal,deptno from emp");
int count=0;
while(rs.next())
{
empno=rs.getString("empno");
name=rs.getString("empname");
sal=rs.getString("sal");
deptno=rs.getString("deptno");
count++;
displaying_area.append(empno+"\t"+name+"\t"+sal+"\t"+deptno+"\n");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"SQL Exception while fetching"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
}
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
empnumber.setText("");
empname.setText("");
salary.setText("");
deptnumber.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new employee_management().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(employee_management.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton add;
private javax.swing.JButton clear;
private javax.swing.JTextField deptnumber;
private javax.swing.JTextArea displaying_area;
private javax.swing.JTextField empname;
private javax.swing.JTextField empnumber;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField salary;
private javax.swing.JButton show;
// End of variables declaration
private void initdb() throws SQLException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"driver not loaded "+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
try{
conn=DriverManager.getConnection("jdbc:oracle:thin:@//pc-name:1521/xe","username","password");
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"SQL Exception 2"+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
}
//this function is called whenever you press the X button(top right corner) to close the frame
//here i am using this to close the connection
public void windowClosing(WindowEvent e)
{
if(conn!=null)
{
try{
conn.close();
System.out.println("Disconnected from DB successfully");
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Error while disconnecting","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
}
/////////////////////////don't copy this/////////////////////
1. please make sure you import the correct files i.e. only
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
other methods will definitely interfere with the code
2.please make sure that you add the ojdbc14.jar file from the property of project
to do this follow-
-right click on the project name
-goto last option PROPERTIES
-then click on LIBRARIES and ADD JAR
-specify the path to ojdbc14.jar file usually it is -C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
3. please make sure you make no stupid spelling mistakes (you are grown up, act like one).
4. please don't bother your friends rather share the link - LINK
Happy Coding !!
Diagram/UI of the application-
DONWLOAD THE CODE FILE-
CODE (100% tested and working)-
package employee_management_app;import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class employee_management extends javax.swing.JFrame {
/**
* Creates new form employee_management
*/
Connection conn=null;
public employee_management() throws SQLException {
initComponents(); //THIS METHOD's BODY IS OF NO USE IGNORE
//here i have inserted a method to open a connection
initdb();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
//THIS ENTIRE BODY IS INIT COMPONENTS PLEASE DO NOT PAY ATTENTION TO IT
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
empnumber = new javax.swing.JTextField();
empname = new javax.swing.JTextField();
salary = new javax.swing.JTextField();
deptnumber = new javax.swing.JTextField();
add = new javax.swing.JButton();
show = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
displaying_area = new javax.swing.JTextArea();
clear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setForeground(new java.awt.Color(240, 240, 240));
jLabel1.setText("EMPLOYEE MANAGEMENT APP");
jLabel2.setForeground(new java.awt.Color(240, 240, 240));
jLabel2.setText("EMP NUMBER");
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("EMP NAME");
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("SALARY");
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("DEPT NUMBER");
empnumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
empnumberActionPerformed(evt);
}
});
salary.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
salaryActionPerformed(evt);
}
});
deptnumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deptnumberActionPerformed(evt);
}
});
add.setText("Add employee");
add.setToolTipText("");
add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActionPerformed(evt);
}
});
show.setText("Employee Records");
show.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showActionPerformed(evt);
}
});
displaying_area.setColumns(20);
displaying_area.setRows(5);
jScrollPane1.setViewportView(displaying_area);
clear.setText("Clear");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(87, 87, 87)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(add)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(show)
.addGap(28, 28, 28)
.addComponent(clear)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel2))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(empnumber)
.addComponent(empname)
.addComponent(deptnumber, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
.addComponent(salary, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(163, 163, 163)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(41, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(empnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(empname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(salary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(deptnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(add)
.addComponent(show)
.addComponent(clear))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void empnumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void deptnumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void salaryActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//THIS IS WHERE WE START CODING-(ADD BUTTON)
private void addActionPerformed(java.awt.event.ActionEvent evt) {
//on pressing add button
int num=0,sal=0,dept=0;
String number=empnumber.getText();
String name=empname.getText();
String sala=salary.getText();
String deptn=deptnumber.getText();
//received the text in string form
try{
num=Integer.parseInt(number);
sal=Integer.parseInt(sala);
dept=Integer.parseInt(deptn);
//checking if any of the text contains string
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter digits only","Error",JOptionPane.ERROR_MESSAGE);
}
try {
String snum,ssal,sdept;
snum=Integer.toString(num);
ssal=Integer.toString(sal);
sdept=Integer.toString(dept);
PreparedStatement ps=conn.prepareStatement("insert into emp values(?,?,?,?)");
ps.setString(1,snum);
ps.setString(2,name);
ps.setString(3,ssal);
ps.setString(4,sdept);
int ans=ps.executeUpdate();
if(ans==0)
JOptionPane.showMessageDialog(null,"SQL Exception while input","Result",JOptionPane.NO_OPTION);
else
JOptionPane.showMessageDialog(null,ans+"rows updated" ,"Result",JOptionPane.NO_OPTION);
} catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQL Exception while input"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
}
private void showActionPerformed(java.awt.event.ActionEvent evt) {
displaying_area.setText(""); //making textbox empty before clicking
//initializing the string with null
String empno=null,name=null,sal=null,deptno=null;
try {
Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=st.executeQuery("select empno,empname,sal,deptno from emp");
int count=0;
while(rs.next())
{
empno=rs.getString("empno");
name=rs.getString("empname");
sal=rs.getString("sal");
deptno=rs.getString("deptno");
count++;
displaying_area.append(empno+"\t"+name+"\t"+sal+"\t"+deptno+"\n");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"SQL Exception while fetching"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
}
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
empnumber.setText("");
empname.setText("");
salary.setText("");
deptnumber.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(employee_management.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new employee_management().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(employee_management.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton add;
private javax.swing.JButton clear;
private javax.swing.JTextField deptnumber;
private javax.swing.JTextArea displaying_area;
private javax.swing.JTextField empname;
private javax.swing.JTextField empnumber;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField salary;
private javax.swing.JButton show;
// End of variables declaration
private void initdb() throws SQLException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"driver not loaded "+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
try{
conn=DriverManager.getConnection("jdbc:oracle:thin:@//pc-name:1521/xe","username","password");
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"SQL Exception 2"+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
}
//this function is called whenever you press the X button(top right corner) to close the frame
//here i am using this to close the connection
public void windowClosing(WindowEvent e)
{
if(conn!=null)
{
try{
conn.close();
System.out.println("Disconnected from DB successfully");
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Error while disconnecting","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
}
/////////////////////////don't copy this/////////////////////
No comments:
Post a Comment