zoukankan      html  css  js  c++  java
  • 将JDBC ResultSet结果集转成List

    package test;
    
    import java.sql.Connection;  
    import java.sql.DriverManager;  
    import java.sql.PreparedStatement;  
    import java.sql.SQLException;
    public class DBHelper {
         public static final String url = "jdbc:mysql://127.0.0.1/peixian";  
            public static final String name = "com.mysql.jdbc.Driver";  
            public static final String user = "root";  
            public static final String password = "root";
         
            public Connection conn = null;  
            public PreparedStatement pst = null;  
         
            public  DBHelper(String sql) {  
                try {  
                    Class.forName(name);//指定连接类型  
                    conn = DriverManager.getConnection(url,user,password);//获取连接  
                    pst = conn.prepareStatement(sql);//准备执行语句  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
         
            public void close() {  
                try {  
                    this.conn.close();  
                    this.pst.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }
       
    }
    //==============执行demo
    package test;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Demo {  
        static String sql = null;  
        static DBHelper db1 = null;  
        static ResultSet ret = null;  
        
        public static void main(String[] args) {  
            sql = "select *from xy_sys_menu";//SQL语句  
            db1 = new DBHelper(sql);//创建DBHelper对象
            
            try {  
                List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
                ret = db1.pst.executeQuery();//执行语句,得到结果集  
                ResultSetMetaData md = ret.getMetaData(); //获得结果集结构信息,元数据
                int columnCount = md.getColumnCount();   //获得列数 
                while (ret.next()) {
                    Map<String,Object> rowData = new HashMap<String,Object>();
                    for (int i = 1; i <= columnCount; i++) {
                        rowData.put(md.getColumnName(i), ret.getObject(i));
                    }
                    list.add(rowData);
         
                }
                System.out.println("list============="+list);
                ret.close();  
                db1.close();//关闭连接  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }
        
    }
  • 相关阅读:
    PHP培训教程 PHP里10个鲜为人知但却非常有用的函数
    跟我学Spring Boot(一)创建Spring Boot 项目
    android 4.0 webview 无法播放视频
    vs2008 安装部署 启动项
    android 使用webview 加载网页
    hbase 无法打开60010网页
    oracle 提示没有监听
    hbase 基本命令
    hbase 单机版安装
    win7 64位远程连接oracle11g64位
  • 原文地址:https://www.cnblogs.com/xianz666/p/13547251.html
Copyright © 2011-2022 走看看