Start by first preparing to test if the Oracle database starts normally "is the primary Oracle-related service open"
Then create a class that connects to the database with the first thing to remember is what steps are required to connect to the database, and what parameters are required for these steps 1. Load class-driven parameters:
the corresponding driver name"2. Get Connection" parameter:
URL
, user name, password "You can then get a connection to the connection database. Configure the parameters first. Private Static String
classname = " Oracle.jdbc.driver.OracleDriver " ;
private
static String
url="Jdbc:oracle:thin : @192.168.2.2:1521:java46 " ;
private
static String
username= "Scott" ;
private
static String
password= "NIIT" ;1. Load DriverClass.
forname(
classname); 2. Get the connection
Connection =drivermanager.
Getconnection(
url,
username,
password);
Package Com.niit.common;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Public
class jdbcconnectfactory { Class-Driven name
private
static String
classname= " Oracle.jdbc.driver.OracleDriver " ; Url
private
static String
url="Jdbc:oracle:thin : @192.168.2.2:1521:java46 " ; Connect to database user name
private
static String
username= "Scott" ;Password to connect to the database
private
static String
password= "NIIT" ;Declares aConnection
private
static Connection
Connection =
Null; The method is statically decorated to form a static code block, and the value is loaded once Public
static Connection getconnection () {
Try {
Try { Singleton design Mode "There is only one connect for the entire project, if it is empty" If < Span style= "color: #0000c0; Font-family:courier New; Font-size:small; " >
connection ==
null || Connection .isclosed ()) {Load Class-drivenClass.
forname(
ClassName);Get database connection Connection =drivermanager.
getconnection (
URL
Username Password } }
catch (SQLException e) { e.printstacktrace (); } }
catch (classnotfoundexception e) { e.printstacktrace (); }Back to Connection
return
connection; } }
JDBC-connected Oracle optimized version