此文中的代码主要列出连接数据库的关键代码其他访问数据库代码省略
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);