目录结构
1.在domain包中创建User实体类
package com. rick. domain;
import java. util. Date;
public class User {
private int id;
private String name;
private Date birthday;
private float money;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Date getBirthday ( ) {
return birthday;
}
public void setBirthday ( Date birthday) {
this . birthday = birthday;
}
public float getMoney ( ) {
return money;
}
public void setMoney ( float money) {
this . money = money;
}
}
2.在Dao包中创建User的实现接口
package com. rick. dao;
import com. rick. domain. User;
public interface UserDao {
public void addUser ( User user) ;
public User getUser ( int userId) ;
public User findUser ( String loginName, String password) ;
public void update ( User user) ;
public void delete ( User user) ;
}
3.新建一个utils包创建工具类JdbcUtils
package com. rick. utlis;
import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. ResultSet;
import java. sql. SQLException;
import java. sql. Statement;
public final class JdbcUtils {
private static String url = "jdbc:mysql://localhost:3306/jdbc" ;
private static String user = "root" ;
private static String password = "" ;
private JdbcUtils ( ) {
}
static {
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
} catch ( ClassNotFoundException e) {
throw new ExceptionInInitializerError ( e) ;
}
}
public static Connection getConnection ( ) throws SQLException {
return DriverManager. getConnection ( url, user, password) ;
}
public static void free ( ResultSet rs, Statement st, Connection conn) {
try {
if ( rs != null)
rs. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( st != null)
st. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} finally {
if ( conn != null)
try {
conn. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
4.在impl中创建接口实现类UserDaoJdbcImpl
package com. rick. dao. impl;
import java. sql. Connection;
import java. sql. PreparedStatement;
import java. sql. ResultSet;
import java. sql. SQLException;
import java. sql. Statement;
import com. rick. dao. UserDao;
import com. rick. domain. User;
import com. rick. utlis. JdbcUtils;
public class UserDaoJdbcImpl implements UserDao {
public void addUser ( User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils. getConnection ( ) ;
String sql = "insert into user(name,birthday, money) values (?,?,?) " ;
ps = conn. prepareStatement ( sql) ;
ps. setString ( 1 , user. getName ( ) ) ;
ps. setDate ( 2 , new java. sql. Date ( user. getBirthday ( ) . getTime ( ) ) ) ;
ps. setFloat ( 3 , user. getMoney ( ) ) ;
ps. executeUpdate ( ) ;
} catch ( SQLException e) {
throw new DaoException ( e. getMessage ( ) , e) ;
} finally {
JdbcUtils. free ( rs, ps, conn) ;
}
}
public void delete ( User user) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils. getConnection ( ) ;
st = conn. createStatement ( ) ;
String sql = "delete from user where id=" + user. getId ( ) ;
st. executeUpdate ( sql) ;
} catch ( SQLException e) {
throw new DaoException ( e. getMessage ( ) , e) ;
} finally {
JdbcUtils. free ( rs, st, conn) ;
}
}
public User findUser ( String loginName, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JdbcUtils. getConnection ( ) ;
String sql = "select id, name, money, birthday from user where name=?" ;
ps = conn. prepareStatement ( sql) ;
ps. setString ( 1 , loginName) ;
rs = ps. executeQuery ( ) ;
while ( rs. next ( ) ) {
user = mappingUser ( rs) ;
}
} catch ( SQLException e) {
throw new DaoException ( e. getMessage ( ) , e) ;
} finally {
JdbcUtils. free ( rs, ps, conn) ;
}
return user;
}
public User getUser ( int userId) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JdbcUtils. getConnection ( ) ;
String sql = "select id, name, money, birthday from user where id=?" ;
ps = conn. prepareStatement ( sql) ;
ps. setInt ( 1 , userId) ;
rs = ps. executeQuery ( ) ;
while ( rs. next ( ) ) {
user = mappingUser ( rs) ;
}
} catch ( SQLException e) {
throw new DaoException ( e. getMessage ( ) , e) ;
} finally {
JdbcUtils. free ( rs, ps, conn) ;
}
return user;
}
private User mappingUser ( ResultSet rs) throws SQLException {
User user = new User ( ) ;
user. setId ( rs. getInt ( "id" ) ) ;
user. setName ( rs. getString ( "name" ) ) ;
user. setMoney ( rs. getFloat ( "money" ) ) ;
user. setBirthday ( rs. getDate ( "birthday" ) ) ;
return user;
}
public void update ( User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils. getConnection ( ) ;
String sql = "update user set name=?, birthday=?, money=? where id=? " ;
ps = conn. prepareStatement ( sql) ;
ps. setString ( 1 , user. getName ( ) ) ;
ps. setDate ( 2 , new java. sql. Date ( user. getBirthday ( ) . getTime ( ) ) ) ;
ps. setFloat ( 3 , user. getMoney ( ) ) ;
ps. setInt ( 4 , user. getId ( ) ) ;
ps. executeUpdate ( ) ;
} catch ( SQLException e) {
throw new DaoException ( e. getMessage ( ) , e) ;
} finally {
JdbcUtils. free ( rs, ps, conn) ;
}
}
}
5.在dao中创建一个捕获异常类DaoException继承RuntimeException
package com. rick. dao;
public class DaoException extends RuntimeException {
public DaoException ( ) {
}
public DaoException ( String message) {
super ( message) ;
}
public DaoException ( Throwable cause) {
super ( cause) ;
}
public DaoException ( String message, Throwable cause) {
super ( message, cause) ;
}
public DaoException ( String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super ( message, cause, enableSuppression, writableStackTrace) ;
}
}
6.在业务逻辑层service包中创建UserService(模拟,此项目中无用)
package com. rick. service;
import com. rick. dao. UserDao;
public class UserService {
private UserDao userDao;
public void regist ( com. rick. domain. User user) {
this . userDao. addUser ( user) ;
}
}
7.创建daoconfig.properties文件
8.在Dao中创建工厂类DaoFactory
package com. rick. dao;
import java. io. InputStream;
import java. util. Properties;
public class DaoFactory {
private static UserDao userDao = null;
private static DaoFactory instance = new DaoFactory ( ) ;
private DaoFactory ( ) {
try {
Properties prop = new Properties ( ) ;
InputStream inStream = DaoFactory. class . getClassLoader ( )
. getResourceAsStream ( "daoconfig.properties" ) ;
prop. load ( inStream) ;
String userDaoClass = prop. getProperty ( "userDaoClass" ) ;
Class clazz = Class. forName ( userDaoClass) ;
userDao = ( UserDao) clazz. newInstance ( ) ;
} catch ( Throwable e) {
throw new ExceptionInInitializerError ( e) ;
}
}
public static DaoFactory getInstance ( ) {
return instance;
}
public UserDao getUserDao ( ) {
return userDao;
}
}
9.测试
package com. rick. dao;
import java. util. Date;
import com. rick. domain. User;
public class UserDaoTest {
public static void main ( String[ ] args) {
UserDao userDao = DaoFactory. getInstance ( ) . getUserDao ( ) ;
User user = new User ( ) ;
user. setBirthday ( new Date ( ) ) ;
user. setName ( "dao name1" ) ;
user. setMoney ( 1000.0f ) ;
userDao. addUser ( user) ;
}
}