Java之JDBC查询结果集封装到对象

有任何问题加群讨论,还可以领取精美学习资料噢~

群号:733899823

加群时请备注CSDN~

工程目录结构如下:

Java之JDBC查询结果集封装到对象_第1张图片

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

}


 

你可能感兴趣的:(Java)