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

Emp Form

Uploaded by

alehegn manaye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Emp Form

Uploaded by

alehegn manaye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

//import package

import java.sql.*;
public class Employee extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
/** Creates new form Employee */

public Employee() {

initComponents();

DoConnect(); // method declaration used to connect database

public void DoConnect(){

try

//STEP 2: Register JDBC driver

Driver d = new oracle.jdbc.driver.OracleDriver();

DriverManager.registerDriver( d );

System.out.println("Driver Loaded");

//SETP 3 :Open connection to database


con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hrmdb", "hrmdb"); //type 4

stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

// Step 4: Execute query

String sql="select * from emptable";

rs=stmt.executeQuery(sql);

// populate first record on the form

rs.first();

txtid.setText(rs.getString("eid"));

txtname.setText(rs.getString("ename"));

txtfname.setText(rs.getString("efname"));

txtage.setText(Integer.toString(rs.getInt("eage")));

//to display on output windows

while (rs.next()) { //start at raw 2

String id=rs.getString(1);

String name=rs.getString(2);

System.out.println(id + " "+ name);

}catch(SQLException err){

System.out.println(err.getMessage());

//For First navigation button

try {

if(con!=null)

{ rs.first();

txtid.setText(rs.getString("eid"));
txtname.setText(rs.getString("ename"));

txtfname.setText(rs.getString("efname"));

txtage.setText(Integer.toString(rs.getInt("eage")));

} catch(SQLException ex)

{ JOptionPane.showMessageDialog(null,ex.getMessage()); }

//for next navigation button

try {

if(con!=null)

{ rs.next();

txtid.setText(rs.getString("eid"));

txtname.setText(rs.getString("ename"));

txtfname.setText(rs.getString("efname"));

txtage.setText(Integer.toString(rs.getInt("eage")));

} catch(SQLException ex)

JOptionPane.showMessageDialog(null,ex.getMessage());

//for previous navigation button

try {

if(con!=null)

{ rs.previous();

txtid.setText(rs.getString("eid"));
txtname.setText(rs.getString("ename"));

txtfname.setText(rs.getString("efname"));

txtage.setText(Integer.toString(rs.getInt("eage")));

} catch(SQLException ex)

{ JOptionPane.showMessageDialog(null,ex.getMessage()); }

//For Last navigation button

try {

if(con!=null)

{ rs.last();

txtid.setText(rs.getString("eid"));

txtname.setText(rs.getString("ename"));

txtfname.setText(rs.getString("efname"));

txtage.setText(Integer.toString(rs.getInt("eage")));

} catch(SQLException ex)

JOptionPane.showMessageDialog(null,ex.getMessage());

//Save new data

try {

if(con!=null)

PreparedStatement pstmt=con.prepareStatement("insert into emptable values(?,?,?,?)");

String id1=txtid.getText();
String name=txtname.getText();

String fname=txtfname.getText();

int age= Integer.parseInt(txtage.getText());

rs.afterLast();

pstmt.setString(1, id1);

pstmt.setString(2, name);

pstmt.setString(3, fname);

pstmt.setInt(4, age);

pstmt.executeUpdate();

JOptionPane.showMessageDialog(null,"One row inserted");

} catch(SQLException ex)

JOptionPane.showMessageDialog(null,ex.getMessage());

//Delete record

String id1=txtsearch.getText();

try{

String sql = "DELETE FROM emptable WHERE eid=?";

PreparedStatement ps = con.prepareStatement(sql);

ps.setString(1, id1);
int rowsDeleted = ps.executeUpdate();
if (rowsDeleted > 0)
System.out.println("A Record was deleted successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}
//Search record by id
try{
String id1=txtsearch.getText();
String str="select * from emptable where eid='"+id1+"'";
rs= stmt.executeQuery(str);
rs.next();
txtid.setText(rs.getString("eid"));
txtname.setText(rs.getString("ename"));
txtfname.setText(rs.getString("efname"));
txtage.setText(Integer.toString(rs.getInt("eage")));
System.out.println("A Record is found successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}

//update record
try{
String id=txtid.getText();
String name=txtname.getText();
String fname=txtfname.getText();
int age= Integer.parseInt(txtage.getText());
PreparedStatement ps=null;
String str="update emptable set ename=?,efname=?,eage=? where eid=?";
ps=con.prepareStatement(str);
ps.setString(1, name);
ps.setString(2, fname);
ps.setInt(3, age);
ps.setString(4, id);
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"One row updated");
} catch (SQLException ex) {
System.out.println(ex.getMessage()); }
}// end of employee class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy