我的书上是这样连的:// TestConnection.java – Load JDBC and connect to databaseimport java.sql.*;
import oracle.jdbc.driver.*;public class TestConnection {public static void main(String [] vars)
{
Connection conn;
try
{
DriverManager.registerDriver(new
oracle.jdbc.OracleDriver());// The following needs to be edited with
// your database specifics:
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@noizmaker:1521:osiris",
"scott", "tiger");
}
catch (SQLException e)
{
System.out.println("Caught: " + e);
System.exit(1);
}
}
}
import oracle.jdbc.driver.*;public class TestConnection {public static void main(String [] vars)
{
Connection conn;
try
{
DriverManager.registerDriver(new
oracle.jdbc.OracleDriver());// The following needs to be edited with
// your database specifics:
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@noizmaker:1521:osiris",
"scott", "tiger");
}
catch (SQLException e)
{
System.out.println("Caught: " + e);
System.exit(1);
}
}
}
老兄我要的不是怎么连接oracle啊,而是取的表的元数据
ResultSet getColumns(String catalog, String schemaPattern,
String tableNamePattern, String columnNamePattern)
throws SQLExceptionGets a description of the table columns available in catalog catalog.Descriptions of columns are returned only if the table schema name matches schemaPattern, the table name matches tableNamePattern, and the column name matches columnNamePattern. The descriptions are ordered by the columns TABLE_SCHEM, TABLE_NAME, and ORDINAL_POSITION.If the driver supports the new data types available in the JDBC 2.0 API, they will be included as rows in the ResultSet object this method returns.PARAMETERS:catalog
a String object representing a catalog name; "" retrieves columns for tables without a catalog; null indicates that the catalog name should not be used to narrow the search
schemaPattern
a String object representing a schema name pattern; "" retrieves columns for tables without a schema; null indicates that the schema name should not be used to narrow the search
tableNamePattern
a String object representing a table name pattern
columnNamePattern
a String object representing a column name pattern
RETURNS:a ResultSet object, with each row being a description of a table columnEach row in the ResultSet object has the following fields:1. TABLE_CAT
String object giving the table catalog, which may be null
2. TABLE_SCHEM
String object giving the table schema, which may be null
3. TABLE_NAME
String object giving the table name
4. COLUMN_NAME
String object giving the column name
5. DATA_TYPE
short indicating the JDBC (SQL) data type from java.sql.Types
6. TYPE_NAME
String object giving the local type name used by the data source
7. COLUMN_SIZE
int indicating the column size. For char or date types, this is the maximum number of characters; for numeric or decimal types, this is the precision.
8. BUFFER_LENGTH
is not used
9. DECIMAL_DIGITS
int indicating the number of fractional digits
10. NUM_PREC_RADIX
int indicating the radix, which is typically either 10 or 2
11. NULLABLE
int indicating whether a column can be NULL
The possible values are:
columnNoNulls
- NULL values might not be allowed
columnNullable
- NULL values are definitely allowed
columnNullableUnknown
- whether NULL values are allowed is unknown
12. REMARKS
String object containing an explanatory comment on the column; may be null
13. COLUMN_DEF
String object containing the default value for the column; may be null
14. SQL_DATA_TYPE
int; currently unused
15. SQL_DATETIME_SUB
int; currently unused
16. CHAR_OCTET_LENGTH
int indicating the maximum number of bytes in the column (for char types only)
17. ORDINAL_POSITION
int indicating the index of the column in a table; the first column is 1, the second column is 2, and so on
18. IS_NULLABLE
String object; either NO indicating that the column definitely does not allow NULL values, YES indicating that the column might allow NULL values, or an empty string ("") indicating that nullability is unknown
19. SCOPE_CATALOG
String object giving the catalog of the table that is the scope of a reference attribute; null if DATA_TYPE is not REF
20. SCOPE_SCHEMA
String object giving the schema of the table that is the scope of a reference attribute; null if DATA_TYPE is not REF
21. SCOPE_TABLE
String object giving the table name that is the scope of a reference attribute; null if DATA_TYPE is not REF
22. SOURCE_DATA_TYPE
short giving the SQL type from java.sql.Types that is the source type of a distinct type or user-generated Ref type; null if DATA_TYPE is not DISTINCT or a user-generated REF
EXAMPLE:ResultSet rs = dbmd.getColumns(null, null, "EMPLOYEES", "%NAME");
把 tableNamePattern设为 null 过,有数据出现的,真的搞不懂...救命啊!!!!!!!!!!!!!!!
ResultSet rs = dbmd.getColumns(null, null, "EMPLOYEES", "%NAME");你的:
rs = dmd.getColumns(null,null,tableName,"%"(null也试过不行));是不是这里“"%"(null也试过不行)”的问题?
如:
ResultSet rs=stmt.executeQuery(query);//query是select from your table
ResultSetMetaData md=rs.getMetaData();
//下面也不要用while (rs.next()),原因是记录集可能没有记录,如查询空表等情况。
for(int i=1;i<md.getColumnCount();i++){
//你的打印代码
}
taleName, null));
System.out.println(" ");
while (results.next()){
clowname=results.getString("COLUMN_NAME");
……
试试这个