慕姐8265434
import java.sql.*;import java.util.ArrayList;import java.util.Hashtable;import java.util.List;public class Test{ public static void main(String[] args) throws Exception {// 驱动String driver = "com.mysql.jdbc.Driver";// 数据库连接String url = "jdbc:mysql://localhost:3306/test";// 用户名String user = "root";// 数据库密码String password = "admin";// 加载驱动Class.forName(driver);// 获取链接Connection connection = DriverManager.getConnection(url, user, password);// 创建查询声明PreparedStatement preparedStatement = connection.prepareStatement("select * from dept");// 获取结果ResultSet resultSet = preparedStatement.executeQuery();// 获取各个列的信息ResultSetMetaData metaData = resultSet.getMetaData();// 一条数据保存在一个Hashtable 将所有Hashtable存放到listList<Hashtable<String, Object>> rows = new ArrayList<Hashtable<String, Object>>();while (resultSet.next()) {Hashtable<String, Object> row = new Hashtable<String, Object>();for (int i = 1; i <= metaData.getColumnCount(); i++) {// 打印列名System.out.println(metaData.getColumnName(i));row.put(metaData.getColumnName(i), resultSet.getObject(i));}rows.add(row);}resultSet.close();preparedStatement.close();connection.close();}}