Java ResultSet案例讲解

网友投稿 219 2022-10-05


Java ResultSet案例讲解

ResultSet

ResultSet是我们使用jdbc连接时,查询的一个返回结果集,ResultSet resultSet = stmt.executeQuery(sql),下面就使用例子介绍ResultSet的使用

例子是通过jdbc连接查account表中的数据,然后用实体类Account封装起来,返回这个类的集合。

jdbc工具类代码

package com.lingaolu.Utils;

import java.io.FileReader;

import java.io.IOException;

import java.net.URL;

import java.sql.*;

import java.util.Properties;

/**

* @author 林高禄

* @create 2020-06-23-11:12

*/

public class JdbcUtils {

private static String driver;

private static String url;

private static String userName;

private static String pw;

static{

try {

Properties p = new Properties();

ClassLoader classLoader = JdbcUtils.class.getClassLoader();

// 这个路径相对于src的路径来说

URL resource = classLoader.getResource("com/lingaolu/file/jdbc.properties");

String path = resource.getPath();

p.load(new FileReader(path));

driver = p.getProperty("driver");

url = p.getProperty("url");

userName = p.getProperty("user");

pw = p.getProperty("password");

Class.forName(driver);

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public static Connection createConnection() throws SQLException {

return DriverManager.getConnection(url, userName, pw);

}

public static void close(Statement stmt,Connection con){

if(null != stmt){

try {

stmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(null != con){

try {

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void close(ResultSet set,Statement s,Connection con){

if(null != set){

try {

set.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

close(s,con);

}

}

Account实体类代码

package com.lingaolu.jdbcConnector;

/**

* @author 林高禄

* @create 2020-06-24-8:28

*/

public class Account {

private int id;

private String name;

private double balance;

private int myAge;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

public int getMyAge() {

return myAge;

}

public void setMyAge(int myAge) {

this.myAge = myAge;

}

@Override

public String toString() {

return "Account{" +

"id=" + id +

", name='" + name + '\'' +

", balance=" + balance +

", myAge=" + myAge +

'}';

}

}

测试Demo3的代码

package com.lingaolu.jdbcConnector;

import com.lingaolu.Utils.JdbcUtils;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

/**

* @author 林高禄

* @create 2020-06-23-17:27

*/

public class Demo3 {

public static void main(String[] args) {

String sql = "select * from account";

List accounts = fineAccount(sql);

accounts.forEach(System.out::println);

System.out.println("----------------------------------");

sql = "select * from account where name='张三'";

accounts = fineAccount(sql);

accounts.forEach(System.out::println);

}

public static List fineAccount(String sql){

Connection con = null;

Statement stmt = null;

ResultSet resultSet = null;

List rerurnList = new ArrayList<>();

try {

con = JdbcUtils.createConnection();

stmt = con.createStatement();

resultSet = stmt.executeQuery(sql);

Account acc = null;

while(resultSet.next()){

// 引号里的字段要与表里的一样

int id = resultSet.getInt("id");

String name = resultSet.getString("name");

double balance = resultSet.getDouble("balance");

int age = resultSet.getInt("age");

acc = new Account();

acc.setId(id);

acc.setName(name);

acc.setBalance(balance);

acc.setMyAge(age);

rerurnList.add(acc);

}

} catch (SQLException e) {

e.printStackTrace();

}finally {

JdbcUtils.close(resultSet,stmt,con);

}

return rerurnList;

}

}

表中的数据

运行输出:

Account{id=1, name='张三', balance=500.0, myAge=17}

Account{id=2, name='李四', balance=1000.0, myAge=16}

Account{id=7, name='张三', balance=600.0, myAge=19}

Account{id=11, name='林帅', balance=20000.0, myAge=18}

----------------------------------

Account{id=1, name='张三', balance=500.0, myAge=17}

Account{id=7, name='张三', balance=600.0, myAge=19}

结果与预期的一致


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:网络安全学习笔记工具篇(五) ——使用OpenSCAP 命令行进行基线扫描与修复(网络安全课外笔记)
下一篇:同态加密实现数据隐私计算,能让你的小秘密更加秘密(动态加密解密算法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~