2 回答
TA贡献1813条经验 获得超2个赞
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存放到list
List<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();
}}
TA贡献1834条经验 获得超8个赞
参考如下例子,利用jdbc获取到结果集后,利用ResultSetMetaData的方法就可以取到字段名了!
package com.javacodegeeks.snippets.core;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class RetrieveColumnNamesExample {
public static void main(String[] args) {
Connection connection = null;
try {
// 加载jdbc驱动
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
//创建连接
String serverName = "localhost";
String schema = "test";
String url = "jdbc:mysql://" + serverName + "/" + schema;
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
System.out.println("Successfully Connected to the database!");
} catch (ClassNotFoundException e) {
System.out.println("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
System.out.println("Could not connect to the database " + e.getMessage());
}
try {
// 创建结果集
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM test_table");
// 获取 metadata
ResultSetMetaData metadata = results.getMetaData();
// 从 metadata中获取列的个数
int columnCount = metadata.getColumnCount();
System.out.println("test_table columns : ");
//循环获取列的名字并打印
for (int i=1; i<=columnCount; i++) {
String columnName = metadata.getColumnName(i);
System.out.println(columnName);
}
} catch (SQLException e) {
System.out.println("Could not retrieve database metadata " + e.getMessage());
}
}
}
其中以下的重点代码片段:
ResultSet results = statement.executeQuery("SELECT * FROM test_table");
// 获取 metadata
ResultSetMetaData metadata = results.getMetaData();
// 从 metadata中获取列的个数
int columnCount = metadata.getColumnCount();
System.out.println("test_table columns : ");
//循环获取列的名字并打印
for (int i=1; i<=columnCount; i++) {
String columnName = metadata.getColumnName(i);
System.out.println(columnName);
}
添加回答
举报