html5-img
1 / 27

JDBC

JDBC. Ada 7 lanhkah untuk mengakses database: Load the JDBC Driver Define the connection URL Establish the connection Create a statement object Execute a query or update Process the result Close the connection. JDBC. try { // Class.forName("org.gjt.mm.mysql.Driver"); // Step 1

garin
Download Presentation

JDBC

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. JDBC • Ada 7 lanhkah untuk mengakses database: • Load the JDBC Driver • Define the connection URL • Establish the connection • Create a statement object • Execute a query or update • Process the result • Close the connection

  2. JDBC try { // Class.forName("org.gjt.mm.mysql.Driver"); // Step 1 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // step1 } catch (Exception E) { E.printStackTrace(); } // end catch

  3. JDBC try { con = DriverManager.getConnection("jdbc:odbc:Database1");//,"root",""); // step 2 and 3

  4. JDBC stmt = con.createStatement(); // step 4 rs = stmt.executeQuery("SQL Query'"); // step 5 while (rs.next()) { // step 6 : : } // while

  5. JDBC stmt.close(); // step7 con.close();

  6. Access Database • Microsoft Acess Database : db1

  7. Setting Access Database • Menset Database yang dipakai sebagai acuan dalam Program • Start -> Control Panel -> ODBC Data Source

  8. Setting Access Database • Tampil jendela ODBC Data Source Administrator • Click System DSN-> Add-> pilih Driver (Microsoft Access Driver) -> Finish. Dalam hal ini kita memakai Access Database. • Tentukan Letak File Database yang digunakan

  9. Setting Access Database • Click OK • Data base db1 siap diakses oleh Program

  10. JDBC Program try { conn = DriverManager.getConnection("jdbc:odbc:db1"); // step 2 and 3 stmt = conn.createStatement(); // step 4 rs = stmt.executeQuery("SELECT * FROM tabel02"); while (rs.next()) { // step 6 System.out.print(rs.getString("Nomer") + " "); System.out.print(rs.getString("Judul") + " "); System.out.print(rs.getString("Pengarang") + " "); System.out.print(rs.getString("Penerbit") + " "); System.out.print(rs.getString("harga") + " "); System.out.println("Oke"); } // while stmt.close(); conn.close(); } catch(SQLException sqle) { System.err.println("SQLException : " + sqle.getMessage()); } // end cacth } } package jdbc01; import java.sql.*; public class JDBCTest01 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // step1 } catch (ClassNotFoundException cnfe) { System.err.println(cnfe); } catch (Exception E) { E.printStackTrace(); } // end catch

  11. JDBC Program • Hasil Program 1 Java 2 Complete Reference Patrick Naughton McGraw Hill 600000 Oke 2 Distributed Systems Tanenbaum Prentice Hall 500000 Oke 3 Home Networking Bible Plumley IDG Books 375000 Oke • Bandingkan

  12. JDBC Program dengan Access • Setting perlu dilakukan pada Control Panel, untuk menset Database yang dipakai • Pada Java Program tidak diperlukan setting apapun • Sun telah menyediakan driver secara otomatis untuk Access • Hal ini tidak berlaku untuk Sistem Database yang lain, misal: mysql, oracle, dll.

  13. Mysql Database • Mysql dapat didownload di : www.mysql.com/downloads • Extract and Just install it: Setup.exe • Mysql terletak di C:\mysql • Aktifkan mysql admin dengan double click di : C:\mysql\bin\winmysqladmin

  14. Mysql Database • Inilah tampilannya • Atau tampak di pojok kanan bawah

  15. Mysql Database • Mysql database : test002 • Tabel : tabel02

  16. Java to mysql • JDBC Driver for MySQL dapat di-download di : www.mysql.com/downloads dengan nama : MySQL Connector/J 2.0.14 • Extract ke C:\ • JDBC Driver akan terletak di subdirectory C:\mysql-connector-java-2.0.14

  17. Java to mysql • Dengan JBUILDER, buat program (class) Java misalnya : JDBCTest01.java • Project -> Project Properties -> Path -> Required Libraries -> Add... -> New, tuliskan nama library-nya, misal mysql-connector-java, click Add...

  18. Java to mysql • Pilih subdirectory C:\mysql-connector-java-..., di mana JDBC driver untuk mysql diletakkan, click OK

  19. Java to mysql • Library yang baru akan tampak seperti pada gambar, click OK

  20. JDBC Program try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test002","rsn","risanuri"); stmt = conn.createStatement(); // step 4 rs = stmt.executeQuery("SELECT * FROM tabel02"); while (rs.next()) { // step 6 System.out.print(rs.getString("Nomer") + " "); System.out.print(rs.getString("Judul") + " "); System.out.print(rs.getString("Pengarang") + " "); System.out.print(rs.getString("Penerbit") + " "); System.out.print(rs.getString("harga") + " "); System.out.println("Oke"); } // while stmt.close(); conn.close(); } catch(SQLException sqle) { System.err.println("SQLException : " + sqle.getMessage()); } // end cacth } } package jdbc01; import java.sql.*; public class JDBCTest01 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("org.gjt.mm.mysql.Driver"); // Step 1 } catch (ClassNotFoundException cnfe) { System.err.println(cnfe); } catch (Exception E) { E.printStackTrace(); } // end catch

  21. Hasil JDBC mysql 1 Core Servlets and JSP Marty Hall Prentice Hall 350000 Oke 2 Java 2 Complete Reference Patrick Naughton McGraw Hill 600000 Oke • J2EE Unleashed Bambara Allen Sams 600000 Oke • Bandingkan

  22. JDBC Program dengan mysql • Setting tidak perlu dilakukan pada Control Panel • Perlu dilakukan setting pada Java Program • Driver JDBC disediakan oleh mysql dan harus di-download secara terpisah • Mysql memungkinkan memanggil Database lewat jaringan

  23. Tambahan con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pasiendreritta","root",""); stmt = con.createStatement(); stmt.executeUpdate("INSERT INTO catatanpasien VALUES('"+ navn[0] +"','"+ navn[1] +"','"+ navn[2] +"','"+ navn[3] +"','"+ navn[4] +"','"+ navn[5] +"');");

  24. Mysql tools • Dbtools • Mysql-front

  25. Java to mysql • Setting Classpath, prosesnya sama dengan setting classpath yang lain • Pada Win98, Start ->Run (sysedit), kemudian pada autoexec.bat tambahkan C:\mysql-connector-java-2.0.14, yang merupakan letak subdir JDBC Driver

  26. Java to mysql

  27. Java to mysql • Compile • myjava>javac JDBCTest01.java • Buat subdir jdbc01 di dalam :\myjava • Letakkan JDBCTest01.class pada subdir :\myjava\jdbc01 • Eksekusi • \myjava>java jdbc01.JDBCTest01 1 Routing TCP/IP Jeff Doyle null 900000 Oke 2 Top Down Network Design Oppenheimer 0 600000 Oke 3 Java Security Jess Gams 0 500000 Oke

More Related