Analysis of the application of data source in JDBC

Source: Internet
Author: User
Tags requires

Brief introduction

As we all know, JDBC (Java database connection) is an important part of the Java 2 Enterprise Edition. It is an API based on the SQL layer. By embedding SQL statements into the JDBC interface, users can perform almost all database operations through Java programs. JDBC provides only interfaces, and the implementation of a specific class requires the designer of the database to complete it. By generating instances of these interfaces, Java programs can execute SQL calls correctly even for different databases. So for programmers, you don't have to focus on how to send SQL instructions to a database, because programmers need to understand and use only JDBC interfaces, and only in rare cases will use classes that target a particular database, such as a programmer who wants to use Oracle's extended API.

In the JDBC program, the first thing to do is to implement a connection to the database. In the sample program, we are using the oracle8i JDBC package. Connecting to a database typically requires the following steps:

1. Register the database driver (driver). You can explicitly register the driver by calling the Registerdriver method of the Java.sql.DriverManager class, or you can register the driver implicitly by loading the database driver class. For example, we want to register the oracle8i JDBC driver with a virtual machine

// 显式注册
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// 隐式注册
Class.forName(“oracle.jdbc.driver.OracleDriver”);

There is no detailed discussion on how virtual machines automatically register a database driver loaded through the ClassLoader (ClassLoader), which is beyond the scope of this article.

2. Establish a connection. Call the Getconnection () method of the Java.sql.DriverManager class to establish a connection to the database. The getconnection () method returns a Connection object. It should be noted that the getconnection () method automatically selects the most appropriate driver from the database driver registry.

3. When a connection is established, Automatic Updates (autocommit) are allowed. The Serautocommit () method that invokes the Java.sql.Connection interface can set whether the database is updated immediately after the program sends an SQL instruction to the database.

The following is a concrete example. In this instance, the URL used as the getconnection () method parameter is in the NET8 keyword-value pair format. Of course, you can also use the normal format. The database is installed on a server named Chicago, the protocol used is the TCP protocol, the port used is 1521, the SID of the database is CHIDB, and the database driver used is the Oracle JDBC thin driver.

import java.sql.*;
//Initialize constant
private static String URL =
jdbc:oracle:thin:@ (description= address= ( Host=chicago) "+
" (protocol=tcp) (port=1521)) (Connect_data= (SID=CHIDB))) ";
//You can also set the URL to "jdbc:oracle:thin:@ chicago:1521:chidb"
private static String username = "Guest";
Private STA Tic String Password = "Guest";   
Try
{
//Registration Database
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Establish the connection
Connection conn =
drivermanager.getconnection (URL, username, password);
//Allow Automatic Updates
Conn.setauto Commit (TRUE);
}
catch (ClassNotFoundException e)
{
E.printstacktrace ();
}
catch (SQLException e)
{
E.printstacktrace ();
}

From the perspective of practical application, we can see that there are several problems in this way to connect to the database. The first is the security issue, because the program code contains the username and password, other people if you can get bytecode, you can use the Decompile tool to obtain the user name and password. The second is the portability of code. If you want the database name or user name of the connection to change, the programmer needs to modify the source program and then send the modified program to the user. That is, the software cannot exist independently from the database. This will not only greatly improve the cost of software, but also not conducive to the development of the software itself. It is also possible that, in some cases, the organization that provides the data does not want the database username and password to be known to programmers who write the program. This raises the question of how to hide sensitive information when establishing a connection between Java and the database.

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.