Java JDBC Link Database

Source: Internet
Author: User

How JDBC Works

-Load Driver

-Create Statement Object

-Execute SQL statement

-Working with result sets

-Close Connection

Drive Load Mode

Class.forName ("Oracle.jdbc.driver.OracleDriver") (note: The Load driver class driver class implements autoenrollment in DriverManager via a static block)

Connection con=drivermanager.getcommection ("Address parameter", "Database Account", "Database Password"), note: Connection is only the interface is actually implemented by the database vendor provided by the driver package is completed

Statement interface (not recommended for performing static block drives in general projects)

Statement stmt = Con.createstatement ();

Oracle implementation to download the corresponding Ojdbc6.jar/ojdbc14.jar

Below we use Factory mode to establish data connection

Import java.io.IOExecption;

Import java.sql.SQLException;

Import java.sql.Commection;

Import java.util.Properties;

First, you need to have a db.properties file that stores the linked data information

The file saves the following data (note that there is no space in the stored file)

Driver=oracle ....... Database-driven

url=localhost//Database Address

User=root//Database name

pwd=root//Database Password

Data loading

Class Trean () {

private static driver; Database-driven

private static URL//database address

Prvate static user//database name

private static PWD//Database Password

Because this data needs to be loaded once, it's OK.

static{

Need to instantiate object properties

try{

Properties P = new properties ();

P.load (Trean.class.getClassLoader (). getResourceAsStream ())

Driver = P.getproperty ("Driver");

url = p.getproperty ("url");

user = P.getproperty ("user");

PWD = P.getproperty ("pwd");

Class.forName (driver);//Load Drive

}catch (exeption e) {

E.printstacktrace ();

throw new Runexception ("Load resource failed", E);

}

}

Public Connection getconnection () throws sqlexception{

Return drivermanager.getcommection (URL,USER,PWD);

}

public void Colse (Connection con) {

if (con!=null) {

try{

Con.close

}catch (exeption e) {

E.printstacktrace ();

throw new Runexception ("Close connection Failed", e);

}

}

}

}

Java JDBC Link Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.