本文主要是介绍11 获取表中全部数据并打印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
while(resultSet.next())循环获取每条记录
每个循环中循环获取每列
通过getMetaData获取列信息,
resultSet.getXXX(第几列)
XXX为java类型,对应数据库类型,列信息中可以获取到
所以判断第几行第几列的列数据库类型,转成java类型
从而知道XXX的具体类型
从而获取到第几行第几列的数据
import java.sql.*;
import java.util.*;public class Pre {private static String url = "jdbc:mysql:///dict?useSSL=false";private static String user = "root";private static String password = "123456";private static String sql = "select * from products";private static Connection conn = null;private static PreparedStatement pre = null;private static ResultSet resultSet = null;private static ResultSetMetaData metaData = null;private static int columnCount = 0;//private static Map<String,String> maplt = null; //labelName与其对应数据库类型的映射//private static List labell = null; //labelName集合static {try {//1.注册Class.forName("com.mysql.cj.jdbc.Driver");//2连接conn = DriverManager.getConnection(url,user,password);//操作对象pre = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);resultSet = pre.executeQuery();//获取列信息,元数据metaData = resultSet.getMetaData();//总共有多少列columnCount = metaData.getColumnCount();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();}}/*private static String kv(String k){//Map根据键找值for (String key : maplt.keySet()) {if(key == k){return maplt.get(key);}}return "1";}*//*private static void labelMapType() throws Exception{//label与类型的映射maplt = new HashMap();if(resultSet.next()){for(int i = 1; i<=columnCount; i++){String columnLabel = metaData.getColumnLabel(i);String columnTypeName = metaData.getColumnTypeName(i);maplt.put(columnLabel,columnTypeName);}}resultSet.first();}*//*private static void labelList() throws Exception{//label集合labell = new ArrayList();if (resultSet.next()){for (int i = 1; i<= columnCount; i++){String columnLabel = metaData.getColumnLabel(i);labell.add(columnLabel);}}resultSet.first();}*///数据库类型与java类型对应/*Iterator entries = map.entrySet().iterator();while (entries.hasNext()) {Map.Entry entry = (Map.Entry) entries.next();System.out.println(entry.getKey() + "," +entry.getValue());}*///数据库类型与java类型对应,根据数据库类型获得java类型private static String typeorm(String label){Map<String,String> ormap = new HashMap();ormap.put("VARCHAR","String");ormap.put("DECIMAL","int");ormap.put("TEXT","String");return ormap.get(label);}public static void main(String[] args) throws Exception {//labelMapType();//labelName与其对应类型映射,知道labelName就知道其数据库类型 //maplt//labelList();//labelName的集合 //labellresultSet.previous();while (resultSet.next()){for (int i = 1; i <= columnCount;i++){/*//获取其中一个labelName,找到其对应的数据库类型,再找到其对应的java类型if (typeorm(kv((String) labell.get(i-1)))== "String"){System.out.print(resultSet.getString(i));System.out.print(" ");} else if(typeorm(kv((String) labell.get(i-1)))== "int") {System.out.print(resultSet.getInt(i));System.out.print(" ");}*/if(typeorm(metaData.getColumnTypeName(i)) == "String"){System.out.print(resultSet.getString(i));System.out.print(" ");} else if(typeorm(metaData.getColumnTypeName(i)) == "int") {System.out.print(resultSet.getInt(i));System.out.print(" ");}}System.out.println();}}
}
这篇关于11 获取表中全部数据并打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!