[CODE]
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class DatabaseUtilities {
static String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
static String dbName = "JDBC";
static String urlRoot = "jdbc:odbc:";
private ActionListener exceptionListener = null;
public DatabaseUtilities() {
registerDriver();
}
public void setDatabaseName(String dbName) {
this.dbName = dbName;
}
public void registerDriver() {
try {
Class.forName(jdbcDriver);
DriverManager.registerDriver(new JdbcOdbcDriver());
} catch(ClassNotFoundException e) {
reportException(e.getMessage());
} catch(SQLException e) {
reportException(e.getMessage());
}
}
public String login(String tableName, String dbuser, String dbpassword)
{
String message = null;
Vector user = new Vector();
Vector password = new Vector();
String url = urlRoot + dbName;
String SQLCommand = "SELECT * FROM " + tableName + ";";
try {
Connection con = DriverManager.getConnection(url);
Statement stms = con.createStatement();
ResultSet rs = stms.executeQuery(SQLCommand);
while(rs.next()) {
user.add(rs.getString("name").trim());
password.add(rs.getString("password").trim());
}
} catch(SQLException sqlExc) {
reportException(sqlExc.getMessage());
}
if (!user.contains(dbuser) && !password.contains(dbpassword))
{
message = "你输入的用户名不存在";
}
else
{
if (!password.contains(dbpassword))
{
message = "你输入的密码有误";
}else
{
message = "你以管理员"+dbuser+"的身份登陆";
}
}
return message;
}
public void execute(String SQLCommand) {
String url = urlRoot + dbName;
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
stmt.execute(SQLCommand);
con.close();
} catch(SQLException e) {
reportException(e.getMessage());
}
}
public void excute(String[] SQLCommand) {
String url = urlRoot + dbName;
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
for(int i = 0; i < SQLCommand.length; i++) {
stmt.execute(SQLCommand[i]);
}
con.close();
} catch(SQLException e) {
reportException(e.getMessage());
}
}
public String[] getColumnNames(String tableName) {
Vector dataSet = new Vector();
String[] columnNames = null;
String url = urlRoot + dbName;
String SQLCommand = "SELECT * FROM " + tableName + ";";
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLCommand);
ResultSetMetaData md = rs.getMetaData();
columnNames = new String[md.getColumnCount()];
for(int i = 0; i < columnNames.length; i++) {
columnNames[i] = md.getColumnLabel(i+1);
}
con.close();
} catch(SQLException e) {
reportException(e.getMessage());
}
return columnNames;
}
public String[] getColumnNamesUsingQuery(String SQLCommand) {
Vector dataSet = new Vector();
String[] columnNames = null;
String url = urlRoot + dbName;
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLCommand);
ResultSetMetaData md = rs.getMetaData();
columnNames = new String[md.getColumnCount()];
for(int i = 0; i < columnNames.length; i++) {
columnNames[i] = md.getColumnLabel(i+1);
}
con.close();
} catch(SQLException e) {
reportException(e.getMessage());
}
return columnNames;
}
public String[] getDataTypes(String tableName) {
Vector dataSet = new Vector();
String[] dataTypes = null;
String url = urlRoot + dbName;
String SQLCommand = "SELECT * FROM " + tableName + ";";
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLCommand);
ResultSetMetaData md = rs.getMetaData();
dataTypes = new String[md.getColumnCount()];
for(int i = 0; i < dataTypes.length; i++) {
dataTypes[i] = md.getColumnTypeName(i+1);
}
con.close();
} catch(SQLException e) {
reportException(e.getMessage());
}
return dataTypes;
}
public Vector executeQuery(String SQLQuery) {
Vector dataSet = new Vector();
String url = urlRoot + dbName;
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLQuery);
ResultSetMetaData md = rs.getMetaData();
int nColumns = md.getColumnCount();
while(rs.next()) {
Vector rowData = new Vector();
for(int i = 1; i <= nColumns; i++) {
rowData.addElement(rs.getObject(i));
}
dataSet.addElement(rowData);
}
con.close();
} catch(SQLException e) {
reportException(e.getMessage());
}
return dataSet;
}
public void setExceptionListener(ActionListener exceptionListener) {
this.exceptionListener = exceptionListener;
}
private void reportException(String exception) {
if(exceptionListener != null) {
ActionEvent evt = new ActionEvent(this, 0, exception);
exceptionListener.actionPerformed(evt);
}
else {
System.err.println(exception);
}
}
}
[/CODE]
其它的一些方法,我写的程序用没用到。。。我只是把我的代码,插入到原来书上例题中去了。。。
个人认为这些方法应该可以包括操作数据库所有的操作了
[此贴子已经被作者于2007-1-14 13:01:48编辑过]
开开心心的过&玩每一天!!!!