package cn.tedu.jdbc.day03;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import cn.tedu.jdbc.day02.DBUtils;
/*
* 结果集元数据
*
*/
public class Demo01 {
public static void main(String[] args) {
/* Connection conn = null;
try {
conn = DBUtils.getConnection();
String sql = "select * from "
+ "robin_user_zqk";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
//结果集元数据
ResultSetMetaData meta = rs.getMetaData();
int n = meta.getColumnCount();
for(int i=1;i<=n;i++) {
String name1 = meta.getColumnName(i);
System.out.print(name1);
if(i<n) {
System.out.print(",");
}
}
String name1 = meta.getColumnName(1);
String name2 = meta.getColumnName(2);
String name3= meta.getColumnName(3);
System.out.println(n+","+name1+","+name2+","+name3);
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtils.close(conn);
}*/
//封装成一个方法
String sql = "select * from "
+ "robin_user_zqk";
print(sql);
}
/*
* 重构:打印一个SQL查询结果的全部列名
* 权限修饰符+返回值类型+方法名(参数类型){方法体}
*/
public static void print(String sql) {
//...
Connection conn = null;
try {
conn = DBUtils.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData meta = rs.getMetaData();
int n = meta.getColumnCount();
for(int i=1;i<=n;i++) {
String name1 = meta.getColumnName(i);
System.out.print(name1);
if(i<n) {
System.out.print(",");
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtils.close(conn);
}
}
}
/*
* 一本书的元数据
*/
JDBC_结果集元数据
最新推荐文章于 2022-08-03 18:02:15 发布