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

Package Jdbcdemo Corrected

The document contains Java classes for managing courses, employees, and students using JDBC for database operations. Each class includes methods for creating, reading, updating, and deleting records in a database, along with constructors and getter/setter methods for object properties. The main classes include Course, Employee, and Student, with corresponding CRUD operations implemented in CourseCURD, EmployeeCURD, and StudentCURD classes.

Uploaded by

rethinakumari
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)
6 views

Package Jdbcdemo Corrected

The document contains Java classes for managing courses, employees, and students using JDBC for database operations. Each class includes methods for creating, reading, updating, and deleting records in a database, along with constructors and getter/setter methods for object properties. The main classes include Course, Employee, and Student, with corresponding CRUD operations implemented in CourseCURD, EmployeeCURD, and StudentCURD classes.

Uploaded by

rethinakumari
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/ 15

package jdbcdemo;

Course.java

--------------------------------

public class Course {

private String crid;

private String crsName;

private int duration;

private int fee;

public Course() {

super();

}
public Course(String crid, String crsName, int duration, int fee) {

super();

this.crid = crid;

this.crsName = crsName;

this.duration = duration;

this.fee = fee;

public String getCrid() {

return crid;

public void setCrid(String crid) {

this.crid = crid;

public String getCrsName() {

return crsName;

public void setCrsName(String crsName) {

this.crsName = crsName;

public int getDuration() {

return duration;

public void setDuration(int duration) {

this.duration = duration;

public int getFee() {

return fee;

}
public void setFee(int fee) {

this.fee = fee;

@Override

public String toString() {

return "Course [crid=" + crid + ", crsName=" + crsName + ", duration=" +


duration + ", fee=" + fee + "]";

courseCURD.java

----------------------------

package jdbcdemo;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Scanner;

public class CourseCURD {

public static void main(String[] args) {

CourseCURD crsCRUD = new CourseCURD();

Scanner sc = new Scanner(System.in);

System.out.println("Enter Course details: COURSE NAME/DURATION/FEE");

String crid = crsCRUD.getCourseID();


String crsname = sc.next();

int duration = sc.nextInt();

int fee = sc.nextInt();

Course c=new Course(crid,crsname,duration,fee);

//insert student data to student_tb

boolean sts = crsCRUD.insertCourse(c);

if(sts)

System.out.println("Course details inserted");

else

System.out.println("Course details could not be inserted");

private String getCourseID() {

String crid="cr";

//code

try {

//load the driver

Class.forName("oracle.jdbc.driver.OracleDriver");

//establish Connection

String url ="jdbc:oracle:thin:@localhost:1521:XE";

String user="wcf20jan";

String pwd="wcf20jan";

Connection con = DriverManager.getConnection(url, user,


pwd);

//prepare the query statement

String qry ="select crid_seq.nextval crid from dual";

PreparedStatement ps = con.prepareStatement(qry);

//execute statement

ResultSet res = ps.executeQuery();


//process result

if(res.next())

crid = crid+res.getInt("crid");

//close connection

con.close();

}catch(Exception ex) {

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

return crid;

private boolean insertCourse(Course c) {

boolean sts =false;

//code

try {

//load the driver

Class.forName("oracle.jdbc.driver.OracleDriver");

//establish Connection

String url ="jdbc:oracle:thin:@localhost:1521:XE";

String user="wcf20jan";

String pwd="wcf20jan";

Connection con = DriverManager.getConnection(url, user, pwd);

//prepare the query statement

String qry="insert into coursetb values(?,?,?,?)";

PreparedStatement ps = con.prepareStatement(qry);

ps.setString(1,c.getCrid());

ps.setString(2,c.getCrsName());

ps.setInt(3,c.getDuration());

ps.setInt(4, c.getFee());
//execute statement

int recCount = ps.executeUpdate();

//process result

if(recCount==1)

sts = true;

//close connection

con.close();

catch(Exception ex) {

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

return sts;

Employee.java

------------------------------

package jdbcdemo;

public class Employee implements Cloneable {

public int empid; // instance var

public String name; // instance var

public int salary; // instance var

public Employee() {

empid = 9999;

name = "no name";


salary = 1000;

public Employee(int empid, String name, int salary) { // a= b

this.empid = empid;

this.name = name;

this.salary = salary;

public void print() { // instance method

System.out.println(empid + " " + name + " " + salary);

public Employee getClone() // throws CloneNotSupportedException

Employee emp = null;

// code

try {

emp = (Employee) super.clone();

} catch (CloneNotSupportedException e) {

System.out.println("Clone not supported");

return emp;

@Override

public String toString() {


return "Employee [empid=" + empid + ", name=" + name + ", salary=" + salary
+ "]";

EmployeeCURD.java

-------------------------------------------

package jdbcdemo;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Scanner;

public class EmployeeCRUD {

public static void main(String[] args) {

EmployeeCRUD empCRUD = new EmployeeCRUD();

Scanner sc = new Scanner(System.in);

System.out.println("Enter Employee details: NAME/SALARY");

int eid = empCRUD.getEmpID();

String name = sc.next();

int sal = sc.nextInt();

Employee e=new Employee(eid,name,sal);

//insert student data to student_tb

boolean sts = empCRUD.insertEmp(e);


if(sts)

System.out.println("Employee details inserted");

else

System.out.println("Employee details could not be inserted");

private int getEmpID() {

int eid=0;

//code

try {

//load the driver

Class.forName("oracle.jdbc.driver.OracleDriver");

//establish Connection

String url ="jdbc:oracle:thin:@localhost:1521:xe";

String user="wcf20jan";

String pwd="wcf20jan";

Connection con = DriverManager.getConnection(url, user,


pwd);

//prepare the query statement

String qry ="select eid_seq.nextval eid from dual";

PreparedStatement ps = con.prepareStatement(qry);

//execute statement

ResultSet res = ps.executeQuery();

//process result

if(res.next())

eid = res.getInt("eid");

//close connection

con.close();

}catch(Exception ex) {
System.out.println(ex.getMessage());

return eid;

private boolean insertEmp(Employee e) {

boolean sts =false;

//code

try {

//load the driver

Class.forName("oracle.jdbc.driver.OracleDriver");

//establish Connection

String url ="jdbc:oracle:thin:@localhost:1521:xe";

String user="wcf20jan";

String pwd="wcf20jan";

Connection con = DriverManager.getConnection(url, user, pwd);

//prepare the query statement

String qry="insert into emp values(?,?,?)";

PreparedStatement ps = con.prepareStatement(qry);

ps.setInt(1,e.empid);

ps.setString(2,e.name);

ps.setInt(3,e.salary);

//execute statement

int recCount = ps.executeUpdate();

//process result

if(recCount==1)

sts = true;

//close connection

con.close();
}

catch(Exception ex) {

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

return sts;

Student.java

-------------------------------

/**

*/

package jdbcdemo;

public class Student {

private int rno;

private String name;

private int score;

public Student() {

super();

public Student(int rno, String name, int score) {

super();

this.rno = rno;
this.name = name;

this.score = score;

public int getRno() {

return rno;

public void setRno(int rno) {

this.rno = rno;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getScore() {

return score;

public void setScore(int score) {

this.score = score;

@Override

public String toString() {

return "Student [rno=" + rno + ", name=" + name + ", score=" + score + "]";

}
StudentCURD.java

-----------------------------------------

/**

*/

package jdbcdemo;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.util.Scanner;

public class StudentCRUD {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter Student details: RNO/NAME/SCORE");

int rno = sc.nextInt();

String name = sc.next();

int score = sc.nextInt();

Student s=new Student(rno,name,score);

//insert student data to student_tb

StudentCRUD stCRUD = new StudentCRUD();

boolean sts = stCRUD.insertSt(s);

if(sts)

System.out.println("student details inserted");

else

System.out.println("student details could not be inserted");

}
private boolean insertSt(Student s) {

boolean sts =false;

//code

try {

//load the driver

Class.forName("oracle.jdbc.driver.OracleDriver");

//Establish Connection

String url ="jdbc:oracle:thin:@localhost:1521:xe";

String user="wcf20jan";

String pwd="wcf20jan";

Connection con = DriverManager.getConnection(url, user, pwd);

//prepare the query

String qry="insert into student_tb values(?,?,?)";

PreparedStatement ps = con.prepareStatement(qry);

ps.setInt(1,s.getRno());

ps.setString(2,s.getName());

ps.setInt(3,s.getScore());

//Execute

int recCount = ps.executeUpdate();

//process result

if(recCount==1)

sts = true;

catch(Exception ex) {

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

return sts;
}

You might also like