JDBC Various database connection URL key codes

Source: Internet
Author: User
Tags db2 postgresql sybase sybase database

When connecting to a database through JDBC, each database has a different URL format, in order to make it easier for everyone to use, I have provided the following 7 kinds of common database connection sample code, according to the actual needs of the corresponding changes.

1.Oracle Database

Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();

String url = "Jdbc:oracle:thin: @localhost: 1521:orcle";

String user = "Test";

String password = "Test";

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


2.DB2 Database

Class.forName ("Com.ibm.db2.jdbc.app.DB2Driver"). newinstance ();

String url = "Jdbc:db2://localhost:5000/testdb";

String user = "admin";

String password = "Test";

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

3.SQL Server Database

Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();

String url = "Jdbc:microsoft:sqlserver://localhost:1433;databasename=testdb";

String user = "sa";

String password = "Test";

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

4. Sybase database

Class.forName ("Com.sybase.jdbc.SybDriver"). newinstance ();

String url = "Jdbc:sybase:tds:localhost:5007/testdb";

Properties sysprops = System.getproperties ();

Sysprops.put ("User", "userid");

Sysprops.put ("Password", "User_password");

Connection conn = drivermanager.getconnection (URL, sysprops);

5.Informix Database

Class.forName ("Com.infoxmix.jdbc.IfxDriver"). newinstance ();

String url = "jdbc:infoxmix-sqli://localhost:1533/testdb:informixserver=myserver;user=testuser;password= Testpassword ";

Connection conn = drivermanager.getconnection (URL);

6.MySQL Database

Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();

String url = "jdbc:mysql://localhost/testdb?user=testuser&password=testpassword&useunicode=true& characterencoding=gb2312 ";

Connection conn = drivermanager.getconnection (URL);

7.PostgreSQL Database

Class.forName ("Org.postgresql.Driver"). newinstance ();

String url = "Jdbc:postgresql://localhost/testdb";

String user = "MyUser";

String password = "Test";

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

JDBC Various database connection URL key codes

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.