Home » Java » JDBC connect to the database

JDBC connect to the database

Import, JDBC, Packages

Import, JDBC, Packages:Addimportstatements
To, your, Java, program, to, import, required, classes, in,, your, Java, code.

import java.sql.* standard JDBC programs; / / for
Import java.math.* BigDecimal and BigInteger support
; / / for
Register, JDBC, Driver:This, step
Causes, the, JVM, to, load, desired, driver, implementation, into, memory, so, the, it, can,, fulfill, your, JDBC, requests.

Approach I - Class.forName ()

try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
}
Catch (ClassNotFoundException, ex) {
System.out.println (Error:, unable, to, load, driver, class,...);
System.exit (1);
}
try {
Class.forName ("oracle.jdbc.driver.OracleDriver").NewInstance ();
}
Catch (ClassNotFoundException, ex) {
System.out.println (Error:, unable, to, load, driver, class,...);
System.exit (1);
Catch (IllegalAccessException, ex) {
System.out.println ("Error:, access, problem, while, loading)";
System.exit (2);
Catch (InstantiationException, ex) {
System.out.println ("Error:, unable, to, instantiate, driver)";
System.exit (3);
}

Approach II - DriverManager.registerDriver ()
You, should, use, theregisterDriver (), method
If, you, are, using, a, compliant, JVM, such, as, the, non-JDK, one, provided, by, Microsoft.


try {
Driver myDriver = new, oracle.jdbc.driver.OracleDriver ();
DriverManager.registerDriver (myDriver);
}
Catch (ClassNotFoundException, ex) {
System.out.println (Error:, unable, to, load, driver, class,...);
System.exit (1);
}

Database, URL, Formulation:This, is
To, create, a, properly, formatted, that, points, to, the, address, database, to, which,, you, wish, to, connect.

getConnection (String, URL)

getConnection (String, URL, Properties, prop)

getConnection (String, URL, String, user, String, password)

Create, Connection, Object:Finally,
Code, a, call, to, theDriverManagerobject'sgetConnection
) method, to, establish, actual, database, connection.

copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest

Hot Channels »