📄 15p.txt
字号:
class Database
{
//这是一个将 JDBC 数据库的所有功能封装在单个对象中的类
Connection con;
resultSet results;
ResultSetMetaData rsmd;
DatabaseMetaData dma;
String catalog;
String types[];
public Database(String driver)
{
types = new String[1];
types[0] = "TABLES"; //初始化类型
try{Class.forName(driver);} //加载 JDBC-ODBC 桥驱动程序
catch (Exception e)
{System.out.println(e.getMessage());}
}
//--
public void Open(String url, String cat)
{
catalog = cat;
try {con = DriverManager.getConnection(url);
dma =con.getMetaData(); //获取元数据
}
catch (Exception e)
{System.out.println(e.getMessage());}
}
//--
public String[] getTableNames()
{
String[] tbnames = null;
Vector tname = new Vector();
//将表名添加到一个 Vector 中,
//因为我们不知道有多少个表
try {
results =
new resultSet(dma.getTables(catalog, null,
"%", types));
while (results.hasMoreElements())
tname.addElement(results.getColumnValue("TABLE_NAME"));
}
catch (Exception e) {System.out.println(e);}
//将表名复制到一个 String 数组中
tbnames = new String[tname.size()];
for (int i=0; i< tname.size(); i++)
tbnames[i] = (String)tname.elementAt(i);
return tbnames;
}
//--
public String[] getTableMetaData()
{
// 返回表类型的信息
results = null;
try{
results =
new resultSet(dma.getTables(catalog, null,
"%", types));
}
catch (Exception e)
{System.out.println(e.getMessage());}
return results.getMetaData();
}
//--
public String[] getColumnMetaData(String tablename)
{
//返回一个列的数据
results = null;
try {
results =
new resultSet(dma.getColumns(catalog, null,
tablename, null));
}
catch (Exception e)
{System.out.println(e.getMessage());}
return results.getMetaData();
}
//--
public String[] getColumnNames(String table)
{
//返回一个列名数组
String[] tbnames = null;
Vector tname = new Vector();
try {
results =
new resultSet(dma.getColumns(catalog, null,
table, null));
while (results.hasMoreElements() )
tname.addElement(results.getColumnValue("COLUMN_NAME"));
}
catch (Exception e) {System.out.println(e);}
tbnames = new String[tname.size()];
for (int i=0; i< tname.size(); i++)
tbnames[i] = (String)tname.elementAt(i);
return tbnames;
}
//--
public String getColumnValue(String table,
String columnName)
{
//返回给定列的值
String res = null;
try
{
if (table.length()>0)
results =
Execute("Select " + columnName +
" from " + table +
" order by "+columnName);
if (results.hasMoreElements())
res = results.getColumnValue(columnName);
}
catch (Exception e)
{System.out.println("Column value error" +
columnName+ e.getMessage());}
return res;
}
//--
public String getNextValue(String columnName)
{
// 使用存储的 resultSet
//返回该列的下一个值
String res = "";
try
{
if (results.hasMoreElements())
res = results.getColumnValue(columnName);
}
catch (Exception e)
{System.out.println("next value error"+
columnName+ e.getMessage());}
return res;
}
//--
public resultSet Execute(String sql)
{
//对此数据库执行一个 SQL 查询
results = null;
try
{
Statement stmt = con.createStatement();
results = new resultSet(stmt.executeQuery(sql));
}
catch (Exception e)
{System.out.println("execute error"+
e.getMessage());}
return results;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -