请问使用java如何获得数据库表中各字段的字段名?

比如我传给rs一条SQL语句,"select * from TableName" ,我想得到TableName的字段名,这样我就可以把一条结果保存到HashTable里,整个结果同样可以放到HashList里,方便jsp页面上的JSTL取值,最好付上整个DB代码内容,谢谢

月关宝盒
浏览 671回答 2
2回答

慕姐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();}}

MMMHUHU

参考如下例子,利用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");// 获取 metadataResultSetMetaData 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");// 获取 metadataResultSetMetaData 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);}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
MySQL