java如何获得数据库表中各字段的字段名
比如我传给rs一条SQL语句,"select*fromTableName",我想得到TableName的字段名,这样我就可以把一条结果保存到HashTable里,整个结果...
比如我传给rs一条SQL语句,"select * from TableName" ,我想得到TableName的字段名,这样我就可以把一条结果保存到HashTable里,整个结果同样可以放到HashList里,方便jsp页面上的JSTL取值,最好付上整个DB代码内容,谢谢
展开
推荐于2017-09-01 · 知道合伙人互联网行家
关注
展开全部
Java获取数据库的表中各字段的字段名,代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class TestDemo {
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://数据库IP地址:3306/数据库名称";
String user = "数据库用户名";
String pass = "数据库用户密码";
conn = DriverManager.getConnection(url, user, pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
Connection conn = getConnection();
String sql = "select * from AccessType";
PreparedStatement stmt;
try {
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData data = rs.getMetaData();
for (int i = 1; i <= data.getColumnCount(); i++) {
// 获得所有列的数目及实际列数
int columnCount = data.getColumnCount();
// 获得指定列的列名
String columnName = data.getColumnName(i);
// 获得指定列的列值
int columnType = data.getColumnType(i);
// 获得指定列的数据类型名
String columnTypeName = data.getColumnTypeName(i);
// 所在的Catalog名字
String catalogName = data.getCatalogName(i);
// 对应数据类型的类
String columnClassName = data.getColumnClassName(i);
// 在数据库中类型的最大字符个数
int columnDisplaySize = data.getColumnDisplaySize(i);
// 默认的列的标题
String columnLabel = data.getColumnLabel(i);
// 获得列的模式
String schemaName = data.getSchemaName(i);
// 某列类型的精确度(类型的长度)
int precision = data.getPrecision(i);
// 小数点后的位数
int scale = data.getScale(i);
// 获取某列对应的表名
String tableName = data.getTableName(i);
// 是否自动递增
boolean isAutoInctement = data.isAutoIncrement(i);
// 在数据库中是否为货币型
boolean isCurrency = data.isCurrency(i);
// 是否为空
int isNullable = data.isNullable(i);
// 是否为只读
boolean isReadOnly = data.isReadOnly(i);
// 能否出现在where中
boolean isSearchable = data.isSearchable(i);
System.out.println(columnCount);
System.out.println("获得列" + i + "的字段名称:" + columnName);
System.out.println("获得列" + i + "的类型,返回SqlType中的编号:"+ columnType);
System.out.println("获得列" + i + "的数据类型名:" + columnTypeName);
System.out.println("获得列" + i + "所在的Catalog名字:"+ catalogName);
System.out.println("获得列" + i + "对应数据类型的类:"+ columnClassName);
System.out.println("获得列" + i + "在数据库中类型的最大字符个数:"+ columnDisplaySize);
System.out.println("获得列" + i + "的默认的列的标题:" + columnLabel);
System.out.println("获得列" + i + "的模式:" + schemaName);
System.out.println("获得列" + i + "类型的精确度(类型的长度):" + precision);
System.out.println("获得列" + i + "小数点后的位数:" + scale);
System.out.println("获得列" + i + "对应的表名:" + tableName);
System.out.println("获得列" + i + "是否自动递增:" + isAutoInctement);
System.out.println("获得列" + i + "在数据库中是否为货币型:" + isCurrency);
System.out.println("获得列" + i + "是否为空:" + isNullable);
System.out.println("获得列" + i + "是否为只读:" + isReadOnly);
System.out.println("获得列" + i + "能否出现在where中:"+ isSearchable);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2013-04-11
展开全部
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();
}}
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();
}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考如下例子,利用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);
}
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class TestDemo {
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/struts2Demo";
String user = "root";
String pass = "ncs-cyber_tj";
conn = DriverManager.getConnection(url, user, pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
Connection conn = getConnection();
String sql = "select * from AccessType";
PreparedStatement stmt;
try {
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData data = rs.getMetaData();
for (int i = 1; i <= data.getColumnCount(); i++) {
// 获得所有列的数目及实际列数
int columnCount = data.getColumnCount();
// 获得指定列的列名
String columnName = data.getColumnName(i);
// 获得指定列的列值
int columnType = data.getColumnType(i);
// 获得指定列的数据类型名
String columnTypeName = data.getColumnTypeName(i);
// 所在的Catalog名字
String catalogName = data.getCatalogName(i);
// 对应数据类型的类
String columnClassName = data.getColumnClassName(i);
// 在数据库中类型的最大字符个数
int columnDisplaySize = data.getColumnDisplaySize(i);
// 默认的列的标题
String columnLabel = data.getColumnLabel(i);
// 获得列的模式
String schemaName = data.getSchemaName(i);
// 某列类型的精确度(类型的长度)
int precision = data.getPrecision(i);
// 小数点后的位数
int scale = data.getScale(i);
// 获取某列对应的表名
String tableName = data.getTableName(i);
// 是否自动递增
boolean isAutoInctement = data.isAutoIncrement(i);
// 在数据库中是否为货币型
boolean isCurrency = data.isCurrency(i);
// 是否为空
int isNullable = data.isNullable(i);
// 是否为只读
boolean isReadOnly = data.isReadOnly(i);
// 能否出现在where中
boolean isSearchable = data.isSearchable(i);
System.out.println(columnCount);
System.out.println("获得列" + i + "的字段名称:" + columnName);
System.out.println("获得列" + i + "的类型,返回SqlType中的编号:"
+ columnType);
System.out.println("获得列" + i + "的数据类型名:" + columnTypeName);
System.out.println("获得列" + i + "所在的Catalog名字:"
+ catalogName);
System.out.println("获得列" + i + "对应数据类型的类:"
+ columnClassName);
System.out.println("获得列" + i + "在数据库中类型的最大字符个数:"
+ columnDisplaySize);
System.out.println("获得列" + i + "的默认的列的标题:" + columnLabel);
System.out.println("获得列" + i + "的模式:" + schemaName);
System.out
.println("获得列" + i + "类型的精确度(类型的长度):" + precision);
System.out.println("获得列" + i + "小数点后的位数:" + scale);
System.out.println("获得列" + i + "对应的表名:" + tableName);
System.out.println("获得列" + i + "是否自动递增:" + isAutoInctement);
System.out.println("获得列" + i + "在数据库中是否为货币型:" + isCurrency);
System.out.println("获得列" + i + "是否为空:" + isNullable);
System.out.println("获得列" + i + "是否为只读:" + isReadOnly);
System.out.println("获得列" + i + "能否出现在where中:"
+ isSearchable);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class TestDemo {
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/struts2Demo";
String user = "root";
String pass = "ncs-cyber_tj";
conn = DriverManager.getConnection(url, user, pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
Connection conn = getConnection();
String sql = "select * from AccessType";
PreparedStatement stmt;
try {
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData data = rs.getMetaData();
for (int i = 1; i <= data.getColumnCount(); i++) {
// 获得所有列的数目及实际列数
int columnCount = data.getColumnCount();
// 获得指定列的列名
String columnName = data.getColumnName(i);
// 获得指定列的列值
int columnType = data.getColumnType(i);
// 获得指定列的数据类型名
String columnTypeName = data.getColumnTypeName(i);
// 所在的Catalog名字
String catalogName = data.getCatalogName(i);
// 对应数据类型的类
String columnClassName = data.getColumnClassName(i);
// 在数据库中类型的最大字符个数
int columnDisplaySize = data.getColumnDisplaySize(i);
// 默认的列的标题
String columnLabel = data.getColumnLabel(i);
// 获得列的模式
String schemaName = data.getSchemaName(i);
// 某列类型的精确度(类型的长度)
int precision = data.getPrecision(i);
// 小数点后的位数
int scale = data.getScale(i);
// 获取某列对应的表名
String tableName = data.getTableName(i);
// 是否自动递增
boolean isAutoInctement = data.isAutoIncrement(i);
// 在数据库中是否为货币型
boolean isCurrency = data.isCurrency(i);
// 是否为空
int isNullable = data.isNullable(i);
// 是否为只读
boolean isReadOnly = data.isReadOnly(i);
// 能否出现在where中
boolean isSearchable = data.isSearchable(i);
System.out.println(columnCount);
System.out.println("获得列" + i + "的字段名称:" + columnName);
System.out.println("获得列" + i + "的类型,返回SqlType中的编号:"
+ columnType);
System.out.println("获得列" + i + "的数据类型名:" + columnTypeName);
System.out.println("获得列" + i + "所在的Catalog名字:"
+ catalogName);
System.out.println("获得列" + i + "对应数据类型的类:"
+ columnClassName);
System.out.println("获得列" + i + "在数据库中类型的最大字符个数:"
+ columnDisplaySize);
System.out.println("获得列" + i + "的默认的列的标题:" + columnLabel);
System.out.println("获得列" + i + "的模式:" + schemaName);
System.out
.println("获得列" + i + "类型的精确度(类型的长度):" + precision);
System.out.println("获得列" + i + "小数点后的位数:" + scale);
System.out.println("获得列" + i + "对应的表名:" + tableName);
System.out.println("获得列" + i + "是否自动递增:" + isAutoInctement);
System.out.println("获得列" + i + "在数据库中是否为货币型:" + isCurrency);
System.out.println("获得列" + i + "是否为空:" + isNullable);
System.out.println("获得列" + i + "是否为只读:" + isReadOnly);
System.out.println("获得列" + i + "能否出现在where中:"
+ isSearchable);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询