1. 程式人生 > >Java之JDBC查詢結果集封裝到物件

Java之JDBC查詢結果集封裝到物件

工程目錄結構如下:


User類:

package com.xiao.pojo;

/**
 * @author 笑笑
 * @Date   2018年3月15日上午9:20:28
 *	
 */
public class User {
	
	private int uid = 0;
	private String username = null;
	private String password = null;
	private int age = 0;
	private String gender = null;
	private String email = null;
	
	//空參構造器
	public User() {
	}
	//有參構造器
	public User(int uid, String username, String password, int age, String gender, String email) {
		this.uid = uid;
		this.username = username;
		this.password = password;
		this.age = age;
		this.gender = gender;
		this.email = email;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	//重寫toString
	@Override
	public String toString() {
		return "User [uid=" + uid + ", username=" + username + ", password=" + password + ", age=" + age + ", gender="
				+ gender + ", email=" + email + "]";
	}

	
}

封裝測試類-JDBC_02.java:

package com.xiao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

import com.xiao.pojo.User;
import com.xiao.utils.JDBCUtils_01;

/**
 * @author 笑笑
 * @Date   2018年3月15日下午1:15:20
 *	
 */
public class JDBC_02 {
	
	//把結果集封裝到物件
	@Test
	public void test() throws Exception{
		//使用工具類註冊並獲取連線
		Connection cn = JDBCUtils_01.getConnection();
		//書寫SQL語句
		String sql = "select * from user_tb";
		//獲取SQL語句預編譯物件
		PreparedStatement ps = cn.prepareStatement(sql);
		//執行SQL語句
		ResultSet rs = ps.executeQuery();
		//建立物件集合
		List<User> list = new ArrayList<>();
		//處理結果集
		while(rs.next()){
			/*方式一:獲取到的每列資料,使用setXXX()方法手動封裝到User物件中
			User u = new User();
			u.setUid(rs.getInt("uid"));
			u.setUsername(rs.getString("username"));
			u.setPassword(rs.getString("password"));
			u.setAge(rs.getInt("age"));
			u.setGender(rs.getString("gender"));
			u.setEmail(rs.getString("email"));*/
			//方式二:用有參構造器封裝
			User u = new User(
					   rs.getInt("uid"),rs.getString("username"),
					   rs.getString("password"),rs.getInt("age"),
					   rs.getString("gender"),rs.getString("email")
					);
			//將封裝的物件放入到集合中
			list.add(u);
		}
		//增強for遍歷物件集合
		for (User user : list) {
			System.out.println(user);
		}
		//釋放資源
		JDBCUtils_01.relase(cn, ps, rs);
	}
	
}

工具類-JDBCUtils_01.java:

package com.xiao.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 * @author 笑笑
 * @Date   2018年3月15日上午10:55:16
 *	
 */
public class JDBCUtils_01 {
	//註冊並獲取資料庫連線
	public static Connection getConnection() throws Exception{
		//1.註冊驅動
		Class.forName("com.mysql.jdbc.Driver");
		//2.宣告資料庫連線資訊,獲取資料庫連線
		String url = "jdbc:mysql://localhost:3306/all_db";
		String username = "root";
		String password = "root";
		Connection cn =  DriverManager.getConnection(url, username, password);
		return cn;
	}
	//釋放資源
	public static void relase(Connection cn,Statement st,ResultSet rs) throws Exception{
		if(rs!=null){									    
			rs.close();
		}
		if(st!=null){
			st.close();
		}
		if(cn!=null){
			cn.close();
		}			
	}

}