EMS CODING
Day8:
-----
JDBC..
create table CourseTB (
CRSID varchar(5) primary key,
CRSNAME varchar(30) not null,
DURATION number check(duration >=1)
FEE number check(fee > 10000)
)
CourseTB
| CRSID | CRSNAME | DURATION | FEE |
-----------------------------------------------------------------
---------
cr1 Java 3
30000
cr2 Oracle 1
15000
crerate sequence crid_seq start with 1 increment by 1;
CRID:
"CR" + crid_seq
user input
validate input
user defined exception
invoke service
design pattern:
MVC
Model
business logic
data/content/entity
util
dbconn
excecption
dao - data access object
crud
View
presentation layer
Controller
validate
EMS:
-----
emp
add/update/del/search/viewall
db: oracle
mainpkg
MainClass
service
bean POJO
Emp
util
DBUtil
…
dao
CRUD add/update/del/search/viewall
-----
EMS:
====
MainClass.java
----------------
package mainpkg;
import java.util.Scanner;
import com.wipro.service.bean.Emp;
import com.wipro.service.dao.EmpDAO;
import com.wipro.service.util.EmpNameException;
import com.wipro.service.util.EmpSalException;
public class MainClass {
public static void main(String[] args) {
int option = 0, choice = 0;
EmpDAO empDAO = new EmpDAO();
Scanner sc = new Scanner(System.in);
do {
System.out.println("Menu");
System.out.println("1-Add Employee 2-Upd 3-Del 4-Search id
5-....");
option = sc.nextInt();
switch (option) {
case 1: // add
System.out.println("Enter Employee details: Name/Salary");
int eid = empDAO.getEmpID();
String name = sc.next();
int sal = sc.nextInt();
Emp emp = new Emp(eid, name, sal);
// validation
if (validateEmp(emp)) {
boolean addsts = empDAO.addEmp(emp);
if (addsts)
System.out.println("Employee details added");
else
System.out.println("Employee details could not
be added");
}
else
System.out.println("Please enter valid details of
Employee");
break;
case 2: // upd
break;
case 3: // del
break;
case 4: // search id
break;
case 5: // view all
break;
default: System.out.println("Wrong option..!");
}
System.out.println("Wish to continue? YES =11 NO=22");
choice = sc.nextInt();
} while (choice == 11);
}
public static boolean validateEmp(Emp e) //throws EmpNameException
{
boolean sts = true;
// code
try {
if(e == null)
throw new NullPointerException("Employee is null/invalid");
if(e.getName().length()<2)
throw new EmpNameException("Employee name should have 2
characters min");
if(e.getSalary() < 10000)
throw new EmpSalException("Employee should have min salary");
}catch(Exception ex) {
sts = false;
System.out.println(ex.getMessage());
}
return sts;
}
}
---------------
Emp.java
--------
package com.wipro.service.bean;
public class Emp {
private int empid;
private String name;
private int salary;
public Emp() {}
public Emp(int eid, String name, int sal) {
empid = eid;
this.name = name;
salary = sal;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString() {
return "Employee [empid=" + empid + ", name=" + name + ", salary=" +
salary + "]";
}
}
---------------------
EmpDAO.java
---------------
package com.wipro.service.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.wipro.service.bean.Emp;
import com.wipro.service.util.DBUtil;
public class EmpDAO {
public int getEmpID() {
int eid=0;
//code
try {
Connection con = DBUtil.getDBConnection();
//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;
}
public boolean addEmp(Emp e) {
boolean sts=false;
//code
try {
Connection con = DBUtil.getDBConnection();
//prepare the query statement
String qry="insert into emp values(?,?,?)";
PreparedStatement ps = con.prepareStatement(qry);
ps.setInt(1,e.getEmpid());
ps.setString(2,e.getName());
ps.setInt(3,e.getSalary());
//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;
}
public boolean updEmp(Emp e) {
boolean sts=false;
//code
return sts;
}
public boolean delEmp(int eid) {
boolean sts=false;
//code
return sts;
}
public Emp searchEmp(int eid) {
Emp emp=null;
//code
return emp;
}
public ArrayList<Emp> getAllEmp() {
ArrayList<Emp> eList=null;
//code
return eList;
}
}
-----------------------
DBUtil.java
--------------
package com.wipro.service.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
public static Connection getDBConnection() {
Connection con = null;
try {
//load the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//establish Connection
String url ="jdbc:oracle:thin:@localhost:1521:orcl";
String user="wcf20jan";
String pwd="wcf20jan";
con = DriverManager.getConnection(url, user, pwd);
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
return con;
}
}
------------------------
EmpNameException.java
--------------------------
package com.wipro.service.util;
public class EmpNameException extends Exception {
public EmpNameException(String msg) {
super(msg);
}
}
---------------------
EmpSalException.java
-------------------------
package com.wipro.service.util;
public class EmpSalException extends Exception {
public EmpSalException() {}
public EmpSalException(String msg) {
super(msg);
}
}
***************************
**********************
Tomorrow:
CPC doubt clearing
HTML/CSS/JavaScript - self
Servlet/JSP
has context menu
has context menu