JDBC连接数据库

1.

 1 public void testDriver() throws SQLException{
 2          //    创建Driver实现类对象    
 3          Driver driver = new com.mysql.jdbc.Driver();
 4          
 5          // 准备连接数据库的基本信息
 6          String url = "jdbc:mysql://localhost:3306/ksy";
 7          Properties info = new Properties();
 8          info.put("user", "root");
 9          info.put("password","");
10          
11          // 调用Driver接口的connect(url, info)来回去数据库连接
12          Connection conn = (Connection) driver.connect(url, info);
13          
14          
15          // com.mysql.jdbc.JDBC4Connection@16290fbc
16          System.out.println(conn);
17     }
18     

2.

 1 /**
 2      * 编写通风用的方法,不修改源程序的情况下,可以获取任何数据库的连接
 3      * 解决方案: 把数据库驱动Driver实现类的全类名、url、user、password放在一个配置文件中
 4      *         通过修改配置稳健的方式实现和具体数据库解耦
 5      * @throws Exception 
 6      */
 7     public Connection getConnection() throws Exception {
 8         String driverClass = null;
 9         String jdbcUrl = null;
10         String user = null;
11         String password = null;
12         
13         // 读取类路径下的文件
14         InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
15         Properties properties = new Properties();
16         properties.load(is);
17         
18         driverClass = properties.getProperty("driverClass");        
19         jdbcUrl = properties.getProperty("jdbcUrl");
20         user = properties.getProperty("user");
21         password = properties.getProperty("password");
22         
23         Driver driver = (Driver) Class.forName(driverClass).newInstance();
24         
25         Properties info = new Properties();
26         info.put("user", user);
27         info.put("password", password);
28         
29         Connection conn = (Connection) driver.connect(jdbcUrl, info);
30         return conn;
31     }
32      
33     public void testGetConnection() throws Exception {
34         // com.mysql.jdbc.JDBC4Connection@2f833eca
35         System.out.println( getConnection());
36     }

3.

1 driverClass = com.mysql.jdbc.Driver
2 jdbcUrl = jdbc:mysql://localhost:3306/ksy
3 user = root
4 password = 

4.

 1 /**
 2      * DriverManage是驱动的管理类
 3      * @throws Exception 
 4      * 
 5      */
 6      
 7     public void testDriverManage() throws Exception {
 8         //驱动的全类名
 9         String driverClass = null;
10         String jdbcUrl = null;
11         String user = null;
12         String password = null;
13         
14         // 读取类路径下的文件
15         InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
16         Properties properties = new Properties();
17         properties.load(is);
18         
19         driverClass = properties.getProperty("driverClass");        
20         jdbcUrl = properties.getProperty("jdbcUrl");
21         user = properties.getProperty("user");
22         password = properties.getProperty("password");
23         
24         // 加载驱动程序(对应的Driver实现类中有注册驱动的静态代码块)
25         Class.forName(driverClass);
26         
27         // 获取数据库连接
28         Connection conn = (Connection) DriverManager.getConnection(jdbcUrl, user, password);
29         System.out.println(conn);
30     }

5.

 1     /**
 2      * 使用DBCP数据库连接池
 3      * @throws Exception 
 4      */
 5     public void testDBCP() throws Exception {
 6         BasicDataSource dataSource = null;
 7         // 创建DBCP数据源实例
 8         dataSource = new BasicDataSource();
 9         
10         // 为数据源实例指定属性
11         dataSource.setUsername("root");
12         dataSource.setPassword("");
13         dataSource.setUrl("jdbc:mysql:///ksy");
14         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
15         
16         // 指定数据源的一些属性
17         // 数据库连接池中初始化连接数
18         dataSource.setInitialSize(5);
19         
20         // 同一时刻向数据库申请的最大连接数
21         dataSource.setMaxActive(5);
22         
23         // 在数据库连接中保存的最少的空闲连接数
24         dataSource.setMinIdle(10);
25         
26         // 等待连接池分配的最长时间
27         dataSource.setMaxWait(1000);
28     
29         // 从数据源中获取数据库连接
30         // jdbc:mysql:///ksy, UserName=root@, MySQL Connector Java
31         System.out.println(dataSource.getConnection());
32         
33         // class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
34         System.out.println(dataSource.getConnection().getClass());
35     }
36     
37     

6.

 1 public void testDBCPWithDataSourceFactory() throws Exception {
 2         
 3         // 加载配置文件
 4         Properties properties = new Properties();
 5         InputStream is = getClass().getClassLoader().getResourceAsStream("dbcp.properties");
 6         properties.load(is);
 7         
 8         // 通过BasicDataSourceFactory.createDataSource(properties)
 9         // 创建dataSource实例
10         final DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
11         
12         // BasicDataSource dasicDataSource = (BasicDataSource) dataSource;
13         // 5
14         // System.out.println(dasicDataSource.getMaxActive());
15         
16         // 从dataSource中获取实例连接
17         // jdbc:mysql://localhost:3306/ksy, UserName=root@, MySQL Connector Java
18          System.out.println(dataSource.getConnection().getClass());
19 }

7.

1 username = root
2 password = 
3 driverClass = com.mysql.jdbc.Driver
4 url = jdbc:mysql://localhost:3306/ksy
5 initialSize = 5
6 maxActive = 5
7 minIdle = 5
8 maxWait = 5000

8.

 1 public void testC3p0() throws Exception {
 2         ComboPooledDataSource cpds = new ComboPooledDataSource();
 3         cpds.setDriverClass("com.mysql.jdbc.Driver");
 4         cpds.setJdbcUrl("jdbc:mysql://localhost:3306/ksy");
 5         cpds.setUser("root");
 6         cpds.setPassword("");
 7         // com.mchange.v2.c3p0.impl.NewProxyConnection@5ddc0e7a
 8         System.out.println(cpds.getConnection());
 9     }
10     
11     

9.

1 public void testC3p0WithConfigFile() throws SQLException {
2         DataSource dataSource = new ComboPooledDataSource("helloc3p0");
3         // com.mchange.v2.c3p0.impl.NewProxyConnection@11e9c82e        
4         System.out.println(dataSource.getConnection());
5     }

 

posted @ 2018-10-28 22:59  KSYOON  阅读(156)  评论(0编辑  收藏  举报