connection.getMetaData 似乎没有返回表信息
我正在使用一个简单的应用程序来帮助学习 Apache.Derby 数据库并在 Eclipse 中工作。 以下代码运行正常,但 conn.getMetaData() 不会返回任何与表相关的有意义的内容 - 例如 colnameslist.size 为 0。但是我添加了 meta.getDatabaseProductName() 来查看发生了什么,并返回“Apache.Derby”,所以我猜测有某种联系。
The connection url is "jdbc:derby:C:/Users/RonLaptop/MyDB".
The string passed into getTableContents() is "MYENERGYAPP.ENERGYTABLE7".
由于它没有错误,我有点不知所措。
package com.energy;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
/** an immutable table model built from getting
metadata about a table in a jdbc database
*/
public class JDBCTableModel extends AbstractTableModel {
Object[][] contents;
String[] columnNames;
Class[] columnClasses;
public JDBCTableModel (Connection conn,
String string)
throws SQLException {
super();
getTableContents (conn, string);
}
protected void getTableContents (Connection conn,
String string)
throws SQLException {
// get metadata: what columns exist and what
// types (classes) are they?
DatabaseMetaData meta = conn.getMetaData();
String productName = meta.getDatabaseProductName();
String[] types = null;
System.out.println ("got meta = " + meta);
ResultSet results =
meta.getColumns (null, null, string, null) ;
System.out.println ("got column results");
ArrayList colNamesList = new ArrayList();
ArrayList colClassesList = new ArrayList();
while (results.next()) {
colNamesList.add (results.getString ("COLUMN_NAME"));
System.out.println ("name: " +
results.getString ("COLUMN_NAME"));
int dbType = results.getInt ("DATA_TYPE");
switch (dbType) {
case Types.INTEGER:
colClassesList.add (Integer.class); break;
case Types.FLOAT:
colClassesList.add (Float.class); break;
case Types.DOUBLE:
case Types.REAL:
colClassesList.add (Double.class); break;
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
colClassesList.add (java.sql.Date.class); break;
default:
colClassesList.add (String.class); break;
};
System.out.println ("type: " +
results.getInt ("DATA_TYPE"));
}
columnNames = new String [colNamesList.size()];
colNamesList.toArray (columnNames);
columnClasses = new Class [colClassesList.size()];
colClassesList.toArray (columnClasses);
// get all data from table and put into
// contents array
Statement statement =
conn.createStatement ();
results = statement.executeQuery ("SELECT * FROM " +
string);
ArrayList rowList = new ArrayList();
while (results.next()) {
ArrayList cellList = new ArrayList();
for (int i = 0; i<columnClasses.length; i++) {
Object cellValue = null;
if (columnClasses[i] == String.class)
cellValue = results.getString (columnNames[i]);
else if (columnClasses[i] == Integer.class)
cellValue = new Integer (
results.getInt (columnNames[i]));
else if (columnClasses[i] == Float.class)
cellValue = new Float (
results.getInt (columnNames[i]));
else if (columnClasses[i] == Double.class)
cellValue = new Double (
results.getDouble (columnNames[i]));
else if (columnClasses[i] == java.sql.Date.class)
cellValue = results.getDate (columnNames[i]);
else
System.out.println ("Can't assign " +
columnNames[i]);
cellList.add (cellValue);
}// for
Object[] cells = cellList.toArray();
rowList.add (cells);
} // while
// finally create contents two-dim array
contents = new Object[rowList.size()] [];
for (int i=0; i<contents.length; i++)
contents[i] = (Object []) rowList.get (i);
System.out.println ("Created model with " +
contents.length + " rows");
// close stuff
results.close();
statement.close();
}
// AbstractTableModel methods
public int getRowCount() {
return contents.length;
}
public int getColumnCount() {
if (contents.length == 0)
return 0;
else
return contents[0].length;
}
public Object getValueAt (int row, int column) {
return contents [row][column];
}
// overrides methods for which AbstractTableModel
// has trivial implementations
public Class getColumnClass (int col) {
return columnClasses [col];
}
public String getColumnName (int col) {
return columnNames [col];
}
}
I am using a simple app to aid learning database with Apache.Derby and working in Eclipse.
The following code runs ok but conn.getMetaData() does not return anything meaningful regarding the table - colnameslist.size for example is 0. However I added meta.getDatabaseProductName() to see what happened and that returns 'Apache.Derby' so I guess there is some sort of connection.
The connection url is "jdbc:derby:C:/Users/RonLaptop/MyDB".
The string passed into getTableContents() is "MYENERGYAPP.ENERGYTABLE7".
As it does not error I am at a bit of a loss.
package com.energy;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
/** an immutable table model built from getting
metadata about a table in a jdbc database
*/
public class JDBCTableModel extends AbstractTableModel {
Object[][] contents;
String[] columnNames;
Class[] columnClasses;
public JDBCTableModel (Connection conn,
String string)
throws SQLException {
super();
getTableContents (conn, string);
}
protected void getTableContents (Connection conn,
String string)
throws SQLException {
// get metadata: what columns exist and what
// types (classes) are they?
DatabaseMetaData meta = conn.getMetaData();
String productName = meta.getDatabaseProductName();
String[] types = null;
System.out.println ("got meta = " + meta);
ResultSet results =
meta.getColumns (null, null, string, null) ;
System.out.println ("got column results");
ArrayList colNamesList = new ArrayList();
ArrayList colClassesList = new ArrayList();
while (results.next()) {
colNamesList.add (results.getString ("COLUMN_NAME"));
System.out.println ("name: " +
results.getString ("COLUMN_NAME"));
int dbType = results.getInt ("DATA_TYPE");
switch (dbType) {
case Types.INTEGER:
colClassesList.add (Integer.class); break;
case Types.FLOAT:
colClassesList.add (Float.class); break;
case Types.DOUBLE:
case Types.REAL:
colClassesList.add (Double.class); break;
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
colClassesList.add (java.sql.Date.class); break;
default:
colClassesList.add (String.class); break;
};
System.out.println ("type: " +
results.getInt ("DATA_TYPE"));
}
columnNames = new String [colNamesList.size()];
colNamesList.toArray (columnNames);
columnClasses = new Class [colClassesList.size()];
colClassesList.toArray (columnClasses);
// get all data from table and put into
// contents array
Statement statement =
conn.createStatement ();
results = statement.executeQuery ("SELECT * FROM " +
string);
ArrayList rowList = new ArrayList();
while (results.next()) {
ArrayList cellList = new ArrayList();
for (int i = 0; i<columnClasses.length; i++) {
Object cellValue = null;
if (columnClasses[i] == String.class)
cellValue = results.getString (columnNames[i]);
else if (columnClasses[i] == Integer.class)
cellValue = new Integer (
results.getInt (columnNames[i]));
else if (columnClasses[i] == Float.class)
cellValue = new Float (
results.getInt (columnNames[i]));
else if (columnClasses[i] == Double.class)
cellValue = new Double (
results.getDouble (columnNames[i]));
else if (columnClasses[i] == java.sql.Date.class)
cellValue = results.getDate (columnNames[i]);
else
System.out.println ("Can't assign " +
columnNames[i]);
cellList.add (cellValue);
}// for
Object[] cells = cellList.toArray();
rowList.add (cells);
} // while
// finally create contents two-dim array
contents = new Object[rowList.size()] [];
for (int i=0; i<contents.length; i++)
contents[i] = (Object []) rowList.get (i);
System.out.println ("Created model with " +
contents.length + " rows");
// close stuff
results.close();
statement.close();
}
// AbstractTableModel methods
public int getRowCount() {
return contents.length;
}
public int getColumnCount() {
if (contents.length == 0)
return 0;
else
return contents[0].length;
}
public Object getValueAt (int row, int column) {
return contents [row][column];
}
// overrides methods for which AbstractTableModel
// has trivial implementations
public Class getColumnClass (int col) {
return columnClasses [col];
}
public String getColumnName (int col) {
return columnNames [col];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试仅使用表的名称;当我这样做时,我不需要将“应用程序”级别的名称放在表名称前面(即只是 EVERYGYTABLE7)。并确保表名具有大写字母;可以创建一个混合大小写名称的表,我认为 Derby 对此很严格。
Try using just the name of the table; when I do this, I don't need to put the name of the 'app' level in front of the table name (i.e. just EVERYGYTABLE7). And make sure the table name has that capitalization; it is possible to create a table with a mixed-case name, and I think Derby is strict about that.
我只是从你的问题中不太清楚。首先,尝试您的表是否存在(忽略大小写),如下所示。
I'm just quite unclear from your question. First, try if your table exists (ignoring case) as something like the one mentioned below.
在字符串“MYENERGYAPP.ENERGYTABLE7”中,“MYENERGYAPP”是架构名称,“ENERGYTABLE7”是表名称。
看起来您正在传递整个字符串 。
尝试将架构名称传递到“schemaName”参数(第二个参数)中 getColumns()),并将表名称传递给“tableName”参数(第三个参数)
。 /docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String" rel="nofollow">http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String, java.lang.字符串、java.lang.String、java.lang.String)
In the string "MYENERGYAPP.ENERGYTABLE7", ""MYENERGYAPP" is the Schema Name, and "ENERGYTABLE7" is the Table Name.
It looks like you are passing the entire string into the "tableName" argument of the DatabaseMetaData.getColumns() call.
Try passing the Schema Name into the "schemaName" argument (the second argument to getColumns()), and pass the Table Name into the "tableName" argument (the third argument).
See http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String, java.lang.String, java.lang.String, java.lang.String)