首页 > 代码库 > 东软培训-002
东软培训-002
数据库操作
实体bean
package org.mo.model; public class UserModel { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
//查询类
package org.mo.model; public class UserQueryModel extends UserModel { private Integer age2; public Integer getAge2() { return age2; } public void setAge2(Integer age2) { this.age2 = age2; } }
//连接数据库操作类
package org.mo.uitl; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; public class ConnectionUtils { private BasicDataSource basicDataSource = new BasicDataSource(); private ConnectionUtils() { basicDataSource.setDriverClassName("org.gjt.mm.mysql.Driver"); basicDataSource .setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gb2312"); basicDataSource.setUsername("root"); basicDataSource.setPassword("root"); } public void close() throws SQLException { basicDataSource.close(); } /** * 内部类 * * @author Administrator * */ private static class SingletonHolder { //饿汉式 private static ConnectionUtils intance = new ConnectionUtils(); } public static ConnectionUtils getIntance() { return SingletonHolder.intance; } public DataSource getDataSource() { return basicDataSource; } }
//DAO
package org.mo.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 org.mo.model.UserModel; import org.mo.model.UserQueryModel; import org.mo.uitl.ConnectionUtils; public class UserJDBCDAO { public void create(UserModel userModel) { Connection connection = null; try { connection = ConnectionUtils.getIntance().getDataSource().getConnection(); final String SQL="INSERT INTO tbl_user(id, name, age) VALUES(?,?,?)"; PreparedStatement ps = connection.prepareStatement(SQL); ps.setInt(1, userModel.getId()); ps.setString(2, userModel.getName()); ps.setInt(3, userModel.getAge()); ps.execute(); ps.close(); } catch (Exception e) { }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void upate(UserModel userModel){ Connection connection = null; try { connection = ConnectionUtils.getIntance().getDataSource().getConnection(); final String SQL="UPDATE tbl_user SET name = ?, age = ? WHERE id = ?"; PreparedStatement ps = connection.prepareStatement(SQL); ps.setString(1, userModel.getName()); ps.setInt(2, userModel.getAge()); ps.setInt(3, userModel.getId()); ps.execute(); ps.close(); } catch (Exception e) { }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void delete(Integer id){ Connection connection = null; try { connection = ConnectionUtils.getIntance().getDataSource().getConnection(); final String SQL="DELETE tbl_user WHERE id = ?"; PreparedStatement ps = connection.prepareStatement(SQL); ps.setInt(1, id); ps.execute(); ps.close(); } catch (Exception e) { }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } public UserModel getSingle(Integer id){ Connection connection = null; UserModel userModel = null; try { connection = ConnectionUtils.getIntance().getDataSource().getConnection(); final String SQL="select id,name,age FROM tbl_user WHERE id = ?"; PreparedStatement ps = connection.prepareStatement(SQL); ps.setInt(1, id); ResultSet executeQuery = ps.executeQuery(); if(executeQuery.next()){ userModel = rsModel(executeQuery); } executeQuery.close(); ps.close(); } catch (Exception e) { }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return userModel; } public List<UserModel> getAll(){ Connection connection = null; List<UserModel> userModels = new ArrayList<UserModel>(); try { connection = ConnectionUtils.getIntance().getDataSource().getConnection(); final String SQL="select id,name,age FROM tbl_user"; PreparedStatement ps = connection.prepareStatement(SQL); ResultSet executeQuery = ps.executeQuery(); if(executeQuery.next()){ UserModel userModel = rsModel(executeQuery); userModels.add(userModel); } executeQuery.close(); ps.close(); } catch (Exception e) { }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return userModels; } private UserModel rsModel(ResultSet executeQuery) throws SQLException { UserModel userModel = new UserModel(); int int1 = executeQuery.getInt("id"); String string = executeQuery.getString("name"); int int2 = executeQuery.getInt("age"); userModel.setId(int1); userModel.setName(string); userModel.setAge(int2); return userModel; } private String genWhere(UserQueryModel userQuseryModel) { StringBuffer stringBuffer = new StringBuffer(); if(userQuseryModel.getId() > 0){ stringBuffer.append(" and id = ? "); } if (userQuseryModel.getName().trim().length() > 0 || userQuseryModel.getName() != null) { stringBuffer.append(" and name like ? "); } if(userQuseryModel.getAge() > 0 ){ stringBuffer.append(" and age >= ? "); } if(userQuseryModel.getAge2() > 0 ){ stringBuffer.append(" and age <= ? "); } return stringBuffer.toString(); } private void preparePs(UserQueryModel uqm, PreparedStatement ps) throws Exception { int count = 1; if (uqm.getId() > 0) { ps.setInt(count++, uqm.getId()); } if (uqm.getName() != null && uqm.getName().trim().length() > 0) { ps.setString(count++, uqm.getName()); } if (uqm.getAge() > 0) { ps.setInt(count++, uqm.getAge()); } if (uqm.getAge2() > 0) { ps.setInt(count++, uqm.getAge2()); } } public List<UserModel> getByCondition(UserQueryModel model){ Connection connection = null; List<UserModel> userModels = new ArrayList<UserModel>(); try { connection = ConnectionUtils.getIntance().getDataSource().getConnection(); final String SQL="select id,name,age FROM tbl_user WHERE 1=1 " + genWhere(model) + " ORDER BY id "; PreparedStatement ps = connection.prepareStatement(SQL); preparePs(model, ps); ResultSet executeQuery = ps.executeQuery(); if(executeQuery.next()){ UserModel userModel = rsModel(executeQuery); userModels.add(userModel); } executeQuery.close(); ps.close(); } catch (Exception e) { }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return userModels; } }
//jar包
mysql-connector-java-5.1.8-bin.jar
commons-pool.jar
commons-dbcp.jar
东软培训-002
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。