Get the App
SLTechnology News&Howtos  ›  Database  › 

What are the common JDBC database connection methods?

Shulou Source: shulou.com Published: 2022-05-31 22:53:00 09月24日 Update

What are the common ways to connect to JDBC databases? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. JDBC connects to DB2

Class.forName ("Com.ibm.db2.jdbc.net.DB2Driver"); String url= "jdbc:db2://dburl:port/DBname" cn = DriverManager.getConnection (url, sUsr, sPwd)

2. JDBC connects to Microsoft SQLServer (microsoft)

Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver"); cn = DriverManager.getConnection ("jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master", sUsr, sPwd)

3. JDBC connects to Sybase (jconn2.jar)

Class.forName ("com.sybase.jdbc2.jdbc.SybDriver"); cn = DriverManager.getConnection ("jdbc:sybase:Tds:DBServerIP:2638", sUsr, sPwd)

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

Class.forName ("org.gjt.mm.mysql.Driver"); cn = DriverManager.getConnection ("jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd)

5. JDBC connection PostgreSQL (pgjdbc2.jar)

Class.forName ("org.postgresql.Driver"); cn = DriverManager.getConnection ("jdbc:postgresql://DBServerIP/myDatabaseName", sUsr, sPwd)

6. JDBC connects to Oracle (classes12.jar)

Class.forName ("oracle.jdbc.driver.OracleDriver"); cn = DriverManager.getConnection ("jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd)

7. JDBC connects to ODBC

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection ("jdbc:odbc:" + sDsn, sUsr, sPwd)

The jdbc connection method for some databases is not fixed, depending on the driver package you use.

For example, mssql's jtdsjar package:

Database URL:jdbc:jtds:sqlserver://localhost:1433;DatabaseName=XXX

Driver class: net.sourceforge.jtds.jdbc.Driver

1. Oracle8/8i/9i database (thin mode)

Class.forName ("oracle.jdbc.driver.OracleDriver") .newInstance (); String url= "jdbc:oracle:thin:@localhost:1521:orcl"; / / orcl is the SID String user= "test" of the database; 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/sample"; / / sample is your database name String user= "admin"; String password= ""; Connection conn= DriverManager.getConnection (url,user,password)

3. Sql Server7.0/2000 database

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

4. Sybase database

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

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 is the database name Connection conn= DriverManager.getConnection (url)

6. MySQL database

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 is the database name Connection conn= DriverManager.getConnection (url)

7. PostgreSQL database

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

8. ODBC is directly connected to access database.

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, ","). After reading the above, have you mastered any common ways to connect JDBC databases? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data database method common use method content more problem driver helpless for this reason skills patterns articles experience industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Huawei NVidia Shulou Tech Info