東軟トレーニング-002

8028 ワード

データベース操作
エンティティbean
package org.mo.model;

public class UserModel {
	
	private Integer id;
	
	private String name;
	
	private Integer age;
	
	private String sex;

	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;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}

//クエリークラス
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, sex) VALUES(?,?,?,?)";
			PreparedStatement ps = connection.prepareStatement(SQL);
			ps.setInt(1, userModel.getId());
			ps.setString(2, userModel.getName());
			ps.setInt(3, userModel.getAge());
			ps.setString(4, userModel.getSex());
			ps.execute();
			ps.close();
		} catch (Exception e) {
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void update(UserModel userModel){
		Connection connection = null;
		try {
			connection = ConnectionUtils.getIntance().getDataSource().getConnection();
			final String SQL="UPDATE tbl_user SET name = ?, age = ?, sex = ? WHERE id = ?";
			PreparedStatement ps = connection.prepareStatement(SQL);
			ps.setString(1, userModel.getName());
			ps.setInt(2, userModel.getAge());
			ps.setString(3, userModel.getSex());
			ps.setInt(4, 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, sex 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, sex 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");
		String string2 = executeQuery.getString("sex");
		userModel.setId(int1);
		userModel.setName(string);
		userModel.setAge(int2);
		userModel.setSex(string2);
		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.getSex().trim().length() > 0
				|| userQuseryModel.getSex() != null) {
			stringBuffer.append(" and sex = ? ");
		}
		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.getSex() != null && uqm.getSex().trim().length() > 0) {
			ps.setString(count++, uqm.getSex());
		}
		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, sex 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