1.com.hanchao.util包是工具类;
- package com.hanchao.util;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
-
-
-
-
-
- public class DBHelp {
-
-
-
-
-
-
-
-
-
- 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";
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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]);
- }
-
- 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;
- }
-
-
-
-
-
-
-
- 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();
- }
- }
- }
- }
-
-
-
-
-
- public void close(PreparedStatement sta, Connection con) {
- this.close(null, sta, con);
- }
-
-
- }
2.com.hanchao.entity包是实体类User;
- package com.hanchao.entity;
-
-
-
-
-
- public class User {
-
- private int id;
- private String username;
- private String address;
-
-
- 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类;可以看到较上篇文章,写法简便多了!
- 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;
-
-
-
-
-
-
- public class UserDao {
-
-
-
-
-
-
-
- private DBHelp db = new DBHelp();
-
-
-
-
-
-
- public int insert(User user) {
- String sql = "insert t_user(username,address) values(?,?)";
- return db.executeSQL(sql, user.getUsername(),user.getAddress());
- }
-
-
-
-
-
-
- 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());
- }
-
-
-
-
-
-
- public int delete(int id) {
- String sql = "delete from t_user where id=?";
- return db.executeSQL(sql, id);
- }
-
-
-
-
-
- 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) {
-
- e.printStackTrace();
- } finally {
- db.close(rs,sta, con);
- }
- return list;
- }
-
-
-
-
-
-
- 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;
- }
-
-
- }
4.com.hanchao.test包是测试类Test;
- package com.hanchao.test;
-
- import java.util.List;
-
- import com.hanchao.dao.UserDao;
- import com.hanchao.entity.User;
-
-
-
-
-
-
- public class Test {
-
- public static void main(String[] args) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- UserDao userDao = new UserDao();
- List<User> list = userDao.retrieve();
- for(User user : list) {
- System.out.println(user.getUsername()+"\t"+user.getAddress());
- }
-
-
-
-
-
- User u = userDao.retrieveById(23);
- System.out.println(u.getClass()+"\n"+u.getAddress());
- }
- }
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/926215 ,如需转载请自行联系原作者