JDBC - JDBC To develop a database application, JDBC or ODBC...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Sept. 2009 Yangjun Chen ACS-3902 1 JDBC To develop a database application, JDBC or ODBC should be used. JDBC – JAVA Database Connectivity ODBC – Open Database Connectivity JDBC-ODBC Bridge ODBC Driver Database Client Client Database Server
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sept. 2009 Yangjun Chen ACS-3902 2 JDBC Connection to a database: 1. Loading driver class Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); 2. Connection to a database String url = “jdbc:odbc:<databaseName>”; Connction con = DriverManager.getConnection(url, <userName>, <password>)
Background image of page 2
Sept. 2009 Yangjun Chen ACS-3902 3 JDBC 1. Sending SQL statements Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(“SELECT * FROM Information WHERE Balance >= 5000”); 4. while (rs.next()) { … } a table name
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sept. 2009 Yangjun Chen ACS-3902 4 JDBC import java.sql.*; public class DataSourceDemo1 { public static void main(String[] args) { Connection con = null; try {//load driver class Class.forName{“sun.jdbs.odbs.JdbsOdbcDriver”); //data source String url = “jdbs:odbc:Customers”; //get connection con = DriverManager.getConnection(url, “sa”, “ “)
Background image of page 4
Sept. 2009 Yangjun Chen ACS-3902 5 JDBC //create SQL statement Statement stmt = con.createStatement(); //execute query Result rs = stmt.executeQuery(“SELECT * FROM Information WHERE Balance >= 5000”); String firstName, lastName; Date birthDate; float balance; int accountLevel;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sept. 2009 Yangjun Chen ACS-3902 6 JDBC while(rs.next()) {firstName = rs.getString(“FirstName”); lastName = rs.getString(“lastName”); balance = rs.getFloat(“Balance”); System.out.println(firstName + “ “ + lastName + “, balance = “ + balance); } } catch(Exception e) {e.printStackTrace();} finally {try{con.close();} catch(Exception e){ } } } }
Background image of page 6
Sept. 2009 Yangjun Chen ACS-3902 7 JDBC Programming in an dynamical environment: Disadvantage of DataSourceDemo1 : If the JDBC-ODBC driver, database, user names, or password are changed, the program has to be modifid. Solution: Configuration file: config.driver=sun.jdbc.odbc.JdbcOdbcDriver config.protocol=jdbc config.subprotocol=odbc config.dsname=Customers config.username=sa config.password= file name: datasource.config <property> = <property value> config – datasource name
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sept. 2009 Yangjun Chen ACS-3902 8 JDBC import java.sql.*; import java.io.*; import java.util.Properties; public class DatabaseAccess { private String configDir; //directory for configuration file private String dsDriver = null; private String dsProtocol = null; private String dsSubprotocol = null; private String dsName = null; private String dsUsername = null; private String dsPassword = null;
Background image of page 8
Sept. 2009 Yangjun Chen ACS-3902 9 JDBC public DatabaseAccess(String configDir) { this.configDir = configDir; }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/17/2010 for the course ACS ACS-3911 taught by Professor Chen during the Spring '10 term at Windsor.

Page1 / 29

JDBC - JDBC To develop a database application, JDBC or ODBC...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online