Frequently Used JDBC Connection Methods

Source: Internet
Author: User
Tags informix sybase sybase database

1. JDBC connection to DB2

Copy codeThe Code is as follows:
Class. forName ("Com.ibm.db2.jdbc.net. DB2Driver ");
String url = "jdbc: db2: // dburl: port/DBname"
Cn = DriverManager. getConnection (url, sUsr, sPwd );

2. JDBC connection to Microsoft SQLServer (microsoft)

Copy codeThe Code is as follows:
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
Cn = DriverManager. getConnection ("jdbc: microsoft: sqlserver: // DBServerIP: 1433; databaseName = master", sUsr, sPwd );

Iii. JDBC connection to Sybase (jconn2.jar)

Copy codeThe Code is as follows:
Class. forName ("com. sybase. jdbc2.jdbc. SybDriver ");
Cn = DriverManager. getConnection ("jdbc: sybase: Tds: DBServerIP: 2638", sUsr, sPwd );

4. JDBC connection to MySQL (mm. mysql-3.0.2-bin.jar)

Copy codeThe Code is as follows:
Class. forName ("org. gjt. mm. mysql. Driver ");
Cn = DriverManager. getConnection ("jdbc: mysql: // DBServerIP: 3306/myDatabaseName", sUsr, sPwd );

V. JDBC connection to PostgreSQL (pgjdbc2.jar)

Copy codeThe Code is as follows:
Class. forName ("org. postgresql. Driver ");
Cn = DriverManager. getConnection ("jdbc: postgresql: // DBServerIP/myDatabaseName", sUsr, sPwd );

6. JDBC connection to Oracle (classes12.jar)

Copy codeThe Code is as follows:
Class. forName ("oracle. jdbc. driver. OracleDriver ");
Cn = DriverManager. getConnection ("jdbc: oracle: thin: @ MyDbComputerNameOrIP: 1521: ORCL", sUsr, sPwd );

VII. JDBC connection to ODBC

Copy codeThe Code is as follows:
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Connection cn = DriverManager. getConnection ("jdbc: odbc:" + sDsn, sUsr, sPwd );

The jdbc connection method of some databases is not fixed. It depends on the driver package you use.
For example, the jtdsjar package of mssql:
Database URL: jdbc: jtds: sqlserver: // localhost: 1433; DatabaseName = XXX
Driver Class: net. sourceforge. jtds. jdbc. Driver


1. Oracle8/8i/9i Database (thin Mode)

Copy codeThe Code is as follows:
Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
String url = "jdbc: oracle: thin: @ localhost: 1521: orcl"; // orcl indicates the SID of the database.
String user = "test ";
String password = "test ";
Connection conn = DriverManager. getConnection (url, user, password );

2. DB2 database

Copy codeThe Code is as follows:
Class. forName ("com. ibm. db2.jdbc. app. DB2Driver"). newInstance ();
String url = "jdbc: db2: // localhost: 5000/sample"; // sample is your database name
String user = "admin ";
String password = "";
Connection conn = DriverManager. getConnection (url, user, password );

3. SQL Server7.0/2000 database

Copy codeThe Code is as follows:
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver"). newInstance ();
String url = "jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = mydb ";
// Mydb is a database
String user = "sa ";
String password = "";
Connection conn = DriverManager. getConnection (url, user, password );

4. Sybase Database

Copy codeThe Code is as follows:
Class. forName ("com. sybase. jdbc. SybDriver"). newInstance ();
String url = "jdbc: sybase: Tds: localhost: 5007/myDB"; // myDB is your database name
Properties sysProps = System. getProperties ();
SysProps. put ("user", "userid ");
SysProps. put ("password", "user_password ");
Connection conn = DriverManager. getConnection (url, SysProps );

5. Informix Database

Copy codeThe Code is as follows:
Class. forName ("com. informix. jdbc. IfxDriver"). newInstance ();
String url = "jdbc: informix-sqli: // 123.45.67.89: 1533/myDB: INFORMIXSERVER = myserver;
User = testuser; password = testpassword "; // myDB indicates the database name.
Connection conn = DriverManager. getConnection (url );

6. MySQL database

Copy codeThe Code is as follows:
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance (); // or Class. forName ("com. mysql. jdbc. Driver ");
String url = "jdbc: mysql: // localhost/myDB? User = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1"
// MyDB indicates the Database Name
Connection conn = DriverManager. getConnection (url );

7. PostgreSQL database

Copy codeThe Code is as follows:
Class. forName ("org. postgresql. Driver"). newInstance ();
String url = "jdbc: postgresql: // localhost/myDB" // myDB indicates the database name.
String user = "myuser ";
String password = "mypassword ";
Connection conn = DriverManager. getConnection (url, user, password );

8. access database directly connected to ODBC

Copy codeThe Code is as follows:
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
String url = "jdbc: odbc: Driver = {MicroSoft Access Driver (*. mdb)}; DBQ =" + application. getRealPath ("/Data/ReportDemo. mdb ");
Connection conn = DriverManager. getConnection (url ,"","");

 

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.