Java连接各种数据库的配置

1.连接MySQL数据库
驱动包:mysql-connector-java-5.1.18-bin.jar
驱动程序名:com.mysql.jdbc.Driver
连接字符串:jdbc:mysql://localhost:3306/database

Java连接各种数据库的配置
 1 package com.chenshuyi.dbsummary;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 import org.junit.Test;
 7 
 8 import junit.framework.TestCase;
 9 
10 public class MySQL extends TestCase {
11     
12     @Test public void testConnectToMySQL()
13     {
14         String driver = "com.mysql.jdbc.Driver";
15         String url = "jdbc:mysql://127.0.0.1:3306/chanjiayi";
16         String user = "root"; 
17         String password = "sa";
18         try{ 
19             Class.forName(driver);
20              Connection conn = DriverManager.getConnection(url, user, password);
21              
22              if(conn != null)
23              {
24                  System.out.println("get Connection SUCCESS !");
25              }
26         }
27         catch(Exception e)
28         {
29             e.printStackTrace();
30         }
31     }
32 }
View Code

 

2.连接SQLServer数据库
驱动包:sqljdbc.jar 或 sqljdbc4.jar
驱动程序名:com.microsoft.jdbc.sqlserver.SQLServerDriver
连接字符串:jdbc:sqlserver://localhost:1433;DatabaseName=mydb

Java连接各种数据库的配置
 1 package com.chenshuyi.dbsummary;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 import org.junit.Test;
 7 
 8 
 9 public class SQLServer {
10     
11     @Test public void testConnectToMySQL()
12     {
13         String driver = "com.mysql.jdbc.Driver";
14         String url = "jdbc:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库名
15         String user = "sa"; 
16         String password = "sa";
17         try{ 
18             Class.forName(driver);
19              Connection conn = DriverManager.getConnection(url, user, password);
20              
21              if(conn != null)
22              {
23                  System.out.println("get Connection SUCCESS !");
24              }
25         }
26         catch(Exception e)
27         {
28             e.printStackTrace();
29         }
30     }
31 }
View Code

 

3.连接Oracle数据库
驱动包:ojdbc4.jar
驱动程序名:oracle.jdbc.driver.OracleDriver
连接字符串:jdbc:oracle:thin:@127.0.0.1:1521:DB (DB为要连接的数据库名)

Java连接各种数据库的配置
 1 package com.chenshuyi.dbsummary;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 import org.junit.Test;
 7 
 8 
 9 public class Oracle {
10     
11     @Test public void testConnectToMySQL()
12     {
13         String driver = "oracle.jdbc.driver.OracleDriver";
14         String url = "jdbc:oracle:thin:@127.0.0.1:1521:DBSummary";
15         String user = "system"; 
16         String password = "sa";
17         try{ 
18             Class.forName(driver);
19              Connection conn = DriverManager.getConnection(url, user, password);
20              
21              if(conn != null)
22              {
23                  System.out.println("GET Connection SUCCESS !");
24              }
25         }
26         catch(Exception e)
27         {
28             e.printStackTrace();
29         }
30     }
31 }
View Code

 注:其中ojdbc4.jar可以在oracle安装目录下找到(例如我的就在:E:\oracle\product\10.2.0\db_1\jdbc\lib)

Java连接各种数据库的配置,布布扣,bubuko.com

Java连接各种数据库的配置

上一篇:安装Oracle 10g RAC是否需要安装HACMP


下一篇:Centos 6.3 编译安装Nginx+php+Mysql