Java connection all kinds of database way quick look-up table

Source: Internet
Author: User
Tags db2 informix postgresql sybase sybase database oracle database
The following is a list of how the various databases use JDBC connections and can be used as a manual.

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 of the database
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 for 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 as a 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 for 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 for 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 for Database name
Connection conn= drivermanager.getconnection (URL);

7. PostgreSQL Database
Class.forName ("Org.postgresql.Driver"). newinstance ();
String url = "Jdbc:postgresql://localhost/mydb"//mydb for database name
String user= "MyUser";
String password= "MyPassword";
Connection conn= drivermanager.getconnection (Url,user,password);

8, Access database directly with the ODBC
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url= "Jdbc:odbc:driver={microsoft Access Driver (*.mdb)};D bq=" +application.getrealpath ("/data/reportdemo.mdb “);
Connection conn = drivermanager.getconnection (URL, "", "");
Statement stmtnew=conn.createstatement ();

Two, JDBC connection MySQL Way

Here's a little tutorial on using JDBC to connect to MySQL

1. Find the driver

MySQL currently offers Java drivers for connection/j, which can be downloaded from the MySQL official website, and locate the Mysql-connector-java-3.0.15-ga-bin.jar file, which is a pure Java driver and does not require additional configuration.
2, dynamic designation Classpath

If you need to dynamically specify CLASSPATH when executing, use the-CP method at execution time. Otherwise, add the. jar file above to the CLASSPATH environment variable.

3. Load Driver
try{
Class.forName (Com.mysql.jdbc.Driver);
System.out.println (Success loading Mysql driver!);
}catch (Exception e)
{

System.out.println (Error loading Mysql driver!);
E.printstacktrace ();
}

4, set the URL of the connection

JDBC:MYSQL://LOCALHOST/DATABASENAME[?PA=VA][&PA=VA]

Three, here are some tips for using JDBC to connect to an Oracle database

1, use the thin driver in the client software development

In the development of Java software, Oracle's database provides four types of drivers, two for application software, applets, Servlets and other client software, and two for the Java stored procedures in the database and other server-side software. In the development of client side software, we can choose OCI driver or thin driver. The OCI driver uses the Java Localization Interface (JNI) to communicate with the database through Oracle client software. The thin driver is a pure Java driver that communicates directly with the database. For maximum performance, Oracle recommends using the OCI driver in the development of the client software, which seems to be correct. However, I recommend using the thin driver because multiple tests have found that, in general, the performance of the thin driver exceeds the OCI driver.

2, turn off the automatic submission function, improve system performance

When you first establish a connection to a database, by default, the connection is in autocommit mode. For better performance, you can turn off the autocommit feature by calling the Setautocommit () method of the connection class with the Boolean false parameter, as follows:
Conn.setautocommit (FALSE);

It is noteworthy that once the autocommit feature is turned off, we need to manage the transaction manually by invoking the commit () and rollback () Methods of the connection class.
3. Use the Statement object in dynamic SQL or time constrained commands

When executing SQL commands, we have two choices: you can use the PreparedStatement object, or you can use the statement object. No matter how many times the same SQL command is used, PreparedStatement only parses and compiles it once. When the statement object is used, it is parsed and compiled each time an SQL command is executed. This may make you think that using a PreparedStatement object is faster than using a statement object. However, the tests I conducted showed that this was not the case in the client software. Therefore, in a time-constrained SQL operation, we should consider using the statement object, unless the SQL commands are processed in batches.

Also, using the statement object makes it easier to write dynamic SQL commands because we can concatenate strings together to create a valid SQL command. Therefore, I think the statement object makes it easier to create and execute dynamic SQL commands.

4. Use helper function to format dynamic SQL commands

When creating dynamic SQL commands that are executed using the statement object, we need to deal with some formatting problems. For example, if we want to create an SQL command that inserts the name O ' Reilly into the table, you must replace the "'" Number in O ' Reilly with a two-connected "" number. The best way to do this is to create a helper method that completes the substitution operation, and then use the helper method that is created when the connection string expresses an SQL command in the heart-taking formula. Similarly, we can have the helper method accept a date-type value and then let it output based on the ORAC

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.