?? jdbcadapter.java.bak
字號(hào):
import java.sql.*;
/**類JDBCAdapter用于建立數(shù)據(jù)庫連接*/
class JDBCAdapter{
/*聲明一個(gè)Connection對(duì)象,用于和數(shù)據(jù)庫建立連接*/
Connection connection;
/*聲明一個(gè)Statement對(duì)象,用于對(duì)數(shù)據(jù)庫執(zhí)行SQL語句*/
Statement statement;
/**下面是構(gòu)造方法,四個(gè)參數(shù)分別代表要連接的數(shù)據(jù)庫,驅(qū)動(dòng)程序,用戶名和密碼*/
public JDBCAdapter(String url, String driverName,
String user, String passwd) {
try {
/*返回driverName字符串對(duì)應(yīng)的類對(duì)象*/
Class.forName(driverName);
/*輸出"Opening db connection"提示信息*/
System.out.println("Opening db connection");
/*下面的語句進(jìn)行數(shù)據(jù)庫的連接*/
connection = DriverManager.getConnection(url, user, passwd);
/*創(chuàng)建SQL語句*/
statement = connection.createStatement();
}
catch (ClassNotFoundException ex) {
/*輸出沒有找到數(shù)據(jù)庫驅(qū)動(dòng)程序類的信息*/
System.err.println("Cannot find the database driver classes.");
System.err.println(ex);
}
catch (SQLException ex) {
/*輸出不能連接到數(shù)據(jù)庫的信息*/
System.err.println("Cannot connect to database.");
System.err.println(ex);
}
}
/**方法executeUpdate用于對(duì)數(shù)據(jù)庫進(jìn)行更新,參數(shù)query指定更新操作的SQL語句*/
public void executeUpdate(String query) {
/*if語句中的條件表示數(shù)據(jù)庫連接不成功或者沒有創(chuàng)建SQL語句*/
if (connection == null || statement == null) {
System.err.println("There is no database to execute the query.");
return;
}
try {
/*執(zhí)行SQL語句,對(duì)數(shù)據(jù)庫進(jìn)行更新*/
statement.executeUpdate(query);
}
catch (SQLException ex) {
System.err.println(ex);
}
}
/**方法executeQuery用于對(duì)數(shù)據(jù)庫進(jìn)行查詢,參數(shù)query指定查詢操作的SQL語句*/
public void executeQuery(String query) {
/*if語句中的條件表示數(shù)據(jù)庫連接不成功或者沒有創(chuàng)建SQL語句*/
if (connection == null || statement == null) {
System.err.println("There is no database to execute the query.");
return;
}
try {
/*執(zhí)行SQL語句,對(duì)數(shù)據(jù)庫進(jìn)行更新*/
statement.executeQuery(query);
}
catch (SQLException ex) {
System.err.println(ex);
}
}
/**將前面生成的SQL語句對(duì)象和數(shù)據(jù)庫連接對(duì)象關(guān)閉*/
public void close() throws SQLException {
System.out.println("Closing db connection");
statement.close();
connection.close();
}
/*通過finalize()執(zhí)行上面的close()方法*/
protected void finalize() throws Throwable {
close();
super.finalize();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -