上两篇博客讲了如何对数据库中的表内容进行增删改的操作,这一篇博客来说说如何实现对数据库的表内容进行读取查询操作。对数据库中的表内容进行查询要用到ResultSet接口(PS对于程序猿来说睡眠是良药一点也不假,今天精神倍棒,继续刷博客)。
ResultSet:当我们查询数据库中的表时,返回的结果通常是一个二维的数据表结果集,这个时候我们要遍历这个结果集,通常就要用到ResultSet接口来进行遍历,用来获取每一行的数据。ResultSet对象具有指向其当前数据行的指针。在最开始的时候指针被至于第一行之前。ResultSet接口中的next方法将指针移动到下一行,如果有下一行返回true继续进行遍历,直到没有下一行时返回false,常常用在while()循环当中。默认的ResultSet对象不可更新,仅有一个向前移动的指针。
ResultSet常用到的方法:
1:boolean next():将光标位置向后移动一行,如果有行返回true,没有行返回false。
2:String getString(int columnIndex):以Java编程语言中的String形式获取ResultSet对象当前指定的行中参数指定的列值。参数类型为int,即根据行索引号来得到指定值。
3:String getString(String columnLabel):参数类型为String,即用行标签来获取ResultSet对象当前指定的行中列的值,返回结果为String类型。
4:int getInt(int columnIndex):参数类型为int,即用行索引来获取ResultSet对象当前指定的行中列的值,返回结果为int类型。
5:int getInt(String columnLabel):同理。其他的getDouble(String columnLabel);getDouble(int columnIndex)等等都是一样的道理。不一一讲解了,想要知道更多的方法请查阅Java JDK自行了解学习,上面是一些比较常用到的方法。
例子:接着对t_employee表进行说事,今天来对t_employee表中的内容进行遍历,如果没有苦于没有数据,可以翻看前面的博客用sql语句添加或者运行JDBC程序进行添加,不推荐借助第三方软件手动添加。
package com.panli.dbutil;
/**
* 连接数据库
*/
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbUtil {
//数据库驱动名字
private static String jdbcName = "com.mysql.jdbc.Driver";
//数据库协议地址
private static String dbUrl = "jdbc:mysql://localhost:3306/db_user";
//数据库用户名
private static String dbUser = "root";
//数据库密码
private static String dbPassword = "123456";
/**
* 获取连接
* @return
* @throws Exception
*/
public static Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
}
/**
* 关闭连接
* @param stmt
* @param conn
* @throws Exception
*/
public static void close(Statement stmt,Connection conn) throws Exception{
if(stmt!=null){
stmt.close();
if(conn!=null){
conn.close();
}
}
}
/**
* 关闭连接
* @param cstmt
* @param conn
* @throws Exception
*/
public static void close(CallableStatement cstmt, Connection conn) throws Exception{
if(cstmt!=null){
cstmt.close();
if(conn!=null){
conn.close();
}
}
}
/**
* 关闭连接
* @param pstmt
* @param conn
* @throws SQLException
*/
public static void close(PreparedStatement pstmt, Connection conn) throws SQLException{
if(pstmt!=null){
pstmt.close();
if(conn!=null){
conn.close();
}
}
}
/**
* 重载关闭方法
* @param pstmt
* @param conn
* @throws Exception
*/
public void close(ResultSet rs,PreparedStatement pstmt, Connection conn) throws Exception{
if(rs!=null){
rs.close();
if(pstmt!=null){
pstmt.close();
if(conn!=null){
conn.close();
}
}
}
}
}
package com.panli.model;
import java.io.File;
/**
* model包下的employee类,对每个字段进行建模
* @author Peter
*
*/
public class Employee {
private int id;
private String userName;
private double salary;
private String job;
private int jobTypeId;
private File context;
private File pic;
/**
* 带有6个参数的构造方法
* @param userName
* @param salary
* @param job
* @param jobTypeId
* @param context
* @param pic
*/
public Employee(String userName, double salary, String job, int jobTypeId,
File context, File pic) {
super();
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
this.context = context;
this.pic = pic;
}
/**
* 具有5个参数的构造方法
* @param userName
* @param salary
* @param job
* @param jobTypeId
* @param context
*/
public Employee(String userName, double salary, String job, int jobTypeId,
File context) {
super();
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
this.context = context;
}
/**
* 默认的构造方法
*/
public Employee() {
super();
// TODO Auto-generated constructor stub
}
/**
* 带一个参数的构造方法
* @param id
*/
public Employee(int id) {
super();
this.id = id;
}
/**
* 带4个参数的构造方法
* @param userName
* @param salary
* @param job
* @param jobTypeId
*/
public Employee(String userName, double salary, String job, int jobTypeId) {
super();
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
}
/**
* 带参数的构造方法
* @param id
* @param userName
* @param salary
* @param job
* @param jobTypeId
*/
public Employee(int id, String userName, double salary, String job,
int jobTypeId) {
super();
this.id = id;
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
}
/**
* 重写toString()方法
*/
@Override
public String toString() {
return "Employee:[id=" + id + ", userName=" + userName + ", salary="
+ salary + ", job=" + job + ", jobTypeId=" + jobTypeId + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getJobTypeId() {
return jobTypeId;
}
public void setJobTypeId(int jobTypeId) {
this.jobTypeId = jobTypeId;
}
public File getContext() {
return context;
}
public void setContext(File context) {
this.context = context;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
}
package com.panli.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* 对数据库中的表内容进行查询操作的类
*/
import com.panli.dbutil.DbUtil;
import com.panli.model.Employee;
public class SelectEmployeeDao {
private static DbUtil dbUtil = new DbUtil();
/**
* 对数据表内容的查询操作 使用String getString(int columnIndex)形式
* @throws Exception
*/
public static void selectData() throws Exception{
Connection conn = dbUtil.getCon();
String sql = "select * from t_employee";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt(1);
String userName = rs.getString(2);
double salary = rs.getDouble(3);
String job = rs.getString(4);
int jobTypeId = rs.getInt(5);
System.out.println("id号:"+id+",名字:"+userName+",薪水:"+salary+",工作:"+job+",工作类型:"+jobTypeId);
}
dbUtil.close(rs, pstmt, conn);
}
/**
* 对数据表内容的查询操作 使用String getString(String columnLabel)形式,比较形象化
* 以后建议常常用这个,但是每个人企业有不同规定与要求。
* @throws Exception
*/
public static void selectData1() throws Exception{
Connection conn = dbUtil.getCon();
String sql = "select * from t_employee";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String userName = rs.getString("userName");
double salary = rs.getDouble("salary");
String job = rs.getString("job");
int jobTypeId = rs.getInt("jobTypeId");
System.out.println("id号:"+id+",名字:"+userName+",薪水:"+salary+",工作:"+job+",工作类型:"+jobTypeId);
}
dbUtil.close(rs, pstmt, conn);
}
/**
* 使用List集合对t_Employee表内容遍历输出
* @return
* @throws Exception
*/
public static List selectData2() throws Exception{
List employeeList = new ArrayList();
Connection conn = dbUtil.getCon();
String sql = "select * from t_employee";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String userName = rs.getString("userName");
double salary = rs.getDouble("salary");
String job = rs.getString("job");
int jobTypeId = rs.getInt("jobTypeId");
Employee employee = new Employee(id, userName, salary, job, jobTypeId);
employeeList.add(employee);
}
dbUtil.close(rs, pstmt, conn);
return employeeList;
}
}
package com.panli.test;
import java.util.ArrayList;
import java.util.List;
import com.panli.dao.SelectEmployeeDao;
import com.panli.model.Employee;
/**
* 测试类
* @author Peter
*
*/
public class Test2 {
private static SelectEmployeeDao selectEmployeeDao = new SelectEmployeeDao();
public static void main(String[] args) throws Exception {
//selectEmployeeDao.selectData();
//selectEmployeeDao.selectData1();
List employeeList = selectEmployeeDao.selectData2();
//使用for each遍历数组集合
for(Employee employee: employeeList){
System.out.println(employee);
}
}
}