当前位置:网站首页>一天学会JDBC04:ResultSet的用法
一天学会JDBC04:ResultSet的用法
2022-08-04 06:40:00 【那人独钓寒江雪.】
package JDBCTest;
import com.sun.org.apache.bcel.internal.generic.Select;
import org.junit.Test;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCDemo5_ResultSet {
@Test public void testResultSet() throws Exception{
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url="jdbc:mysql:///test?useSSL=false";
String username="root";
String password="lyt2529165097";
Connection conn = DriverManager.getConnection(url,username,password);
//3.定义sql
String sql="select * from emp";
//4.获取statement 对象
Statement stmt = conn.createStatement();
//5.执行sql
ResultSet rs=stmt.executeQuery(sql);
//创建集合
List<Account> list=new ArrayList<>();
//6.处理结果,遍历rs中的所有数据 //6.1 光标向下移动一行,并且判断当前是否有数据 while (rs.next()){
//6.2获取数据 getXxx()
Account account=new Account();
int id=rs.getInt("id");
int workno=rs.getInt("workno");
String name=rs.getString("name");
String gender=rs.getString("gender");
int age=rs.getInt("age");
int idcard=rs.getInt("idcard");
String workaddress=rs.getString("workaddress");
Date entrydate=rs.getDate("entrydate");
account.setId(id);
account.setWorkno(workno);
account.setName(name);
account.setGender(gender);
account.setAge(age);
account.setIdcard(idcard);
account.setWorkaddress(workaddress);
account.setEntrydate(entrydate);
System.out.println("--------");
list.add(account);
}
System.out.println(list);
//7.释放资源
rs.close();
stmt.close();
conn.close();
}}
package JDBCTest;
import java.sql.Date;
public class Account {
private int id;
private int workno;
private String name;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getWorkno() {
return workno;
}
public void setWorkno(int workno) {
this.workno = workno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getIdcard() {
return idcard;
}
public void setIdcard(int idcard) {
this.idcard = idcard;
}
public String getWorkaddress() {
return workaddress;
}
public void setWorkaddress(String workaddress) {
this.workaddress = workaddress;
}
public Date getEntrydate() {
return entrydate;
}
@Override public String toString() {
return "Account{
" +
"id=" + id +
", workno=" + workno +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", idcard=" + idcard +
", workaddress='" + workaddress + '\'' +
", entrydate=" + entrydate +
'}';
}
public void setEntrydate(Date entrydate) {
this.entrydate = entrydate;
}
private int age;
private int idcard;
private String workaddress;
private Date entrydate;
}
边栏推荐
猜你喜欢
curl (7) Failed connect to localhost8080; Connection refused
用matlab打造的摩斯电码加解码器音频版,支持包括中文在内的任意字符
FCN - the originator of semantic segmentation (based on tf-Kersa reproduction code)
如何用matlab做高精度计算?【第二辑】
详解CAN总线:常用CAN连接器的使用方法
Activiti 工作流引擎 详解
Distributed Computing Experiment 3 PRC-based Book Information Management System
MATLAB版量化交易技术分析工具TA-Lib【不付费也可获取,不要被付费吓跑】
从零开始单相在线式不间断电源(UPS)(硬件)
idea使用@Autowired注解爆红原因及解决方法
随机推荐
缓存穿透、击穿、雪崩
中职网络安全竞赛C模块MS17-010批量扫描
Provide 和 Inject 的用法
JVM 快速检测死锁
开发小技巧 navicate如何点击单元格显示全部的文本内容或通过图像查看内容
mysql月份比較是否相等
LLVM编译技术应用分析
反射与枚举
学校申请链接
Promise.all 使用方法
data:image/jpg;base64格式数据转化为图片
卷积神经网络CNN
带你了解一下PHP搭建的电商商城系统
IDEA中创建编写JSP
数据特征预处理——缺失值的查看方式及处理
ThreadLocal内存泄漏问题讲解
更改mysql数据库默认的字符集(mysql 存储 emoji表情)
matlab让我的旧手机起死回生
分布式计算实验2 线程池
matlab封闭曲线拟合 (针对一些列离散点)