Professional Documents
Culture Documents
JDBC Project Model Code - PDF Filename UTF-8''JDBC Project Model Code
JDBC Project Model Code - PDF Filename UTF-8''JDBC Project Model Code
ConnUtil.java
package com.app.util;
import java.sql.Connection;
import java.sql.DriverManager;
static {
1|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD
IStudentDao.java
package com.app.dao;
import java.sql.ResultSet;
2|P ag e
by Mr.RAGHU SATHYA TECHNOLOGIES, HYD
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{
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;
}
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;
}
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 {
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