以下罗列了数据库使用jdbc连接的方式,能够作为一个手册使用
1、Oracle8、8i、9i数据库(thin模式)
2、DB2数据库
3、Sql Server7.0、2000数据库
4、Sybase数据库
5、Informix数据库
6、MySql数据库
7、PostgreSQL数据库
8、Access数据库直接使用ODBC
实例如下:
package com.sinosoft.test_01;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import java.util.Properties;import org.omg.CORBA.portable.ApplicationException;public class TestAllConnection { private static Connection connection = null; /** * Oracle8/8i/9i数据库(thin模式) */ public static void getOracleConnection() { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); connection = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl", "test", "test"); } catch (Exception e) { // TODO: handle exception } } /** * DB2数据库 */ public static void getDB2Connection() { try { Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance(); // sample为数据库实例 connection = DriverManager.getConnection( "jdbc:db2://localhost:5000/sample", "test", "test"); } catch (Exception e) { // TODO: handle exception } } /** * SQl Server7.0/2000数据库 */ public static void getSqlServerConnection() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") .newInstance(); // mydb 为数据库实例 connection = DriverManager .getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb", "sa", "1234"); } catch (Exception e) { // TODO: handle exception } } /** * Sybase数据库 */ public static void getSybaseConnection() { try { Class.forName("com.sybase.jdbc.SybDriver").newInstance(); // myDB为数据库实例 String url = "jdbc:sybase:Tds:localhost:5007/myDB"; Properties sysProps = System.getProperties(); sysProps.put("user", "userid"); sysProps.put("password", "user_password"); connection = DriverManager.getConnection(url, sysProps); } catch (Exception e) { // TODO: handle exception } } /** * Informix数据库 */ public static void getInformixConnection() { try { Class.forName("com.informix.jdbc.IfxDriver").newInstance(); connection = DriverManager .getConnection("jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword"); } catch (Exception e) { // TODO: handle exception } } /** * MySql数据库 */ public static void getMySqlConnection() { try { // Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager .getConnection("jdbc:mysql://localhost/myDB?user=soft&password=soft&useUnicode=true&characterEncoding=gbk"); } catch (Exception e) { // TODO: handle exception } } /** * PostgreSQL数据库 */ public static void getPostgerSQLConnection() { try { Class.forName("org.postgresql.Driver").newInstance(); connection = DriverManager.getConnection( "jdbc:postgresql://localhost/myDB", "myuser", "mypassword"); } catch (Exception e) { // TODO: handle exception } } /** * access数据库 */ public static void getAccessConnection() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={MicroSoft Access Driver(*.mdb)};DBQ=" + application.getRealPath("/Data/ReportDemo.mdb"); connection = DriverManager.getConnection(url, "", ""); Statement stmtNew = connection.createStatement(); } catch (Exception e) { // TODO: handle exception } }}