How to use the. properties file configuration driver in JDBC

Source: Internet
Author: User
Tags driver manager

JDBC is the abbreviation for the Java database connection (Java Datebase connectivity), for example, for an Oracle database connection, to connect to a database using. properties as a configuration file.


Using JDBC, you can add, delete, change, check (curd: Create), update (update), read (Retrieve) and delete (delete) operations, and you need to configure the driver files to be able to use JDBC functionality. You can configure the driver directly in the Java code type, as follows:


Step one: Import jar packages to create a Lib folder under the Src folder to place the drive jar package. Select a different jar package based on the version of the JDK. For example, I use the jdk1.6 version, I choose the Ojdbc6.jar. Select the jar package, right-click, and select Build path, and then you can add the driver after you have successfully created the access path;

Step Two: Add driver


You need to configure the following information:

Driver = Oracle.jdbc.driver.OracleDriver

url = jdbc:oracle:thin: @localhost: 1521:ORCL

user = Scott

Password = Tiger


Note:


1. How to obtain the driver path: After opening the Add Class Library (library), Expand Ojdbc6.jar, Find Oracle.jdbc.driver and expand, find Oracledriver.class and expand, right-click on the expanded. class file to select Copy qualified Name. It is recommended to use the above methods to prevent writing omissions.

2.url is the access path for the database. The meaning of each element is as follows:

JDBC Database Access Protocol

Oracle data volume, if you use a different database, you need to change the database name
Thin a driver model for Oracle databases, common Oracle database-driven models and OCI, typically thin

@localhost Local Host IP, can also be accessed with 127.0.0.1, if the database is in other IP needs to use the IP of the database host to access

1521 port number for Oracle database access

ORCL the access name entered when the database is installed

3.user database user developer to provide the corresponding username after creating the database, this example uses the Oracle sample username and password

4.password Database User Login password


Step three Create a database connection after you configure the above information, you can use the reflection method to configure the drive by using the following methods:

Reflection Creation Driver Manager

Class.forName (driver);

Create DATABASE Access connection

Connection conn = drivermanager.getconnection (URL, user, password);

Creating SQL statements

String sql = "~~~~~~~";//create according to your requirements

Step fourth creates a send statement and executes

Create a Send statement PreparedStatement

PreparedStatement pstmt = conn.preparestatement (sql);

Execute the SQL statement (for example, query)
ResultSet rs = Pstmt.excutequery ();


Fifth-Step data processing

Processing RS According to demand

Step sixth close the resource

Close Resource

Rs.close ();

Pstmt.close;

Conn.close;

As you can see from the examples above, adding drivers to the drive path and Java code, if the information sent to the database changes, the need to modify the Java code, may bring inconvenience to development. To solve this problem, you can use a special configuration file to deal with the configuration content, when the database information changes, only need to modify the configuration file, it can enhance the robustness of the code. The specific implementation methods are as follows:

The first step is to create a properties file

Under the SRC package, create a file with the name Conn.properties, because the java.util.properties package can read and write to the properties file

Add the following information to the Conn.properties file:

Driver = Oracle.jdbc.driver.OracleDriver

url = jdbc:oracle:thin: @localhost: 1521:ORCL

user = Scott

Password = Tiger

Save.

The second step is to create a driver with a static code block

Variable declaration

private static String driver;

private static String URL;

private static String user;

private static String password;

Static code block creation driver

static{

Create a Properties Object

Properties prop = new Pro[perties ();

Reflection Read Conn.properties File

Prop.load (ClassName.class.getClassLoader (). getResourceAsStream ("conn.properties"));

Assigning a member variable

Driver = Prop.getproperty ("Driver");

url = prop.getproperty ("url");

user = Prop.getproperty ("user");

Password = prop.getproperty ("Passeord");

Reflection capture Driver Manager

Class.forName (driver);

}


The advantage of the above drive-driven approach is to separate the configuration-driven information from the Java code, to modify the configuration file conn.properties when the database information changes, and to improve the robustness of the program without having to modify the Java code.

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.