星期日, 8月 06, 2006

JSP與JDBC交戰重點(一)

MySQL Driver(http://www.mysql.com/products/connector-j/index.html)
並置於( ..\webapps\Stores\WEB-INF\lib )目錄下
或 將存放路徑 加入環境變數CLASSPATH

*使用JDBS連接MySQL 三步驟 -

Class.forName("com.mysql.jdbc.Driver"); or Class.forName("com.mysql.jdbc.Driver").newInstance();
---------------------------------------------------------

Connection con = DriverManager.getConnection
      ("jdbc:mysql://localhost:3306/db?name=test&password=pwd");

String url = "jdbc:mysql://localhost:3306?db";
String user = "test";
String password = "pwd";
Connection con = DriverManager(url, user, password);
---------------------------------------------------------

Statement stmt = con.createStatement();

++++++++++++++++++++++++++++++++++++++++

<完整寫法>

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection
      ("jdbc:mysql://localhost:3306/db?name=test&password=pwd");
Statement stmt = con.createStatement();

++++++++++++++++++++++++++++++++++++++++

% ResultSet 之參考文章 %
% 參考文章之重點整理 %

createStatement()不給定參數時,預設是
ResultSet.TYPE_FORWARD_ONLY、 ResultSet.CONCUR_READ_ONLY

resultSetType可指定參數有
  1. ResultSet.TYPE_FORWARD_ONLY
  2. ResultSet.TYPE_SCROLL_INSENSITIVE
  3. ResultSet.TYPE_SCROLL_SENSITIVE
第1個只能使用next()來逐筆取得資料
第2個或第3個,則可用afterLast()、previous()、absolute()、relative()等方法。

**第2個及第3個的差別在於能否取得ResultSet改變值後的資料,另外您還必須指定
resultSetConcurrency
  1. ResultSet.CONCUR_READ_ONLY
  2. ResultSet.CONCUR_UPDATABLE

第1個表示只能讀取 ResultSet的資料
第2個表示可以直接使用ResultSet來操作資料庫

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁