1 / 13

JAVA Database

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

kaz
Download Presentation

JAVA Database

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA Database OOP Praktekdengan Java Miswar ,S.st miswar@bps.go.id Sumber: Eddy MuntinaDharma,ST,MT

  2. KoneksiAplikasi Java ke Database

  3. TahapanAkses Database dengan JDBC

  4. JDBC (Java DB Connectivity) Java application { ... "SELECT ... FROM ... WHERE" ... } DBMS

  5. 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

  6. 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

  7. 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

  8. 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

  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.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

  10. 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

  11. Connecting to Database • Connection is establishedConnection con = DriverManager.getConnection(URL,USERID,PWD); • Connection properties (class Properties) • Close the connectioncon.close();

  12. Simple SQL Statements • Statement object for invocationstmt = conn.createStatement();ResultSetrset= stmt.executeQuery( "SELECT address,script,type FROM worklist"); • ResultSetobject for result processing

  13. StudiKasus

More Related