130 likes | 319 Views
JAVA Database. OOP Praktek dengan Java Miswar ,S.st miswar@bps.go.id Sumber : Eddy Muntina Dharma,ST,MT. Koneksi Aplikasi Java ke Database. Tahapan Akses Database dengan JDBC. JDBC (Java DB Connectivity). Java application { ... "SELECT ... FROM ... WHERE" ... }. DBMS. Java
E N D
JAVA Database OOP Praktekdengan Java Miswar ,S.st miswar@bps.go.id Sumber: Eddy MuntinaDharma,ST,MT
JDBC (Java DB Connectivity) Java application { ... "SELECT ... FROM ... WHERE" ... } DBMS
Java application JDBC-API JDBC- Driver manager Native Protocol driver JDBC- Net-driver JDBC-ODBC bridge Native API-driver DB- Middleware ODBC Client library Client library JDBC Drivers
Running a JDBC Application Phase Task Relevant java.sql classes Load driver Create connection DriverManager Connection Initialisation Statement ResultSet etc. Generate SQL statements Process result data Processing Terminate connection Release data structures Connection Statement etc. Termination
A Simple JDBC application import java.sql.*; public class jdbctest { public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection ("jdbc:mysql://lsir-cis-pc8:3306/pcmdb", "user", "passwd"); Statement stmt = con.createStatement(); ResultSetrs = stmt.executeQuery ("select name, number from pcmtable where number < 2"); while(rs.next()) System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")"); stmt.close() con.close(); } catch(Exception e){ System.err.println(e); } }} loadDriver getConnection createStatement execute(SQL) Result handling yes More results ? no closeStatment closeConnection
Loading of Driver • Creates an instance of the driver • Registers driver in the driver manager • Explicit loadingString l_driver = "com.mysql.jdbc.Driver";Class.forName(l_driver); • Several drivers can be loaded and registered
Implicit Driver Loading • Setting system property: jdbc.drivers • A colon-separated list of driver classnames • Can be set when starting the application java -Djdbc.drivers=com.mysql.jdbc.Driverapplication • Can also be set from within the Java application Properties prp = System.getProperties(); prp.put("jdbc.drivers" "com.mimer.jdbc.Driver:com.mysql.jdbc.Driver"); System.setProperties(prp); • The DriverManager class attempts to load all the classes specified in jdbc.drivers when the DriverManager class is initialized
Addressing Database • A connection is a session with one database • Databases are addressed using a URL of the form "jdbc:<subprotocol>:<subname>" • Examples jdbc:mysql:database jdbc:mysql://host/database jdbc:mysql://host:port/database • Defaults: host=localhost, port=3306
Connecting to Database • Connection is establishedConnection con = DriverManager.getConnection(URL,USERID,PWD); • Connection properties (class Properties) • Close the connectioncon.close();
Simple SQL Statements • Statement object for invocationstmt = conn.createStatement();ResultSetrset= stmt.executeQuery( "SELECT address,script,type FROM worklist"); • ResultSetobject for result processing