NetworkBlog

Bagaimana tanggapan anda ?

Followers

Kaya Sekejap

Java Data Base Connector (JDBC)

The JDBC (Java Database Connectivity) API mendefinisikan interface dan kelas untuk menulis aplikasi database di Jawa dengan membuat koneksi database. Using JDBC you can send SQL, PL/SQL statements to almost any relational database. Menggunakan JDBC Anda dapat mengirim SQL, PL / SQL untuk hampir semua database relasional. JDBC is a Java API for executing SQL statements and supports basic SQL functionality. JDBC adalah sebuah API Java untuk menjalankan pernyataan SQL dan mendukung fungsi dasar SQL. It provides RDBMS access by allowing you to embed SQL inside Java code. Hal ini menyediakan akses RDBMS dengan memungkinkan Anda untuk SQL embed di dalam kode Java. Because Java can run on a thin client, applets embedded in Web pages can contain downloadable JDBC code to enable remote database access. Karena Java dapat dijalankan pada thin client, applet tertanam di halaman Web dapat berisi kode download JDBC untuk mengaktifkan akses basis data jauh. You will learn how to create a table, insert values into it, query the table, retrieve results, and update the table with the help of a JDBC Program example. Anda akan belajar bagaimana membuat tabel, masukkan nilai-nilai ke dalamnya, query meja, mengambil hasil, dan memperbarui tabel dengan bantuan contoh Program JDBC.
 
Meskipun JDBC dirancang khusus untuk menyediakan antarmuka Java untuk database relasional, Anda mungkin menemukan bahwa Anda perlu menulis kode Java untuk mengakses database non-relasional juga.

JDBC Architecture Arsitektur JDBC



 aplikasi Java panggilan perpustakaan JDBC. JDBC loads a driver which talks to the database. Beban JDBC driver yang berbicara ke database. We can change database engines without changing database code. Kita dapat mengubah mesin database tanpa mengubah kode database.

JDBC Basics - Java Database Connectivity Steps JDBC Dasar - Langkah Database Connectivity Jawa

Before you can create a java jdbc connection to the database, you must first import the Sebelum Anda dapat membuat koneksi java jdbc ke database, Anda harus terlebih dahulu mengimpor
java.sql package. paket java.sql.
import java.sql.*; The star ( * ) indicates that all of the classes in the package java.sql are to be imported. import java.sql .*; bintang (*) menunjukkan bahwa semua kelas dalam paket java.sql harus diimpor.
1. 1. Loading a database driver, Loading driver database,
In this step of the jdbc connection process, we load the driver class by calling Class.forName() with the Driver class name as an argument. Dalam langkah dari proses koneksi jdbc, kita load kelas driver dengan memanggil Class.forName () dengan nama kelas Driver sebagai argumen. Once loaded, the Driver class creates an instance of itself. Setelah dimuat, kelas Driver menciptakan sebuah instance dari itu sendiri. A client can connect to Database Server through JDBC Driver. Klien dapat terhubung ke Database Server melalui JDBC Driver. Since most of the Database servers support ODBC driver therefore JDBC-ODBC Bridge driver is commonly used. Karena sebagian besar dari dukungan server Database ODBC driver JDBC-ODBC sehingga Jembatan driver umum digunakan.
The return type of the Class.forName (String ClassName) method is “Class”. Jenis pengembalian metode (String ClassName) Class.forName adalah "Kelas". Class is a class in Class adalah sebuah kelas
java.lang package. java.lang paket.
try { try { 
  Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); //Or any other driver Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); / / Atau pengemudi lain 
 } } 
 catch(Exception x){ catch (Exception x) { 
  System.out.println( “Unable to load the driver class!” ); System.out.println ("Tidak dapat memuat kelas driver!"); 
 } } 
2. 2. Creating a oracle jdbc Connection Membuat Koneksi jdbc oracle

The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. Kelas JDBC DriverManager mendefinisikan benda yang dapat menghubungkan aplikasi Java ke driver JDBC. DriverManager is considered the backbone of JDBC architecture. DriverManager dianggap sebagai tulang punggung arsitektur JDBC. DriverManager class manages the JDBC drivers that are installed on the system. mengelola kelas DriverManager driver JDBC yang diinstal pada sistem. Its getConnection() method is used to establish a connection to a database. getConnection Its () metode yang digunakan untuk membuat sambungan ke database. It uses a username, password, and a jdbc url to establish a connection to the database and returns a connection object. Menggunakan username, password, dan url jdbc untuk membangun koneksi ke database dan mengembalikan sebuah objek koneksi. A jdbc Connection represents a session/connection with a specific database. Sebuah Koneksi jdbc merupakan sesi / koneksi dengan database tertentu. Within the context of a Connection, SQL, PL/SQL statements are executed and results are returned. Dalam konteks sebuah Connection, SQL, PL / pernyataan SQL dijalankan dan hasilnya dikembalikan. An application can have one or more connections with a single database, or it can have many connections with different databases. Sebuah aplikasi dapat memiliki satu atau lebih koneksi dengan database tunggal, atau bisa punya banyak koneksi dengan database yang berbeda. A Connection object provides metadata ie information about the database, tables, and fields. Sebuah object Connection menyediakan informasi yaitu metadata tentang database, tabel, dan bidang. It also contains methods to deal with transactions. Ia juga berisi metode untuk menangani transaksi.
JDBC URL Syntax :: jdbc: <subprotocol>: <subname> JDBC URL Sintaks:: jdbc: <subprotocol>: <subname> 
JDBC URL Example :: jdbc: <subprotocol>: <subname>•Each driver has its own subprotocol JDBC URL Contoh:: jdbc: <subprotocol>: • <subname> driver Masing-masing memiliki subprotocol sendiri
•Each subprotocol has its own syntax for the source. • subprotocol Masing-masing memiliki sintaks sendiri untuk sumber. We're using the jdbc odbc subprotocol, so the DriverManager knows to use the sun.jdbc.odbc.JdbcOdbcDriver. Kami menggunakan odbc subprotocol jdbc, sehingga DriverManager mengetahui untuk menggunakan sun.jdbc.odbc.JdbcOdbcDriver tersebut.
try{ try { 
  Connection dbConnection=DriverManager.getConnection(url,”loginName”,”Password”) Koneksi dbConnection = DriverManager.getConnection (url, "loginName", "Password") 
 } } 
 catch( SQLException x ){ catch (SQLException x) { 
  System.out.println( “Couldn't get connection!” ); System.out.println ("Tidak dapat mendapatkan koneksi!"); 
 } } 
3. 3. Creating a jdbc Statement object, Membuat obyek Pernyataan jdbc,
Once a connection is obtained we can interact with the database. Setelah sambungan diperoleh kita dapat berinteraksi dengan database. Connection interface defines methods for interacting with the database via the established connection. Koneksi antarmuka mendefinisikan metode untuk berinteraksi dengan database melalui koneksi didirikan. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method. Untuk menjalankan statemen SQL, Anda perlu instantiate objek Pernyataan dari objek koneksi Anda dengan menggunakan createStatement () method.
Statement statement = dbConnection.createStatement(); Pernyataan Pernyataan = dbConnection.createStatement ();
A statement object is used to send and execute SQL statements to a database. Sebuah objek pernyataan digunakan untuk mengirim dan mengeksekusi pernyataan SQL ke database.
Three kinds of Statements Tiga jenis Laporan
Statement: Execute simple sql queries without parameters. Pernyataan: sederhana query sql Jalankan tanpa parameter.
Statement createStatement() Pernyataan createStatement ()
Creates an SQL Statement object. Membuat obyek Pernyataan SQL.
Prepared Statement: Execute precompiled sql queries with or without parameters. Disiapkan Pernyataan: Jalankan query sql dikompilasi dengan atau tanpa parameter.
PreparedStatement prepareStatement(String sql) PreparedStatement prepareStatement (String sql)
returns a new PreparedStatement object. mengembalikan sebuah objek PreparedStatement baru. PreparedStatement objects are precompiled obyek PreparedStatement yang dikompilasi
SQL statements. Pernyataan SQL.
Callable Statement: Execute a call to a database stored procedure. Callable Pernyataan: Execute panggilan ke prosedur yang tersimpan database.
CallableStatement prepareCall(String sql) CallableStatement prepareCall (String sql)
returns a new CallableStatement object. mengembalikan sebuah objek CallableStatement baru. CallableStatement objects are SQL stored procedure call statements. objek CallableStatement adalah pernyataan SQL prosedur tersimpan panggilan.
4. 4. Executing a SQL statement with the Statement object, and returning a jdbc resultSet. Pelaksana pernyataan SQL dengan objek Pernyataan, dan mengembalikan ResultSet jdbc.
Statement interface defines methods that are used to interact with database via the execution of SQL statements. Pernyataan antarmuka mendefinisikan metode yang digunakan untuk berinteraksi dengan database melalui eksekusi pernyataan SQL. The Statement class has three methods for executing statements: Kelas Pernyataan memiliki tiga metode untuk laporan mengeksekusi:
executeQuery(), executeUpdate(), and execute(). executeQuery (), executeUpdate (), dan melaksanakan (). For a SELECT statement, the method to use is executeQuery . Untuk statemen SELECT, metode untuk digunakan adalah executeQuery. For statements that create or modify tables, the method to use is executeUpdate. Untuk pernyataan yang membuat atau memodifikasi tabel, metode untuk digunakan adalah executeUpdate. Note: Statements that create a table, alter a table, or drop a table are all examples of DDL Catatan: Pernyataan yang membuat tabel, mengubah tabel, atau drop tabel adalah semua contoh DDL
statements and are executed with the method executeUpdate. pernyataan dan dijalankan dengan metode executeUpdate. execute() executes an SQL execute () melaksanakan SQL
statement that is written as String object. pernyataan yang ditulis sebagai objek String.
ResultSet provides access to a table of data generated by executing a Statement. ResultSet menyediakan akses ke tabel data yang dihasilkan dengan menjalankan sebuah Pernyataan. The table rows are retrieved in sequence. Para baris tabel yang akan diambil secara berurutan. A ResultSet maintains a cursor pointing to its current row of data. Sebuah ResultSet menjaga kursor menunjuk ke baris saat ini data. The next() method is used to successively step through the rows of the tabular results. Metode berikutnya () digunakan untuk berturut-turut langkah melalui deretan hasil tabel.
ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. ResultSetMetaData Antarmuka memegang informasi mengenai jenis dan sifat-sifat kolom dalam ResultSet. It is constructed from the Connection object. Hal ini dibangun dari object Connection.

Test JDBC Driver Installation Uji Instalasi JDBC Driver

import javax.swing.JOptionPane; import javax.swing.JOptionPane; 

  public class TestJDBCDriverInstallation_Oracle { public class TestJDBCDriverInstallation_Oracle { 

     public static void main(String[] args) { public static void main (String [] args) { 
     StringBuffer output = new StringBuffer(); StringBuffer output = new StringBuffer (); 
     output.append(”Testing oracle driver installation \n”); output.append ("oracle Pengujian instalasi driver \ n"); 
     try { try { 
      String className = “sun.jdbc.odbc.JdbcOdbcDriver”; String className = "sun.jdbc.odbc.JdbcOdbcDriver"; 
      Class driverObject = Class.forName(className); Kelas driverObject = Class.forName (className); 
      output.append(”Driver : “+driverObject+”\n”); output.append ("Driver:" + driverObject + "\ n"); 
      output.append(”Driver Installation Successful”); output.append ("Instalasi Driver Berhasil"); 
      JOptionPane.showMessageDialog(null, output); JOptionPane.showMessageDialog (null, output); 
       } catch (Exception e) { } Catch (Exception e) { 
        output = new StringBuffer(); keluaran baru StringBuffer = (); 
        output.append(”Driver Installation FAILED\n”); output.append ("Instalasi Driver FAILED \ n"); 
        JOptionPane.showMessageDialog(null, output); JOptionPane.showMessageDialog (null, output); 
     System.out.println(”Failed: Driver Error: ” + e.getMessage()); System.out.println ("Gagal: Driver Error:" + e.getMessage ()); 
     } } 
      } } 
  } } 
Download JDBC Sample Code Download Contoh Kode JDBC

Java JDBC Connection Example, JDBC Driver Example Java Contoh Koneksi JDBC, Contoh Driver JDBC

import java.sql.Connection; java.sql.Connection impor; 
 import java.sql.DatabaseMetaData; java.sql.DatabaseMetaData impor; 
 import java.sql.DriverManager; java.sql.DriverManager impor; 
 import java.sql.SQLException; java.sql.SQLException impor; 
 public class JDBCDriverInformation { public class JDBCDriverInformation { 
  static String userid=”scott”, password = “tiger”; statis String userid = "scott", password = "harimau"; 
  static String url = “jdbc:odbc:bob”; statis String url = "jdbc: odbc: bob"; 
 static Connection con = null; Koneksi statis con = null; 
  public static void main(String[] args) throws Exception { public static void main (String [] args) throws Exception { 
      Connection con = getOracleJDBCConnection(); Connection con = getOracleJDBCConnection (); 
      if(con!= null){ if (null con! =) { 
         System.out.println(”Got Connection.”); System.out.println ("Koneksi Punya."); 
         DatabaseMetaData meta = con.getMetaData(); DatabaseMetaData meta = con.getMetaData (); 
         System.out.println(”Driver Name : “+meta.getDriverName()); System.out.println ("Driver Name:" + meta.getDriverName ()); 
         System.out.println(”Driver Version : “+meta.getDriverVersion()); System.out.println ("Driver Version:" + meta.getDriverVersion ()); 

      }else{ } Else { 
       System.out.println(”Could not Get Connection”); System.out.println ("Tidak dapat Get Connection"); 
      } } 
  } } 

  public static Connection getOracleJDBCConnection(){ public static getOracleJDBCConnection Koneksi () { 

   try { try { 
    Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); 
 } catch(java.lang.ClassNotFoundException e) { } Catch (java.lang.ClassNotFoundException e) { 
    System.err.print(”ClassNotFoundException: “); System.err.print ("ClassNotFoundException:"); 
    System.err.println(e.getMessage()); System.err.println (e.getMessage ()); 
   } } 

   try { try { 
      con = DriverManager.getConnection(url, userid, password); con = DriverManager.getConnection (url, userid, password); 
   } catch(SQLException ex) { } Catch (SQLException ex) { 
    System.err.println(”SQLException: ” + ex.getMessage()); System.err.println ("SQLException:" + ex.getMessage ()); 
   } } 

   return con; return con; 
  } } 
 } } 

0 komentar:

Posting Komentar