chenjinlong

5月7日收获(DAO)

                                                                                                     DAO程序设计
         在用DAO设计模式编写程序时,根据以下几个步骤:
              1、定义接口;
              2、定义底层JavaBean;
              3、数据库连接;
              4、实现接口;
              5、工厂;
              6、主函数;
根据你的项目,在不同的包里面写相应的代码!
          下面是一个简单的例子:
         
1、定义接口: 

package com.qhit.s2.t13.dao;

import java.util.List;

import com.qhit.s2.t13.javaben.User;

public interface IUserDAO {
 
 public List<User> findAll();

}
2、JavaBean:

package com.qhit.s2.t13.javaben;

public class User {
 
 private Integer id;
 private String username;
 private String password;
 public User() {
  super();
 }
 public User(Integer id, String username, String password) {
  super();
  this.id = id;
  this.username = username;
  this.password = password;
 }
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }

}

3、数据库连接:

package com.qhit.s2.t13.dao.impl;

import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
 
 private java.sql.Connection conn = null;
 
// public final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// public final static String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=Company";
// public final static String DBUSER = "sa";
// public final static String DBPASS = "root";
 
 public java.sql.Connection getConnection() {
  try {
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   conn = DriverManager.getConnection(
     "jdbc:sqlserver://localhost:1433;DataBaseName=Company",
     "sa", "root");
   return conn;
  } catch(Exception e) {
   e.printStackTrace();
  }
  return null;
 }
 
 
 public void closeConnection() {
  if(conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

}

4、实现接口:

package com.qhit.s2.t13.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qhit.s2.t13.dao.IUserDAO;
import com.qhit.s2.t13.javaben.User;

public class UserDAOImpl implements IUserDAO {

 public List<User> findAll() {
  
  List<User> userList = new ArrayList<User>();
  
  String sql = "select * from users";
  DBConnection db = new DBConnection();
  try {
   
//   System.out.println(db.getConnection());
   
   PreparedStatement ps = db.getConnection().prepareStatement(sql);
   ResultSet rs = ps.executeQuery();
   if(rs != null) {
    while(rs.next()) {
     User user = new User();
     user.setId(rs.getInt("id"));
     user.setUsername(rs.getString("userName"));
     user.setPassword(rs.getString("password"));
     
     userList.add(user);
    }
   }
   
   
   rs.close();
   ps.close();
   db.closeConnection();
   
   return userList;
   
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   
   e.printStackTrace();
   return null;
  }
  
  
 }

}

5、工厂

package com.qhit.s2.t13.dao.impl;

import com.qhit.s2.t13.dao.IUserDAO;

public class DAOFactory {
 
 public static IUserDAO getUserDAO() {
  return new UserDAOImpl();
 }

}

6、主函数:

package main;

import java.util.List;

import com.qhit.s2.t13.dao.impl.DAOFactory;
import com.qhit.s2.t13.javaben.User;

public class TestMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List<User> list = DAOFactory.getUserDAO().findAll();
  
  if(list != null) {
   System.out.println("chenggong");
  } else {
   System.out.println("shipai");
  }
  
 }

}


 



 


posted on 2010-05-07 17:48 chenjinlong 阅读(320) 评论(1)  编辑  收藏

Feedback

# re: 5月7日收获(DAO) 2010-05-26 14:30 Rabbit

思路是清晰的。  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜