TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1.com.hanchao.util包是工具类;
- [/code]
-
- package com.hanchao.util;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- /**
- * 简化Dao写法的工具类★
- * @author hanlw
- * 2012-07-09
- */
- public class DBHelp {
-
- /**
- * 为什么要写一个工具类呢?
- *
- * 因为这样可以简化我们的Dao类的写法。更何况一个实体类对应一个Dao。
- * 如果每一个Dao都按照上面一篇文章写,那是很费事的啊!!
- */
-
- //1.下面是几个常量
- private static final String DRIVER = "com.mysql.jdbc.Driver";
- private static final String URL = "jdbc:mysql://127.0.0.1:3306/mydb";
- private static final String DB_NAME = "root";
- private static final String DB_PASSWORD = "root";
-
- /**
- * 获取数据库的连接
- * @return
- */
- public Connection getConnection() {
- Connection con = null;
-
- try {
- Class.forName(DRIVER);
- con = DriverManager.getConnection(URL,DB_NAME,DB_PASSWORD);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
-
- /**
- * 该方法用来执行insert,update,delete操作
- * @param sql SQL语句
- * @param args 不定项参数
- * @return
- */
- public int executeSQL(String sql,Object...args) {
- Connection con = getConnection();
- PreparedStatement sta = null;
- int rows = 0;
-
- try {
- sta = con.prepareStatement(sql);
-
- //★注意下面的循环语句★
- for (int i = 0; i < args.length; i++) {
- sta.setObject(i+1, args[i]); //为什么是i+1呢?因为你从前面的文章知道,那是从1开始的!
- }
-
- rows = sta.executeUpdate();
- if(rows > 0) {
- System.out.println("operate successfully !");
- } else {
- System.out.println("fail!");
- }
-
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- this.close(sta, con);
- }
- return rows;
- }
-
- /**
- * rs,sta,con的关闭
- * @param rs
- * @param sta
- * @param con
- */
- public void close(ResultSet rs, PreparedStatement sta, Connection con) {
- try {
- if(rs != null) {
- rs.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if(sta != null) {
- sta.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if(con != null) {
- con.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * sta,con的关闭方法
- * @param sta
- * @param con
- */
- public void close(PreparedStatement sta, Connection con) {
- this.close(null, sta, con);
- }
-
- //--------------------------------------
- }
-
- [b]2.com.hanchao.entity包是实体类User;[/b]
-
- [code]
复制代码
package com.hanchao.entity;
/**
* 实体类
* @author hanlw
* 2012-07-09
*/
public class User {
private int id;
private String username;
private String address;
//getter...setter...
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3.com.hanchao.dao包中是UserDao类;可以看到较上篇文章,写法简便多了!
- [/code]
-
- package com.hanchao.dao;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- import com.hanchao.entity.User;
- import com.hanchao.util.DBHelp;
-
- /**
- * 实体类对应的Dao类
- * @author hanlw
- * 2012-07-09
- */
- public class UserDao {
-
- /**
- * 说明:通过DBHelp类,我们可以简化dao中的insert,update,delete操作
- *
- * 以后有机会的话,咱们也可以简化对retrieve的操作!
- */
-
- private DBHelp db = new DBHelp();
-
- /**
- * 对t_user的插入操作
- * @param user
- * @return
- */
- public int insert(User user) {
- String sql = "insert t_user(username,address) values(?,?)";
- return db.executeSQL(sql, user.getUsername(),user.getAddress());
- }
-
- /**
- * 对t_user的更新操作
- * @param user
- * @return
- */
- public int update(User user) {
- String sql = "update t_user set username=?,address=? where id=?";
- return db.executeSQL(sql, user.getUsername(),user.getAddress(),user.getId());
- }
-
- /**
- * 对t_user的删除操作
- * @param id
- * @return
- */
- public int delete(int id) {
- String sql = "delete from t_user where id=?";
- return db.executeSQL(sql, id);
- }
-
- /**
- * 对t_user的全检索
- * @return
- */
- public List<User> retrieve() {
- List<User> list = new ArrayList<User>();
- Connection con = db.getConnection();
- PreparedStatement sta = null;
- ResultSet rs = null;
-
- try {
- String sql = "select id,username,address from t_user";
- sta = con.prepareStatement(sql);
- rs = sta.executeQuery();
-
- while(rs.next()) {
- User user = new User();
- user.setId(rs.getInt("id"));
- user.setUsername(rs.getString("username"));
- user.setAddress(rs.getString("address"));
- list.add(user);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- db.close(rs,sta, con);
- }
- return list;
- }
-
- /**
- * 跟id进行检索
- * @param id 用户的id号
- * @return
- */
- public User retrieveById(int id) {
- Connection con = db.getConnection();
- PreparedStatement sta = null;
- ResultSet rs = null;
- User user = null;
-
- try {
- String sql = "select id,username,address from t_user where id=?";
- sta = con.prepareStatement(sql);
- sta.setInt(1, id);
- rs = sta.executeQuery();
- if(rs.next()) {
- user = new User();
- user.setId(rs.getInt("id"));
- user.setUsername(rs.getString("username"));
- user.setAddress("address");
- }
-
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- db.close(rs,sta, con);
- }
- return user;
- }
-
- //-------------------------------------------
- }
-
- [b]4.com.hanchao.test包是测试类Test;[/b]
-
- [code]
复制代码
package com.hanchao.test;
import java.util.List;
import com.hanchao.dao.UserDao;
import com.hanchao.entity.User;
/**
* 测试类
* @author hanlw
* 2012-07-09
*/
public class Test {
public static void main(String[] args) {
/**
* 1.insert()
*/
/* UserDao userDao = new UserDao();
User user = new User();
user.setUsername("chenchen");
user.setAddress("yancheng");
userDao.insert(user);
*/
/**
* 2.update()
*/
/* UserDao userDao = new UserDao();
User user = new User();
user.setId(22);
user.setUsername("chen");
user.setAddress("shanghai");
userDao.update(user);
*/
/**
* 3.delete()
*/
// UserDao userDao = new UserDao();
// userDao.delete(22);
/**
* 4.retrieve()
*/
UserDao userDao = new UserDao();
List<User> list = userDao.retrieve();
for(User user : list) {
System.out.println(user.getUsername()+" "+user.getAddress());
}
/**
* 5.检索某一个用户
*/
User u = userDao.retrieveById(23);
System.out.println(u.getClass()+"
"+u.getAddress());
}
}
出处http://hanchaohan.blog.51cto.com/2996417/926215 |
|