You are on page 1of 7

by Mr.

RAGHU SATHYA TECHNOLOGIES, HYD

JDBC One Module DAL Format

ConnUtil.java
package com.app.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnUtil {

private static Connection con=null;

static {
1|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD

//it is used to create only one connection


obj
String
driver="oracle.jdbc.driver.OracleDriver";
String
url="jdbc:oracle:thin:@localhost:1522:ORCL";
String user="system";
String password="admin";
try {
Class.forName(driver);
con=DriverManager.getConnection(url,
user, password);
}catch(Exception e) {
e.printStackTrace();
}
}
// to read connection outside of this class
// use this method
public static Connection getConn() {
return con;
}
}

IStudentDao.java
package com.app.dao;

import java.sql.ResultSet;

//It is called as POJI- provide all abstract methods


//to be performed over One Module : Student

2|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD

public interface IStudentDao {

public int saveStudent(int sid,String


sname,double sfee);
public int deleteStudent(int sid);
public ResultSet getAllStudents();
//public int updateStudent(String sname,double
sfee,int sid);
}

StudentDaoImpl.java
package com.app.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.app.dao.IStudentDao;
import com.app.util.ConnUtil;

/*
* Provide all impl logics to this methods
* using ConnUtil and write SQL queries
*/
public class StudentDaoImpl implements IStudentDao{

public int saveStudent(int sid,String


sname,double sfee) {
String sql="insert into student
values(?,?,?)";
PreparedStatement pstmt=null;
3|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD

int count=0;
try {
//prepare statement

pstmt=ConnUtil.getConn().prepareStatement(sql);

//set data
pstmt.setInt(1, sid);
pstmt.setString(2, sname);
pstmt.setDouble(3, sfee);

//execute SQL
count=pstmt.executeUpdate();

} catch (Exception e) {
e.printStackTrace();
}finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}

public int deleteStudent(int sid) {


String sql="delete from student where sid=?";
PreparedStatement pstmt=null;
int count=0;
try {

4|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD

//prepare statement
pstmt =
ConnUtil.getConn().prepareStatement(sql);

//set data
pstmt.setInt(1, sid);

//execute
count= pstmt.executeUpdate();

} catch (Exception e) {
e.printStackTrace();
}finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}

public ResultSet getAllStudents() {


String sql="select * from student";
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
//prepare stmt

pstmt=ConnUtil.getConn().prepareStatement(sql);
//execute
rs=pstmt.executeQuery();
} catch (Exception e) {

5|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD

e.printStackTrace();
}

return rs;
}
}

Test.java:-
package com.app.test;

import java.sql.ResultSet;

import com.app.dao.IStudentDao;
import com.app.dao.impl.StudentDaoImpl;

/*
* It is only test class, used to test operations
* either working fine or any problem found
*/
public class Test {

public static void main(String[] args) throws


Exception {
IStudentDao dao=new StudentDaoImpl();
/*
//1 st operation
int count = dao.saveStudent(6, "VIJAY",
5.6);
*/
//2nd operation
/*int count=dao.deleteStudent(5);

if(count!=0)

6|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD

System.out.println("Success");
else
System.out.println("fail");*/

//3rd operation
ResultSet rs=dao.getAllStudents();

while (rs.next()) {

System.out.println(rs.getInt(1)+","+rs.getString(
2)+","+rs.getDouble(3));
}

}
}

FB:

https://www.facebook.com/groups/thejavatemple/

7|P ag e

You might also like