数据库编程

来源:互联网 发布:编程初始化是什么意思 编辑:程序博客网 时间:2024/06/06 08:50
package org.wbx.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DataBaseConnection {private static final String DRIVER = "org.gjt.mm.mysql.Driver";private static final String DB_URL = "jdbc:mysql://localhost:3306/test";private static final String DBUSER = "root";private static final String DBPASS = "********";private static Connection con = null ;static{try{Class.forName(DRIVER);}catch(ClassNotFoundException e){}}public DataBaseConnection(){try{con = DriverManager.getConnection(DB_URL,DBUSER,DBPASS);}catch(SQLException e){}}public Connection getConnection(){return con ;}public void close(){if(con!=null){try{con.close();}catch(SQLException e){}}}}
package org.wbx.vo;public class User {private String name ;private String sex ;private int age ;public void setName(String name){this.name = name ;}public void setSex(String sex){this.sex = sex ;}public void setAge(int age){this.age = age ;}public String getName(){return this.name ;}public String getSex(){return this.sex ;}public int getAge(){return this.age ;}public String toString(){return this.name + "\t" + this.sex + "\t" + this.age ;}}
package org.wbx.dao;import java.util.List;import org.wbx.vo.User;public interface IUserDao{public boolean doInsert(User user);public boolean doDelete(User user);public boolean doUpdate(User user);public List<User> doSelect(String key);}
package org.wbx.dao.proxy;import java.util.List;import org.wbx.vo.User;import org.wbx.dao.IUserDao;import org.wbx.dao.impl.UserDaoImpl;import org.wbx.jdbc.DataBaseConnection;public class UserDaoProxy implements IUserDao {private DataBaseConnection dbc = null ;private UserDaoImpl dao = null ;public UserDaoProxy(){dbc = new DataBaseConnection();dao = new UserDaoImpl(dbc.getConnection());}public boolean doInsert(User user){boolean flag = false ;try{flag = dao.doInsert(user);}catch(Exception e){}finally{this.dbc.close();}return flag ;}public boolean doDelete(User user){boolean flag = false ;try{flag = dao.doDelete(user);}catch(Exception e){}finally{this.dbc.close();}return flag ;}public boolean doUpdate(User user){boolean flag = false ;try{flag = dao.doUpdate(user);}catch(Exception e){}finally{this.dbc.close();}return flag ;}public List<User> doSelect(String key){List<User> list = null ;try{list = dao.doSelect(key);}catch(Exception e){}finally{this.dbc.close();}return list ;}}
package org.wbx.dao.impl;import java.util.List;import java.util.ArrayList;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import org.wbx.vo.User;import org.wbx.dao.IUserDao;public class UserDaoImpl implements IUserDao {private Connection con = null ;public UserDaoImpl(Connection con){this.con = con ;}public boolean doInsert(User user){boolean flag = false;String sql = "insert into user (name,sex,age) value (?,?,?)";PreparedStatement pstmt = null ;try{pstmt = con.prepareStatement(sql);pstmt.setString(1,user.getName());pstmt.setString(2,user.getSex());pstmt.setInt(3,user.getAge());if(pstmt.executeUpdate()> 0){flag = true;}}catch(Exception e){}finally{try{pstmt.close();}catch(Exception e){}}return flag;}public boolean doDelete(User user){boolean flag = false ;String sql = "delete from user where name=? && sex=? && age=?";PreparedStatement pstmt = null ;try{pstmt = this.con.prepareStatement(sql);pstmt.setString(1,user.getName());pstmt.setString(2,user.getSex());pstmt.setInt(3,user.getAge());if(pstmt.executeUpdate()>0){flag = true;}}catch(Exception e){}finally{try{pstmt.close();}catch(Exception e){}}return flag ;}public boolean doUpdate(User user){boolean flag = false ;String sql = "update user set sex=?,age=? where name=?";PreparedStatement pstmt = null ;try{pstmt = this.con.prepareStatement(sql);pstmt.setString(1,user.getSex());pstmt.setInt(2,user.getAge());pstmt.setString(3,user.getName());if(pstmt.executeUpdate()>0){flag = true;}}catch(Exception e){}finally{try{pstmt.close();}catch(Exception e){}}return flag ;}public List<User> doSelect(String key){List<User> list = new ArrayList<User>() ;String sql = "select * from user where name like ? or sex like ? or age like ?";PreparedStatement pstmt = null ;ResultSet rs = null ;try{pstmt = this.con.prepareStatement(sql);pstmt.setString(1,"%"+key+"%");pstmt.setString(2,"%"+key+"%");pstmt.setString(3,"%"+key+"%");rs = pstmt.executeQuery();while(rs.next()){User user = new User();user.setName(rs.getString(1));user.setSex(rs.getString(2));user.setAge(rs.getInt(3));list.add(user);}rs.close();}catch(Exception e){}finally{try{pstmt.close();}catch(Exception e){}}return list ;}}
package org.wbx.dao.factory;import org.wbx.dao.IUserDao;import org.wbx.dao.proxy.$UserDaoProxy;public class DaoFactory {public static IUserDao getUserDaoProxy(){return new $UserDaoProxy();}}
package org.wbx.test;import org.wbx.vo.User;import org.wbx.dao.factory.DaoFactory;public class Test {public static void main(String[] args) {for(User user:DaoFactory.getUserDaoProxy().doSelect("")){System.out.println(user);}}}

数据库创建脚本

create table user(nameVARCHAR(10)PRIMARY KEY ,sexVARCHAR(2)NOT NULL ,ageint(3)NOT NULL);


原创粉丝点击