📄 tableinfotest.java
字号:
package ch03.section09;
//Properties对象定义在util包中
//导入JDBCAPI
import java.sql.*;
import java.util.*;
public class TableInfoTest {
public static void main(String args[]) {
//指定数据源
String url = "jdbc:odbc:sample";
//声明连接对象
Connection con;
//声明结果集元数据对象
ResultSetMetaData dmd;
Statement sm;
//声明结果集对象存储数据库信息
ResultSet rs;
try {
//指定驱动程序名称
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
//装载驱动程序
Driver driver = (Driver) Class.forName(driverName).newInstance();
//检测驱动程序是否正确装入
if (driver == null) {
System.out.println("驱动程序装入不成功");
}
else {
System.out.println("驱动程序装入成功");
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
try {
//添加用户名和密码
Properties conn_prp = new Properties();
conn_prp.put("user", "sa");
conn_prp.put("password", "sa");
//获取与指定数据源连接的连接对象
con = DriverManager.getConnection(url, conn_prp);
if (con == null) {
System.out.println("数据库连接不成功!");
}
else {
System.out.println("数据库连接成功!");
}
sm = con.createStatement();
rs = sm.executeQuery("select * from tickets");
dmd = rs.getMetaData();
//Gets the designated column's table's catalog name
String CatalogName = dmd.getCatalogName(2);
if (CatalogName != null || CatalogName.equals("")) {
System.out.println(" 2 column's table's catalog name: " + CatalogName);
}
else {
System.out.println("CatalogName is null");
}
//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.
String ColumnClassName = dmd.getColumnClassName(2);
if (ColumnClassName != null) {
System.out.println("the fully-qualified name of the Java class:" +
ColumnClassName);
}
else {
System.out.println("ColumnClassName is null");
}
//Returns the number of columns in this ResultSet object.
int ColumnCount = dmd.getColumnCount();
System.out.println("the number of columns in this ResultSet object:" +
ColumnCount);
//Indicates the designated column's normal maximum width in characters.
int ColumnDisplaySize = dmd.getColumnDisplaySize(2);
System.out.println(
"the designated column's normal maximum width in characters: " +
ColumnDisplaySize);
//Gets the designated column's suggested title for use in printouts and displays.
String ColumnLabel = dmd.getColumnName(2);
if (ColumnLabel != null) {
System.out.println("the designated column's suggested title:" +
ColumnLabel);
}
else {
System.out.println("ColumnLabel is null");
}
//Get the designated column's name.
String ColumnName = dmd.getColumnName(2);
if (ColumnName != null) {
System.out.println("the designated column's name:" + ColumnName);
}
else {
System.out.println("ColumnName is null");
}
//Retrieves the designated column's database-specific type name.
String ColumnTypeName = dmd.getColumnTypeName(2);
if (ColumnTypeName != null) {
System.out.println(
"the designated column's database-specific type name:" +
ColumnTypeName);
}
else {
System.out.println("ColumnTypeName is null");
}
//Retrieves the designated column's SQL type.
int ColumnType = dmd.getColumnType(2);
System.out.println("the designated column's SQL type: " +
dmd.getColumnType(2));
//Gets the designated column's number of digits to right of the decimal point.
int Scale = dmd.getScale(2);
System.out.println(
"the designated column's number of digits to right of the decimal point: " +
Scale);
//Gets the designated column's table name.
String TableName = dmd.getTableName(2);
if (TableName != null) {
System.out.println("the designated column's table name: " + TableName);
}
else {
System.out.println("TableName is null");
}
//Indicates whether a write on the designated column will definitely succeed.
boolean DefinitelyWritable = dmd.isDefinitelyWritable(2);
if (DefinitelyWritable) {
System.out.println(
"A write on the designated column will definitely succeed");
}
else {
System.out.println(
"A write on the designated column will definitely unsucceed");
}
//Indicates whether the designated column is definitely not writable.
boolean ReadOnly = dmd.isReadOnly(2);
if (ReadOnly) {
System.out.println("the designated column is definitely not writable");
}
else {
System.out.println("the designated column is writable");
}
//Indicates whether the designated column can be used in a where clause.
boolean Searchable = dmd.isSearchable(2);
if (Searchable) {
System.out.println(
"the designated column can be used in a where clause");
}
else {
System.out.println(
"the designated column can not be used in a where clause");
}
//Indicates whether it is possible for a write on the designated column to succeed.
boolean Writable = dmd.isWritable(2);
if (Writable) {
System.out.println(
"it is possible for a write on the designated column to succeed");
}
else {
System.out.println(
"it isn't possible for a write on the designated column to succeed");
}
//Indicates whether values in the designated column are signed numbers.
boolean Signed = dmd.isSigned(2);
if (Signed) {
System.out.println("values in the designated column are signed numbers");
}
else {
System.out.println(
"values in the designated column aren't signed numbers");
}
//closed all
rs.close();
sm.close();
con.close();
}
catch (SQLException ex) {
System.err.println(ex.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -