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:
====
[Link]
----------------
package mainpkg;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainClass {
public static void main(String[] args) {
int option = 0, choice = 0;
EmpDAO empDAO = new EmpDAO();
Scanner sc = new Scanner([Link]);
do {
[Link]("Menu");
[Link]("1-Add Employee 2-Upd 3-Del 4-Search id
5-....");
option = [Link]();
switch (option) {
case 1: // add
[Link]("Enter Employee details: Name/Salary");
int eid = [Link]();
String name = [Link]();
int sal = [Link]();
Emp emp = new Emp(eid, name, sal);
// validation
if (validateEmp(emp)) {
boolean addsts = [Link](emp);
if (addsts)
[Link]("Employee details added");
else
[Link]("Employee details could not
be added");
}
else
[Link]("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: [Link]("Wrong option..!");
}
[Link]("Wish to continue? YES =11 NO=22");
choice = [Link]();
} 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([Link]().length()<2)
throw new EmpNameException("Employee name should have 2
characters min");
if([Link]() < 10000)
throw new EmpSalException("Employee should have min salary");
}catch(Exception ex) {
sts = false;
[Link]([Link]());
}
return sts;
}
}
---------------
[Link]
--------
package [Link];
public class Emp {
private int empid;
private String name;
private int salary;
public Emp() {}
public Emp(int eid, String name, int sal) {
empid = eid;
[Link] = name;
salary = sal;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
[Link] = empid;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
[Link] = salary;
}
public String toString() {
return "Employee [empid=" + empid + ", name=" + name + ", salary=" +
salary + "]";
}
}
---------------------
[Link]
---------------
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmpDAO {
public int getEmpID() {
int eid=0;
//code
try {
Connection con = [Link]();
//prepare the query statement
String qry ="select eid_seq.nextval eid from dual";
PreparedStatement ps = [Link](qry);
//execute statement
ResultSet res = [Link]();
//process result
if([Link]())
eid = [Link]("eid");
//close connection
[Link]();
}catch(Exception ex) {
[Link]([Link]());
}
return eid;
}
public boolean addEmp(Emp e) {
boolean sts=false;
//code
try {
Connection con = [Link]();
//prepare the query statement
String qry="insert into emp values(?,?,?)";
PreparedStatement ps = [Link](qry);
[Link](1,[Link]());
[Link](2,[Link]());
[Link](3,[Link]());
//execute statement
int recCount = [Link]();
//process result
if(recCount==1)
sts = true;
//close connection
[Link]();
}
catch(Exception ex) {
[Link]([Link]());
}
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;
}
}
-----------------------
[Link]
--------------
package [Link];
import [Link];
import [Link];
public class DBUtil {
public static Connection getDBConnection() {
Connection con = null;
try {
//load the driver
[Link]("[Link]");
//establish Connection
String url ="jdbc:oracle:thin:@localhost:1521:orcl";
String user="wcf20jan";
String pwd="wcf20jan";
con = [Link](url, user, pwd);
}catch(Exception ex) {
[Link]([Link]());
}
return con;
}
}
------------------------
[Link]
--------------------------
package [Link];
public class EmpNameException extends Exception {
public EmpNameException(String msg) {
super(msg);
}
}
---------------------
[Link]
-------------------------
package [Link];
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