by Mr.
RAGHU SATHYA TECHNOLOGIES, HYD
JDBC One Module DAL Format
[Link]
package [Link];
import [Link];
import [Link];
public class ConnUtil {
private static Connection con=null;
static {
1|P ag e
by [Link] SATHYA TECHNOLOGIES, HYD
//it is used to create only one connection
obj
String
driver="[Link]";
String
url="jdbc:oracle:thin:@localhost:1522:ORCL";
String user="system";
String password="admin";
try {
[Link](driver);
con=[Link](url,
user, password);
}catch(Exception e) {
[Link]();
}
}
// to read connection outside of this class
// use this method
public static Connection getConn() {
return con;
}
}
[Link]
package [Link];
import [Link];
//It is called as POJI- provide all abstract methods
//to be performed over One Module : Student
2|P ag e
by [Link] 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);
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/*
* 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 [Link] SATHYA TECHNOLOGIES, HYD
int count=0;
try {
//prepare statement
pstmt=[Link]().prepareStatement(sql);
//set data
[Link](1, sid);
[Link](2, sname);
[Link](3, sfee);
//execute SQL
count=[Link]();
} catch (Exception e) {
[Link]();
}finally {
try {
[Link]();
} catch (SQLException e) {
[Link]();
}
}
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 [Link] SATHYA TECHNOLOGIES, HYD
//prepare statement
pstmt =
[Link]().prepareStatement(sql);
//set data
[Link](1, sid);
//execute
count= [Link]();
} catch (Exception e) {
[Link]();
}finally {
try {
[Link]();
} catch (SQLException e) {
[Link]();
}
}
return count;
}
public ResultSet getAllStudents() {
String sql="select * from student";
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
//prepare stmt
pstmt=[Link]().prepareStatement(sql);
//execute
rs=[Link]();
} catch (Exception e) {
5|P ag e
by [Link] SATHYA TECHNOLOGIES, HYD
[Link]();
}
return rs;
}
}
[Link]:-
package [Link];
import [Link];
import [Link];
import [Link];
/*
* 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 = [Link](6, "VIJAY",
5.6);
*/
//2nd operation
/*int count=[Link](5);
if(count!=0)
6|P ag e
by [Link] SATHYA TECHNOLOGIES, HYD
[Link]("Success");
else
[Link]("fail");*/
//3rd operation
ResultSet rs=[Link]();
while ([Link]()) {
[Link]([Link](1)+","+[Link](
2)+","+[Link](3));
}
}
}
FB:
[Link]
7|P ag e