Java uses JDBC to connect to any type of Database (mysql oracle ..), Oraclejdbc

Source: Internet
Author: User
Tags informix sybase database

Java uses JDBC to connect to any type of Database (mysql oracle ..), Oraclejdbc

Package cn. liz. test; import java. io. inputStream; import java. SQL. connection; import java. SQL. driver; import java. SQL. SQLException; import java. util. properties; import org. junit. test; public class JBDCtest {/*** compile a common method to obtain the connection of any database without modifying the source program * solution: put the full class name, url, user, and password of the database Driver implementation class into a * configuration file, and decouple it from the specific database by modifying the configuration file. * @ throws Exception */public Connection getConnection () throws Exception {String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // read the jdbc in the class path. properties file InputStream in = getClass (). getClassLoader (). getResourceAsStream ("jdbc. properties "); Properties properties = new Properties (); properties. load (in); driverClass = properties. getProperty ("driver"); jdbcUrl = properties. getProperty ("jdbcUrl"); user = properties. getProperty ("user"); password = properties. getProperty ("password"); // common Driver objects are reflected by reflection. driver driver = (Driver) Class. forName (driverClass ). newInstance (); Properties info = new Properties (); info. put ("user", user); info. put ("password", password); // obtain the database connection through the connect Method of the Driver. connection connection = driver. connect (jdbcUrl, info); return connection ;}@ Testpublic void testGetConnection () throws Exception {System. out. println (getConnection () ;}< span style = "white-space: pre"> </span>
<Span style = "font-family: Arial, Helvetica, sans-serif;"> jdbc. properties </span> configuration file:
#driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl#user=scott#password=javadriver=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/testuser=rootpassword=

Java JDBC connects to different database writing methods SQL, oracle and mysql JDBC connects to various database writing methods for Java beginners. I suggest you remember to import the corresponding driver jar package.

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

Class. forName ("oracle. jdbc. driver. oracleDriver "). newInstance (); String url = "jdbc: oracle: thin: @ localhost: 1521: orcl"; // orcl database SID 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/sample"; // sample 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 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 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 database name Connection conn = DriverManager. getConnection (url );

6. MySQL database

Class. forName ("org. gjt. mm. mysql. Driver"). newInstance (); String url = "jdbc: mysql: // localhost/myDB? User = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1 "// myDB database name Connection conn = DriverManager. getConnection (url); or

Class. forName ("com. mysql. jdbc. Driver"); String url = "jdbc: mysql: // localhost/myDB? Ser = root & password = system "; conn = DriverManager. getConnection (url );

7. PostgreSQL database

Class. forName ("org. postgresql. driver "). newInstance (); String url = "jdbc: postgresql: // localhost/myDB" // myDB database name String user = "myuser"; String password = "mypassword "; connection conn = DriverManager. getConnection (url, user, password); supplement: SQL Server2005 Database

Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver "). newInstance (); String url = "jdbc: sqlserver: // localhost: 1433; DatabaseName = mydb"; // mydb database String user = "sa"; String password = ""; connection conn = DriverManager. getConnection (url, user, password );
The connection url is a little different from the connection url of sql2000. The JDBC connection method is basically the above.

There are quite a few searches on the Internet. Oh, DriverClass is better to write through the package.
The main url has also changed the corresponding settings of the current database.
When JAVA uses JDBC to connect to the MYSQL database, it establishes another connection object using different database methods to operate the same database.
Url1 = "jdbc: Mysql: // 127.0.0.1: 3306/comm ";

Url2 = "jdbc: Mysql: // 127.0.0.1: 3306/swj2 ";
...
...
DbConnection1 = DriverManager. getConnection (url1, user, password );

DbConnection2 = DriverManager. getConnection (url2, user, password );

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.