Beruflich Dokumente
Kultur Dokumente
JDBC driver A JDBC driver is located in cslabs dir: /usr/local/jt6/lib/mysql-connector-java-5.1.7-bin.jar You can either: copy the zipped file to your local directory and unzip it: > jar xvf mysql-connector-java-5.1.7-bin.jar or make a link from directory com in your local dir to the system JDBC driver. A brief manual of MySQL JDBC in CSlab can be found at: http://personal.cs.cityu.edu.hk/jdemo/
2
JDBC Structure
Java applications
JDBC API JDBC driver manager JDBC driver API JDBC/ODBC Bridge ODBC driver database database
3
2-Tiers Structure
(proxy): 4040
DBMS system
3-Tiers Structure
DBMS system
10
13
(proxy): 4040
DBMS system
14
private Vector queryResults; public synchronized void start() { if (worker == null) { message = "Connecting to database"; worker = new Thread(this); worker.start(); } } public void run() { String url = "jdbc:mysql: //personal.cs.cityu.edu.hk:4040/db_jdemo "; String query = "select COF_NAME, PRICE from COFFEES"; try { Class.forName("com.mysql.jdbc.Driver"); Vector results = new Vector(); con= DriverManager. getConnection(url, "jdemo", "apple1");
stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) {// process rs row by row String s = rs.getString("COF_NAME"); float f = rs.getFloat("PRICE"); String text = s + " " + f; results.addElement(text); } stmt.close(); con.close(); setResults(results); repaint(); } catch(SQLException e) {;} }
15
16
3-Tiers Structure
DBMS system
17
3.
4.
20
21
while (rs.next()) { String s = rs.getString("COF_NAME"); float f = rs.getFloat("PRICE"); String text = s + "|" + f; results.append(text + "\n"); } stmt.close(); con.close(); return(results.toString());
}
22
23