从数据库中获取列名以及表 [英] Fetching column names from database along with table
问题描述
我想在访问表时显示列名行。在这里我尝试了这个代码......但只显示没有列名的表。我用java eclipse和sqlite数据库
我尝试过:
table = new JTable();
table.setBounds(86,92,773,307);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
contentPane.add(table);
尝试
{
字符串查询=从查询中选择*;
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(例外e)
{
e.printStackTrace();
}
请给出解决方案
I want to display column name row while accessing table. Here I tried this code... but only table displayed without column name. I used java eclipse and sqlite database
What I have tried:
table = new JTable();
table.setBounds(86, 92, 773, 307);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
contentPane.add(table);
try
{
String query="Select * from enquiry";
PreparedStatement pst=conn.prepareStatement(query);
ResultSet rs=pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e)
{
e.printStackTrace();
}
please give solution for it
推荐答案
至少有两种方法 - 一种使用SQlite PRAGMA table_info(表名) - 因此可能如下所示: -
There are at least two methods - one using a SQlite PRAGMA table_info(table-name) - so that may look like :-
String tableName = "enquiry"; // your table name
Cursor c = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
if (c.moveToFirst()) {
do {
System.out.println("name: " + c.getString(1) + " type: " + c.getString(2));
} while (c.moveToNext());
}
c.close();
或使用ResultSetMetaData - 请参阅以下链接以获取示例使用ResultSetMetaData获取表的列名的Java代码 [ ^ ]和 Java& ; JDBC& SQLite:从用户选择的数据库表中读取数据并在JTable中显示 [ ^ ]
这篇关于从数据库中获取列名以及表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!