此文中的代碼主要列出連接數據庫的關鍵代碼其他訪問數據庫代碼省略
Oracle/i/i數據庫(thin模式)
ClassforName(oraclejdbcdriverOracleDriver)newInstance();
String url=jdbc:oracle:thin:@localhost::orcl;
//orcl為數據庫的SID
String user=test;
String password=test;
Connection conn= DriverManagergetConnection(urluserpassword);
DB數據庫
ClassforName(comibmdbjdbcappDBDriver )newInstance();
String url=jdbc:db://localhost:/sample;
//sample為你的數據庫名
String user=admin;
String password=;
Connection conn= DriverManagergetConnection(urluserpassword);
Sql Server/數據庫
ClassforName(commicrosoftjdbcsqlserverSQLServerDriver)newInstance();
String url=jdbc:microsoft:sqlserver://localhost:;DatabaseName=mydb;
//mydb為數據庫
String user=sa;
String password=;
Connection conn= DriverManagergetConnection(urluserpassword);
Sybase數據庫
ClassforName(comsybasejdbcSybDriver)newInstance();
String url = jdbc:sybase:Tds:localhost:/myDB;
//myDB為你的數據庫名
Properties sysProps = SystemgetProperties();
SysPropsput(useruserid);
SysPropsput(passworduser_password);
Connection conn= DriverManagergetConnection(url SysProps);
Informix數據庫
ClassforName(rmixjdbcIfxDriver)newInstance();
String url =
jdbc:informixsqli://:/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword;
//myDB為數據庫名
Connection conn= DriverManagergetConnection(url);
MySQL數據庫
ClassforName(orggjtmmmysqlDriver)newInstance();
String url =jdbc:mysql://localhost/myDB?user=soft&password=soft&useUnicode=true&characterEncoding=_
//myDB為數據庫名
Connection conn= DriverManagergetConnection(url);
PostgreSQL數據庫
ClassforName(orgpostgresqlDriver)newInstance();
String url =jdbc:postgresql://localhost/myDB
//myDB為數據庫名
String user=myuser;
String password=mypassword;
Connection conn= DriverManagergetConnection(urluserpassword);
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25826.html