JDBC連接各大數據庫

**1.Oracle數據庫(thin模式)**
Class.forName("oracle.jdbc.driver.OracleDriver");//數據庫驅動字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";//連接URL字符串   orcl爲數據庫的SID
String user="scott";//數據庫用戶名
String password="tiger";//數據庫密碼
Connection conn=DriverManager.getConnection(url,user,password);

**2.DB2數據庫**
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");//數據庫驅動字符串
String url="jdbc:db2://localhost:5000/sample";//連接URL字符串    sample爲數據庫的名字
String user="admin";//數據庫用戶名
String password="admin";//數據庫密碼
Connection conn=DriverManager.getConnection(url,user,password);

**3.MySQL數據庫**
Class.forName("com.mysql.Driver");//數據庫驅動字符串
String url="jdbc:mysql://localhost:3306/myDB";//連接URL字符串    myDB爲數據庫的名字
String user="root";//數據庫用戶名
String password="root";//數據庫密碼
Connection conn=DriverManager.getConnection(url,user,password);

**4.SQL Server數據庫**
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//數據庫驅動字符串
String url="jdbc:sqlserver://localhost:1433;DatabaseName=epet";//連接URL字符串    epet爲數據庫的名字
String user="sa";//數據庫用戶名
String password="sa";//數據庫密碼
Connection conn=DriverManager.getConnection(url,user,password);

**5.H2數據庫**
Class.forName("org.h2.Driver");//數據庫驅動字符串
String url="jdbc:h2:~/test";//連接URL字符串   test爲數據庫的名字
String user="sa";//數據庫用戶名
String password="sa";//數據庫密碼
Connection conn=DriverManager.getConnection(url,user,password);

**6.PostgreSQL數據庫**
Class.forName("org.postgresql.Driver");//數據庫驅動字符串
String url="jdbc:postgresql://localhost:5432/testdb";//連接URL字符串   testdb爲數據庫的名字
String user="postgresql";//數據庫用戶名
String password="postgresql";//數據庫密碼
Connection conn=DriverManager.getConnection(url,user,password);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章