使用JDBC實現(xiàn)數(shù)據(jù)訪問對象層(DAO)代碼示例
JAVA是面向對象的語言,開發(fā)者在操作數(shù)據(jù)的時候,通常更習慣面對一個特定類型的對象,如一個用戶就是一個User類的對象。DAO層需要做的,就是為上層提供充分的對象支持,讓上層再也看不到具體的數(shù)據(jù),而是一個個活生生的對象。
增加,刪除,查詢和修改操作是DAO需要做的最基本的4項操作。查詢一般需要提供遍歷查詢和id查詢,對于遍歷查詢,DAO需要提供User泛型的list對象,對于id查詢則提供已經(jīng)裝配好數(shù)據(jù)的User對象,至于增加和修改操作,上層一般會提供一個User對象,DAO把User對象中的數(shù)據(jù)使用Insert語句插入到表格中。刪除操作則只需提供一個id即可
class User{
private long id;
private String name;
private String gender;
public User(){
super();
}
public User(long id,String name,String gender){
super();
this.id = id;
this.name = name;
this.gender = gender;
}
//get,set方法
}
//DAO類
public class jdbcDao{
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
e.printStackTrace();
}
}
private Connection getConn(){
try{
return DriverManager.getConnection("jdbc:mysql://localhost:3306:xe","root","password");
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
private void release(ResultSet rs,Statement ps,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(ps!=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//用ID獲取用戶對象
public User getUserById(long id){
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
String sql = "select * from user where id = ?";
try{
conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.setLong(1,id);
rs = ps.executeQuery();
if(rs.next()){
//如果存在,則直接構建并返回用戶對象
User user = new User(rs.getLong("id"),rs.getString("name"),rs.getString("gender"));
return user;
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.release(rs,ps,conn);
}
return null;
}
//查詢所有用戶
public List<User> getAllUsers(){
List<User> list = new ArrayList<User>();
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
String sql = "select * from user ";
try{
conn = this.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//循環(huán)添加用戶對象
while(rs.next()){
User user = new User(rs.getLong("id"),rs.getString("name"),rs.getString("gender"));
list.add(user);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.release(rs,ps,conn);
}
return list;
}
//修改用戶數(shù)據(jù)
public User updateUser(User user){
PreparedStatement ps = null;
Connection conn = null;
String sql = "update user set id =?,name=?,gender=?";
try{
conn = this.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setLong(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getGender());
int rst = ps.executeUpdate();
if(rst>0){
return new User(user.getId(),user.getName(),user.getGender());
}
conn.commit();
}catch(Exception e){
e.printStackTrace();
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
}finally{
this.release(null,ps,conn);
}
return null;
}
}
//刪除用戶數(shù)據(jù)
public boolean deleteUser(long id){
PreparedStatement ps = null;
Connection conn = null;
String sql = "delete from user where id =?;
try{
conn = this.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setLong(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getGender());
int rst = ps.executeUpdate();
if(rst>0){
return user;
}
conn.commit();
}catch(Exception e){
e.printStackTrace();
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
}finally{
this.release(null,ps,conn);
}
return null;
}
}
//插入用戶數(shù)據(jù)
public User insertUser(User user){
PreparedStatement ps = null;
Connection conn = null;
String sql = "insert into user values(?,?,?)";
try{
conn = this.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setLong(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getGender());
int rst = ps.executeUpdate();
if(rst>0){
return user;
}
conn.commit();
}catch(Exception e){
e.printStackTrace();
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
}finally{
this.release(null,ps,conn);
}
return null;
}
}
}
}
總結
以上就是本文關于使用JDBC實現(xiàn)數(shù)據(jù)訪問對象層(DAO)代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱:JDBC常用接口總結、BaseJDBC和CRUDDAO的寫法實例代碼、JDBC中resutset接口操作實例詳解等,如有不足之處,歡迎留言指出,小編會及時回復大家并改正。感謝朋友們對腳本之家的支持!
相關文章
使用IDEA創(chuàng)建servlet?JavaWeb?應用及使用Tomcat本地部署的實現(xiàn)
本文主要介紹了使用IDEA創(chuàng)建servlet?JavaWeb?應用及使用Tomcat本地部署2022-01-01
Java 數(shù)據(jù)結構與算法系列精講之字符串暴力匹配
字符串暴力匹配算法是指在一個長字符串中暴力尋找是否包含某一子串所謂暴力匹配,就是不使用任何其他算法,將兩個字符串中的字符一一進行比對2022-02-02
Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解
本文主要介紹了Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04

