我用getTables从数据库里取表的名字,总是有问题?敬请高手赐教!!!谢谢!

ruby123 2002-02-08 11:28:28
con = DriverManager.getConnection(url);
dma =con.getMetaData();
String[] types = new String[1];
types[0] = "TABLES";
results = dma.getTables(null, null, "%", types);

...全文
449 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruby123 2002-03-12
  • 打赏
  • 举报
回复
haichuang(不戒和尚)给了我正确答案,我在此谢谢他!!!
ResultSet tables = metaData.getTables( "person", null,null, table );
中间的两个参数要用:null,null.
haichuang 2002-03-05
  • 打赏
  • 举报
回复
我用的就是SQL SERVER 2000呀,执行完全正确!能将你的代码贴出来吗,我可以在我这里测试一下。或给我发信:hcx-yz@sohu.com
wolfsquare 2002-03-05
  • 打赏
  • 举报
回复
有可能是驱动问题...我用的是inet的驱动.
ruby123 2002-03-05
  • 打赏
  • 举报
回复
我用的是SQL SERVER 2000,既然7.0都可以,那2000更没有问题了,我只是需要这条语句成功:
ResultSet rs=conn.getMetaData().getTables(dbName,null,null,new String[]{"TABLE"});
但是rs返回总是null。
要取表的名字是有很多方法,我只是想为什么这个方法不行,我查过了getTables这文档,说有的数据库可能不支持,但我想SQL SERVER 2000这最起码的应该支持。为什么 ?

ruby123 2002-03-04
  • 打赏
  • 举报
回复
其实,
results = dma.getTables(null, null, "", nill);
没有错,后面的两个参数我都试过好几种。我用的是SQL SERVER,可能是不支持,有谁用过?请告诉我是什么原因?
fifth_season 2002-03-04
  • 打赏
  • 举报
回复
你们不要把通用报表中的程序到处贴
wolfsquare 2002-03-04
  • 打赏
  • 举报
回复
另外说明一下:我用的也是MS SQL, 7.0。
wolfsquare 2002-03-04
  • 打赏
  • 举报
回复
我的代码是实际运行过正确的....
如果不行的话,不是你的其它地方出问题就是我开发的程序一直在欺骗我....
haichuang 2002-03-04
  • 打赏
  • 举报
回复
ResultSet rs = conn.getMetaData().getTables(dbName,null,null,new String[]{"TABLE"});
while(rs.next()){
System.out.println(rs.getString(3));
}
haichuang 2002-03-04
  • 打赏
  • 举报
回复
哈哈!我刚写了一个简单的工具,给你贴一下源代码:
==================================================
//DatabaseTool.java

package ejbdesign;

import java.sql.*;

public class DabaseTool {
private DBConn conner;
public DabaseTool(DBConn dbConnection){
this.conner = dbConnection;
}
public java.util.List getTableNameList(String dbName) throws java.sql.SQLException, java.lang.ClassNotFoundException{
Connection conn = conner.getConn();
java.util.ArrayList result = new java.util.ArrayList();
//conn.setCatalog(dbName);
ResultSet rs = conn.getMetaData().getTables(dbName,null,null,new String[]{"TABLE"});
while(rs.next()){
result.add(rs.getString(3));
}
conn.close();
return result;
}
public java.util.List getDbNameList() throws java.sql.SQLException, java.lang.ClassNotFoundException{
Connection conn = conner.getConn();
java.sql.DatabaseMetaData dmd = conn.getMetaData();
java.util.ArrayList result = new java.util.ArrayList();
ResultSet rs = dmd.getCatalogs();
while(rs.next()){
result.add(rs.getString(1));
}
conn.close();
return result;
}

public static void main(String[] args) throws Exception{
//MSSQLDBConn conn = new MSSQLDBConn("dbserver");
ODBCConn conn = new ODBCConn("Oriental");
DabaseTool dbt = new DabaseTool(conn);
pr("Database List\n");
pr("==================\n");
pr(dbt.getDbNameList());
pr("Table list in Oriental\n");
pr("======================\n");
pr(dbt.getTableNameList(null));
}
public static void pr(Object p){
System.out.print(p);
}
public static void pr(Object[] p){
for(int i=0; i<p.length;i++){
pr(p[i]);
}
}
public static void pr(java.util.List list){
for(int i=0; i<list.size(); i++){
pr(list.get(i));
pr("\n");
}
}
public static void pr(ResultSet p) throws Exception{
java.sql.ResultSetMetaData rsmd = p.getMetaData();
int cols = rsmd.getColumnCount();
for(int i=1;i<=cols;i++){
pr(rsmd.getColumnName(i)+"\t");
}
pr("\n=================================================================================\n");
while(p.next()){
for(int i=1; i<=cols; i++){
pr(p.getString(i)+"\t");
}
pr("\n");
}
}
}
peifeinet 2002-03-04
  • 打赏
  • 举报
回复
不用考虑的这么复杂吧,SQL SERVER不是有个系统表SYSOBJECTS嘛,他里面有你所有的表的名称呀!!你可以试试,没有必要一定要用JAVA呀!!
zling 2002-03-03
  • 打赏
  • 举报
回复
对不起,是String[]型的,不过应该是types[0] = "TABLE";
而不是types[0] = "TABLES";

Virginer 2002-03-02
  • 打赏
  • 举报
回复
你用的是什么数据库?看看是不是数据库方面的问题,是不是缺少系统级支持的存储过程啊!
zling 2002-03-02
  • 打赏
  • 举报
回复
嗨!types是String[][]型,不是String[]型
wolfsquare 2002-03-01
  • 打赏
  • 举报
回复
为什么dma.getTables你一定要加"%"呢? 试着将它用null来代替?
ruby123 2002-03-01
  • 打赏
  • 举报
回复
有谁用过
con = DriverManager.getConnection(url);
dma =con.getMetaData();
String[] types = new String[1];
types[0] = "TABLES";
results = dma.getTables(null, null, "%", types);
我得到的results 总是空的,为什么取不出表名?请高手赐教!!!这个问题搞了我几个星期了。
wolfsquare 2002-02-08
  • 打赏
  • 举报
回复
void loadDataBaseTableInfo(){ // 从已指定数据库中获取所有表名,填入列表选择框
int i;
int NumberOfTable;
DatabaseMetaData dbMeta;
ResultSet resultset;
String currentCatalog;
String[] TableType={"TABLE","VIEW"};
String TableName;
Vector listdata=new Vector(20,10);

if (connection!=null){
try{

dbMeta=connection.getMetaData();
currentCatalog=connection.getCatalog ();
resultset=dbMeta.getTables(currentCatalog,null,null,TableType);

while(resultset.next()){
TableName=resultset.getObject(3).toString();
listdata.addElement (TableName);
}
jCboTableName.setModel(new DefaultComboBoxModel(listdata));
if(jCboTableName.getItemCount()>0){
jCboTableName.setSelectedIndex(0);
}

}
catch(SQLException ex){
System.err.println(ex);
return;
}

}
}
ruby123 2002-02-08
  • 打赏
  • 举报
回复
TO skyyoung(路人甲):
你好!你是这个论坛的高手!我知道,我的好几个问题都是你解决的。谢谢你!!!
我的问题是:我要从数据库中取出所有表的清单。
skyyoung 2002-02-08
  • 打赏
  • 举报
回复
java.sql
Interface ResultSetMetaData
All Known Subinterfaces:
RowSetMetaData

--------------------------------------------------------------------------------

public interface ResultSetMetaData
An object that can be used to get information about the types and properties of the columns in a ResultSet object. The following code fragment creates the ResultSet object rs, creates the ResultSetMetaData object rsmd, and uses rsmd to find out how many columns rs has and whether the first column in rs can be used in a WHERE clause.


ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);





--------------------------------------------------------------------------------

Field Summary
static int columnNoNulls
The constant indicating that a column does not allow NULL values.
static int columnNullable
The constant indicating that a column allows NULL values.
static int columnNullableUnknown
The constant indicating that the nullability of a column's values is unknown.
Method Summary
String getCatalogName(int column)
Gets the designated column's table's catalog name.
String getColumnClassName(int column)
Returns the fully-qualified name of the Java class whose instances are manufactured if the method ResultSet.getObject is called to retrieve a value from the column.
int getColumnCount()
Returns the number of columns in this ResultSet object.
int getColumnDisplaySize(int column)
Indicates the designated column's normal maximum width in characters.
String getColumnLabel(int column)
Gets the designated column's suggested title for use in printouts and displays.
String getColumnName(int column)
Get the designated column's name.
int getColumnType(int column)
Retrieves the designated column's SQL type.
String getColumnTypeName(int column)
Retrieves the designated column's database-specific type name.
int getPrecision(int column)
Get the designated column's number of decimal digits.
int getScale(int column)
Gets the designated column's number of digits to right of the decimal point.
String getSchemaName(int column)
Get the designated column's table's schema.
String getTableName(int column)
Gets the designated column's table name.
boolean isAutoIncrement(int column)
Indicates whether the designated column is automatically numbered, thus read-only.
boolean isCaseSensitive(int column)
Indicates whether a column's case matters.
boolean isCurrency(int column)
Indicates whether the designated column is a cash value.
boolean isDefinitelyWritable(int column)
Indicates whether a write on the designated column will definitely succeed.
int isNullable(int column)
Indicates the nullability of values in the designated column.
boolean isReadOnly(int column)
Indicates whether the designated column is definitely not writable.
boolean isSearchable(int column)
Indicates whether the designated column can be used in a where clause.
boolean isSigned(int column)
Indicates whether values in the designated column are signed numbers.
boolean isWritable(int column)
Indicates whether it is possible for a write on the designated column to succeed.

wolfsquare 2002-02-08
  • 打赏
  • 举报
回复
请将出错信息发上来看看。
加载更多回复(1)

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧