// Interface Class package com. syxy. dao; import com. syxy. domain. user; // The dao interface for manipulating the database public interface UserDao {// Add a User public void addUser (user User); // get a User public user getUser (int userId ); // find a User public User findUser (String loginName); // update the user data public void update (User user User); // delete a User public void delete (user User user );} // interface implementation class package com. syxy. dao. impl; import java. SQL. connection; import java. SQL. preparedStatemen T; import java. SQL. resultSet; import java. SQL. SQLException; import com. syxy. dao. userDao; import com. syxy. domain. user; import com. syxy. utils. jdbcUtils; public class UserDaoJdbcImpl implements UserDao {@ Override public void addUser (User user User) {// TODO Auto-generated method stub Connection conn = null; PreparedStatement ps = null; resultSet rs = null; try {conn = jdbcUtils. getConnection (); String SQL = "Insert into user (name, birthday, monery) 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.exe cuteUpdate ();} catch (Exception e) {e. printStackTrace ();} finally {jdbcUtils. free (rs, ps, conn) ;}@ Override public User getUser (int userId) {Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; User use R = 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.exe cuteQuery (); while (rs. next () {user = MappingUser (rs) ;}} catch (Exception e) {e. printStackTrace ();} finally {jdbcUtils. free (rs, ps, conn);} return user;} @ Override public User findUser (String loginName) {Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; user user = null; try {conn = jdbcUtils. getConn Ection (); String SQL = "select id, money, birthday from user where name =? "; Ps = conn. prepareStatement (SQL); ps. setString (1, loginName); rs = ps.exe cuteQuery (); while (rs. next () {user = MappingUser (rs) ;}} catch (Exception e) {e. printStackTrace ();} finally {jdbcUtils. free (rs, ps, conn);} return user;} private User MappingUser (ResultSet rs) throws SQLException {User 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 ;}@ Override 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.exe cuteUpdate ();} catch (Exception e) {e. printStackTrace ();} finally {jdbcUtils. free (rs, ps, conn) ;}@ Override public void delete (User user) {Connection conn = null; PreparedStatement ps = null; Res UltSet rs = null; try {conn = jdbcUtils. getConnection (); String SQL = "delete from user where id =? "; Ps = conn. prepareStatement (SQL); ps. setInt (1, user. getId (); ps.exe cuteUpdate ();} catch (Exception e) {e. printStackTrace ();} finally {jdbcUtils. free (rs, ps, conn) ;}}// domain user class package com. syxy. 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) {th Is. 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 ;}// dao factory class (easy to reuse) package com. syxy. dao; import java. io. file; import jav A. io. fileInputStream; 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 = new FileInputStream (new File ("src/daoconfig. properties "); prop. load (inStream); String userDaoClass = prop. getProp Erty ("userDaoClass"); userDao = (UserDao) Class. forName (userDaoClass ). newInstance ();} catch (Exception e) {// TODO: handle exception} public static DaoFactory getInstance () {return instance;} public UserDao getUserDao () {return userDao ;}// operation database tool class package com. syxy. utils; 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 = "lxtalx"; private jdbcUtils () {} static {try {Class. forName ("com. mysql. jdbc. driver ");} catch (ClassNotFoundException e) {throw new ExceptionInInitializerError (e) ;}} public static Connection getConnection () throw S SQLException {return DriverManager. getConnection (url, user, password);} public static void free (ResultSet rs, Statement st, Connection conn) {try {if (rs! = Null) rs. close () ;}catch (Exception e) {e. printStackTrace () ;}finally {try {if (st! = Null) st. close ();} catch (Exception e2) {e2.printStackTrace ();} finally {try {conn. close () ;}catch (Exception e3) {e3.printStackTrace ();}}}}}