Download presentation
Presentation is loading. Please wait.
Published byLorin Rogers Modified over 9 years ago
1
JAVA DATABASE OOP Praktek dengan Java Miswar,S.st miswar@bps.go.id Sumber : Eddy Muntina Dharma,ST,MT
2
KONEKSI APLIKASI JAVA KE DATABASE
3
Tahapan Akses Database dengan JDBCResultSet Statement selectupdatecreateinsert Connection userpasswordhostdatabaseurl DriverManager Driver MySQLPostgreSQLOracle
4
JDBC (Java DB Connectivity) Java application {... "SELECT... FROM... WHERE"... } DBMS
5
JDBC Drivers Java application JDBC- Driver manager Native Protocol driver JDBC- Net-driver Native API-driver JDBC-ODBC bridge Client library DB- Middleware ODBC Client library JDBC-API
6
Running a JDBC Application PhaseTaskRelevant java.sql classes Initialisation Processing Termination Load driver Create connection Generate SQL statements Process result data Terminate connection Release data structures DriverManager Connection Statement ResultSet etc. Connection Statement etc.
7
A Simple JDBC application loadDriver getConnection createStatement execute(SQL) Result handling More results ? closeStatment closeConnection no yes 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(); ResultSet rs = 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); } }}
8
Loading of Driver Creates an instance of the driver Registers driver in the driver manager Explicit loading String l_driver = "com.mysql.jdbc.Driver"; Class.forName(l_driver); Several drivers can be loaded and registered
9
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.Driver application 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
10
Addressing Database A connection is a session with one database Databases are addressed using a URL of the form "jdbc: : " Examples jdbc:mysql:database jdbc:mysql://host/database jdbc:mysql://host:port/database Defaults: host=localhost, port=3306
11
Connecting to Database Connection is established Connection con = DriverManager.getConnection(URL,USERID,PW D); Connection properties (class Properties) Close the connection con.close();
12
Simple SQL Statements Statement object for invocation stmt = conn.createStatement(); ResultSet rset= stmt.executeQuery( "SELECT address,script,type FROM worklist"); ResultSet object for result processing
13
Studi Kasus
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.