Hashtable映射数据库字段

  1 package com.test;
  2 
  3 import java.sql.*;
  4 import java.util.ArrayList;
  5 import java.util.Hashtable;
  6 import java.util.List;
  7 public class Test{
  8     // 驱动
  9     String driver = "com.mysql.jdbc.Driver";
 10     // 数据库连接
 11     String url = "jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true";
 12     // 用户名
 13     String user = "root";
 14     // 数据库密码
 15     String password = "hotlon";
 16     // 加载驱动
 17     
 18     List<Hashtable<String, Object>> rows ;
 19     
 20     public List<Hashtable<String, Object>> queSql(){
 21         
 22         try {
 23             Class.forName(driver);
 24         } catch (ClassNotFoundException e) {
 25             // TODO Auto-generated catch block
 26             e.printStackTrace();
 27         }
 28         // 获取链接
 29         Connection connection = null;
 30         try {
 31             connection = DriverManager.getConnection(url, user, password);
 32         } catch (SQLException e) {
 33             // TODO Auto-generated catch block
 34             e.printStackTrace();
 35         }
 36         // 创建查询声明
 37         PreparedStatement preparedStatement = null;
 38         try {
 39             preparedStatement = connection.prepareStatement("select * from renyuan");
 40         } catch (SQLException e) {
 41             // TODO Auto-generated catch block
 42             e.printStackTrace();
 43         }
 44         // 获取结果
 45         ResultSet resultSet = null;
 46         try {
 47             resultSet = preparedStatement.executeQuery();
 48         } catch (SQLException e) {
 49             // TODO Auto-generated catch block
 50             e.printStackTrace();
 51         }
 52         // 获取各个列的信息
 53         ResultSetMetaData metaData = null;
 54         try {
 55             metaData = resultSet.getMetaData();
 56         } catch (SQLException e) {
 57             // TODO Auto-generated catch block
 58             e.printStackTrace();
 59         }
 60         // 一条数据保存在一个Hashtable 将所有Hashtable存放到list
 61         rows = new ArrayList<Hashtable<String, Object>>();
 62         try {
 63             while (resultSet.next()) {
 64                 Hashtable<String, Object> row = new Hashtable<String, Object>();
 65                 for (int i = 1; i <= metaData.getColumnCount(); i++) {
 66                     // 打印列名
 67                     //System.out.println(metaData.getColumnName(i));
 68                     //System.out.println(row.get(metaData.getColumnName(i)));
 69                     row.put(metaData.getColumnName(i), resultSet.getObject(i)==null?"":resultSet.getObject(i));
 70                     //System.out.println(row.get(metaData.getColumnName(i)));
 71                     }
 72                 rows.add(row);
 73                 }
 74         } catch (SQLException e) {
 75             // TODO Auto-generated catch block
 76             e.printStackTrace();
 77         }
 78         try {
 79             resultSet.close();
 80         } catch (SQLException e) {
 81             // TODO Auto-generated catch block
 82             e.printStackTrace();
 83         }
 84         try {
 85             preparedStatement.close();
 86         } catch (SQLException e) {
 87             // TODO Auto-generated catch block
 88             e.printStackTrace();
 89         }
 90         try {
 91             connection.close();
 92         } catch (SQLException e) {
 93             // TODO Auto-generated catch block
 94             e.printStackTrace();
 95         }
 96         
 97         
 98         //System.out.println(rows.get(1).get("uid"));
 99         return rows;
100 
101     }
102     
103     
104     
105     public static void main(String[] args) throws Exception {
106         
107         Test test = new Test();
108         System.out.println(test.queSql().get(1).get("uid"));
109         
110         
111         }
112     }

Hashtable映射数据库字段,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。