0% found this document useful (0 votes)
58 views6 pages

JDBC Programs

Uploaded by

Sripathi Ravi
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)
58 views6 pages

JDBC Programs

Uploaded by

Sripathi Ravi
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

//Program Insert data into emp table scott/tiger

/* Database User Name: scott


Password : tiger
Table Name : emp
Table Structure
Column Name Data Type Nullable Default Primary Key
EMPNO NUMBER(4,0) No - -
ENAME VARCHAR2(200) Yes - -
JOB VARCHAR2(9) Yes - -
MGR NUMBER(4,0) Yes - -
HIREDATE DATE Yes - -
SAL NUMBER(7,2) Yes - -
COMM NUMBER(7,2) Yes - -
DEPTNO NUMBER(2,0) Yes - -

CREATE TABLE EMP(


EMPNO NUMBER(4,0) NOT NULL ENABLE,
ENAME VARCHAR2(200),
JOB VARCHAR2(9),
MGR NUMBER(4,0),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2,0)
)
*/
//Program Insert data into emp table scott/tiger
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class JDBCInsert {
public static void main(String[] args) {
try {
// Load the Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Define and Establish Connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott",
"tiger");
// Create Statement object
Statement st = con.createStatement();
int empno = 0, mgrno = 0, deptno = 0;
String ename, job, hiredate;
double sal, comm;

Scanner console = new Scanner(System.in);


System.out.print("Enter Employee No. ");
empno = Integer.parseInt(console.nextLine());

System.out.print("Enter Employee Name ");


ename = console.nextLine();

System.out.print("Enter Job ");


job = console.nextLine();

System.out.print("Enter Manager No. ");


mgrno = console.nextInt();

System.out.print("Enter Employee HireDate (dd-MON-YY)");


hiredate = console.next();

System.out.print("Enter Employee Salary ");


sal = console.nextDouble();

System.out.print("Enter Employee Commission ");


comm = console.nextDouble();

System.out.print("Enter Employee Departement No. ");


deptno = console.nextInt();

System.out.println("Employee No. " + empno);


System.out.println("Employee Name " + ename);
System.out.println("Employee Job " + job);
System.out.println("Manager No. " + mgrno);
System.out.println("Hiredate " + hiredate);
System.out.println("Salary " + sal);
System.out.println("Commission " + comm);
System.out.println("Employee Department No. " + deptno);
String cmd = "INSERT INTO emp values(" + empno + ",'" + ename +
"','" + job + "'," + mgrno + ",'" + hiredate + "'," + sal + "," + comm +
"," + deptno + ")";
System.out.println(cmd);
int result = st.executeUpdate(cmd);
if (result == 1) {
System.out.println("Record is Successfully Added");
}
st.close();
con.close();
} catch (Exception ex) {
System.out.println("Error : " + ex.getMessage());
ex.printStackTrace();
}
}
}

/*
Output:
E:\WT\JDBC-Programs>java JDBCInsert
Enter Employee No. 7900
Enter Employee Name Venkatesh Prasad
Enter Job MANAGER
Enter Manager No. 7349
Enter Employee HireDate (dd-MON-YY)12-FEB-23
Enter Employee Salary 6000.00
Enter Employee Commission 300
Enter Employee Departement No. 20
Employee No. 7900
Employee Name Venkatesh Prasad
Employee Job MANAGER
Manager No. 7349
Hiredate 12-FEB-23
Salary 6000.0
Commission 300.0
Employee Department No. 20
INSERT INTO emp values(7900,'Venkatesh Prasad','MANAGER',7349,'12-FEB-
23',6000.0,300.0,20)
Record is Successfully Added

*/
/*
Program retrieves data from employee table
Database User Name: hr
Password : hr
Table Name : EMPLOYEE

This Program retrieves employee_id, first_name, salary from employee table


and displays it
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCRetrieve {


public static void main(String[] args) {
try {
// Load the Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Define and Establish Connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr",
"hr");
// Create Statement object
Statement st = con.createStatement();
// Create ResultSet and execute the Query
ResultSet rst = st.executeQuery("SELECT EMPLOYEE_id, FIRST_NAME, SALARY
FROM EMPLOYEES");
int empno;
double sal;
String ename;
System.out.println("EMPNO \t EMP NAME\t\tSAL ");
System.out.println("----- \t --------\t\t--- ");
// Process the Result
while (rst.next()) {
empno = rst.getInt("EMPLOYEE_ID");
ename = rst.getString(2);
sal = rst.getDouble(3);
System.out.println(empno+"\t"+ename+"\t\t\t"+sal);
}
// Close the Connection
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
/*
Database User Name: hr
Password : hr
Table Name : EMPLOYEE

This Program increases the Salary by 30% and updates in sal column of
employee with empno = 102 from emp table

*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCUpdate {
public static void main(String[] args) {
try {
// Load the Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Define and Establish Connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hr",
"hr");
// Create Statement object
Statement st = con.createStatement();

// Create Update string


String cmd = "UPDATE EMPLOYEES ";
cmd += " SET SALARY = (SALARY + (SALARY*30)/100) ";
cmd += "WHERE employee_id=102";
// Execute the Update Statement
int count = st.executeUpdate(cmd);
if (count == 1) {
System.out.println("Updation is successful");
} else {
System.out.println("Updation is unsuccessful");
}
con.close();
} catch (Exception ex) {
System.out.println("Error " + ex);
}
}
}
/*
Program Delete data from Database

Database User Name: hr


Password : hr
Table Name : EMPLOYEE

This Program deletes the Employee details with given employee number
from employee table
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class JDBCDelete {
public static void main(String[] args) {
try {
// Load the Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Define and Establish Connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hr",
"hr");
// Create Statement object
Statement st = con.createStatement();
Scanner console = new Scanner(System.in);
System.out.print("Enter Employee No. ");
int empno = console.nextInt();
// Create Delete SQL Statement
String cmd = "DELETE FROM EMPLOYEES ";
cmd += "WHERE employee_id="+empno;
// Execute the Update Statement
int count = st.executeUpdate(cmd);

if (count == 1) {
System.out.println("Deletion of Employee with ID "+empno+"
is successful");
} else {
System.out.println("Deletion of Employee with ID "+empno+"
is unsuccessful");
}
} catch (Exception ex) {
System.out.println("Error " + ex);
}
}
}

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