2005-01-21 10:52 Author: Ai92 Source: csdnblog duty Edit: Ark
Java Database Connectivity (JDBC) from a group of Java programming language used to prepare the class and interface. JDBC as a tool / database developers provides a standard API, so that they can use pure Java API to write database applications. However, all developers are not exactly the same interface, so the development will bring about changes in the environment must change the configuration. In this paper, a collection of different database connections.
First, the way to connect various databases速查表
The following lists out the various databases using the JDBC connection, it can be used as a manual.
1, Oracle8/8i/9i database (thin model)
Class.forName ( "oracle.jdbc.driver.OracleDriver"). NewInstance ();
String url = "jdbc: oracle: thin: @ localhost: 1521: orcl"; / / orcl is the 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 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 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 the 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 the database name
Connection conn = DriverManager.getConnection (url);
7, PostgreSQL Database
Class.forName ( "org.postgresql.Driver"). NewInstance ();
String url = "jdbc: postgresql: / / localhost / myDB" / / myDB for the database name
String user = "myuser";
String password = "mypassword";
Connection conn = DriverManager.getConnection (url, user, password);
8, access the ODBC database once a straight
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 ,"","");
Statement stmtNew = conn.createStatement ();
Two, JDBC connections MySql way
The following is a JDBC connection to use MySql's a small tutorial
1, driver search
MySQL currently provided for java driver Connection / J, can be download from the MySQL official website and find the mysql-connector-java-3.0.15-ga-bin.jar file, this driver for the pure java driver, do not need to do other configuration.
2, dynamic designated classpath
If necessary the implementation of dynamically assigned when the classpath, in the implementation of methods used-cp. Otherwise they would be above. Jar files added to the classpath environment variable.
3, loading 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 up to connect the url
jdbc: mysql: / / localhost / databasename [? pa = va] [& pa = va]







