各种数据库连接的例子
程序员文章站
2022-06-04 11:06:38
...
1.Oracle(用thin模式)数据库
驱动:"oracle.jdbc.driver.OracleDriver";
Url:jdbc:oracle:thin:@<host>:<port>:<SID>;
String UserName = "username"; //用户名
String Password = "password"; //密码
Class.forName(驱动).newInstance();
connection con = DriverManager.getConnection(Url,Username,Password);
2.DB2
驱动:"com.ibm.db2.jcc.DB2Driver";
Url:"jdbc:db2://<host>:<port>/<database_name>";
String UserName = "username";
String Password = "password";
Class.forName(驱动).newInstance();
connection con = DriverManager.getConnection(Url,UserName,Password);
3.MySQL数据库
驱动:"com.mysql.jdbc.Driver";
Url:"jdbc:mysql://<host>:<port>/<database_name>";
String UserName="username";
String Password="password";
Class.forName(驱动).newInstance(); //加载数据库驱动
connection con = DriverManager.getConnection(Url,Username,Password);
4.Microsoft SQL server 数据库
SQL Server 2000:
驱动: "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Url:"jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=sample";
SQL Server 2005:
驱动: "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Url:"jdbc:sqlserver://<host>:<port>;databaseName=<database_name>";
String UserName="username";
String Password="password";
Class.forName(驱动).newInstance();
connection con=DriverManager.getConnection(Url,Username,Password);