Connect to the database using Java (MySQL, SQL Server, Oracle, DB2)

Source: Internet
Author: User

MySQL:

Private string conurl = "JDBC: mysql: // localhost: 3306/Database"
Class. forname ("com. MySQL. JDBC. Driver ");
Conn = drivermanager. getconnection (conurl, "username", "password ");

SQL Server:

(Jtds connection ):
Private string url = "JDBC: jtds: sqlserver: // localhost: 1433/Database ";
Class. forname ("net. SourceForge. jtds. JDBC. Driver ");
Conn = drivermanager. getconnection (URL, "username", "password ");

(JDBC connection)
String drivername = "com. Microsoft. sqlserver. JDBC. sqlserverdriver ";
String dburl = "JDBC: sqlserver: // localhost: 1433; databasename = Database ";
Class. forname (drivername );
Dbconn = drivermanager. getconnection (dburl, "username", "password ");

ORACLE:
1. jdbc oci: This driver is similar to the traditional ODBC driver. Because it requires Oracle call interface and net8, it needs to install client software on the machine that runs the Java program using this driver.
2. JDBC thin: This type of driver is generally used in Java programs running in Web browsers. It does not communicate through OCI or net8, but through Java sockets. Therefore, you do not need to install the client software on the client machine that uses JDBC thin.
3. JDBC kprb: This type of driver is used by Java programs directly stored in the database, such as Java stored procedures, triggers, and database JSP's. It uses the default/current database session and thus requires no additional database username, password or URL.

(JDBC thin connection)
Class. forname ("oracle. JDBC. Driver. oracledriver ");
String url = "JDBC: oracle: thin: @ machine (IP @) (machine IP): 1521 (port number): ora1 (database )";
Connection conn = drivermanager. getconnection (URL, "username", "password ");

(Jdbc oci connection)
Class. forname ("oracle. JDBC. Driver. oracledriver ");
String url = "JDBC: oracle: oci8: @ ora1 (Net Server)"; // or oci9 @ service, userid, password
Connection conn = drivermanager. getconnection ("url", "Scott (username)", "Tiger (password )");

DB2:

First: at present, IBM has not provided the JDBC driver for type 1.

Type 2 driver: COM. IBM. db2.jdbc. App. db2driver. The driver is also located in the package db2java.zip. JDK must be able to access DB2's db2jdbc. dll and so on.

Usage:
Class. forname ("com. IBM. db2.jdbc. App. db2driver"). newinstance ();
String url = "JDBC: DB2: Sample (database )";
Connection con = drivermanager. getconnection (URL, user, password );

Type 3 Driver: com.ibm.db2.jdbc.net. db2driver, which is located in the package db2java.zip.

Usage:
Class. forname ("com.ibm.db2.jdbc.net. db2driver"). newinstance ();
// The target DB2 system listens to this service on the default port 6789.
// Start the db2jstrt command on the database server to enable the network port.
String url = "JDBC: DB2: // host: 6789: Sample (database )";
Connection con = drivermanager. getconnection (URL, user, password );

Type 4 Driver: COM. IBM. db2.jcc. db2driver, which is located in the package db2jcc. jar.

In this case, the database encoding is UTF-8. Otherwise, an error is returned! Create Database dbname using codeset UTF-8 territory CN,
This statement can be used to create a unicode database with the specified region as China (CN. You can also use JDK for compiling when installing the DB2 database. The path is ../home/IBM/sqllib/Java/JDK, And the JDK version is changed to jdk1.3.

Usage:
Class. forname ("com. IBM. db2.jcc. db2driver"). newinstance ();
String url = "JDBC: DB2: // host: 50000/sample (database )";
Connection con = drivermanager. getconnection (URL, user, password );

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.