java resultset是什么?让我们一起来了解一下吧!
java resultset是我们在运用jdbc进行对接的时候,查询出的一个返回结果集合。Resultset的功能就是完成了存储查询结果,但是它只能读取一次,不能做到滚动读取。
ResultSetMetaData:
我们可以应用 ResultSet.getMetaData() 方法来得到 ResultSetMetaData。通过该信息能够得到表的结构,比如说列名,列的个数,列数据类型等。
一.获取列名
ResultSetMetaData.getColumnName(m);
获取第m位的列名
二.获取列个数
ResultSetMetaData.getColumnCount();
获取列的个数
三.获得列类型
1.ResultSetMetaData.getColumnType(m);
获取第m位的列类型,对应java.sql.Types中的数据信息
2.ResultSetMetaData.getColumnTypeName(m);
获取第m位的列类型名称
实战演练,具体步骤如下:
package com.lingaolu.Utils;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
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/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);
}
}
package com.lingaolu.jdbcConnector;
import com.lingaolu.Utils.JdbcUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
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;
}
}以上就是小编今天的分享了,希望可以帮助到大家。