码迷,mamicode.com
首页 > 数据库 > 详细

web项目与数据库连接方法

时间:2018-04-16 21:58:27      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:name   端口   方法   ted   oid   print   trace   creates   静态变量   

一、直接连接,不封装到工具类中,主要步骤:

先导包:mysql-connector-java-5.1.46-bin.jar(点击跳转到下载界面),放在WebRoot/WEB-INF/lib/下

  1.加载驱动//com.mysql.jdbc.Driver

  2.获取连接 Connection对象

  3.获取用于向数据库发送SQL的Statement对象
  
  4.执行sql,获取数据,解析数据
  
  5.关闭连接,释放资源

 1            /* 协议:子协议://主机:端口/数据库名 */  
 2 String url = "jdbc:mysql://localhost:3306/jdbctest";  
 3   
 4 // mysql数据库的用户名与密码,安装时自己设置,一般默认为root  
 5 String user = "root";  
 6 String password = "root";  
 7   
 8 Connection connection = null;  
 9 Statement statement = null;  
10 ResultSet resultSet = null;  
11 try {  
12     // 1.加载驱动//com.mysql.jdbc.Driver  
13     /* 
14      * DriverManager.registerDriver(new 
15      * Driver());用这种方法会加载两次驱动,也就是说会创建两个drive对象 
16      */  
17     Class.forName("com.mysql.jdbc.Driver");  
18     // 2.获取连接  
19     connection = DriverManager.getConnection(url, user, password);  
20   
21     // 3.获取用于向数据库发送SQL的Statement对象  
22     statement = connection.createStatement();  
23   
24     // 4.执行sql,获取数据  
25     resultSet = statement.executeQuery("SELECT * FROM users;");  
26   
27     // 解析数据  
28     while (resultSet.next()) {  
29         int id = resultSet.getInt("id");  
30         String name = resultSet.getString("name");  
31         String psd = resultSet.getString("password");  
32         String email = resultSet.getString("email");  
33         String birthday = resultSet.getString("birthday");  
34   
35         System.out.println(id + " " + name + " " + psd + " " + email  
36                 + " " + birthday);  
37     }  
38 } catch (ClassNotFoundException e) {  
39     e.printStackTrace();  
40 } catch (SQLException e) {  
41     e.printStackTrace();  
42 } finally {    
43   
44                       //5.关闭连接,释放资源  
45     if (resultSet != null) {  
46         try {  
47             resultSet.close();  
48         } catch (SQLException e) {  
49             // TODO Auto-generated catch block  
50             e.printStackTrace();  
51         }  
52         resultSet = null;  
53     }  
54   
55     if (statement != null) {  
56         try {  
57             statement.close();  
58         } catch (SQLException e) {  
59             // TODO Auto-generated catch block  
60             e.printStackTrace();  
61         }  
62         statement = null;  
63     }  
64   
65     if (connection != null) {  
66         try {  
67             connection.close();  
68         } catch (SQLException e) {  
69             // TODO Auto-generated catch block  
70             e.printStackTrace();  
71         }  
72         connection = null;  
73     }  
74 } 

 

二、将数据库连接封装成一个工具类

这样做的好处是,在实际开发中,就能做到,改一处即可修改全局。

1.建一个名为db.properties的配置文件,放于src/

1 url=jdbc:mysql://localhost:3306/jdbctest  
2 username=root  
3 password=root  
4 driver=com.mysql.jdbc.Driver

2.工具类:

 1 import java.io.IOException;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 import java.util.Properties;
 8 
 9 public class JdbcUtil {
10     
11     //私有静态变量,用以读取配置文件
12     private static Properties config=new Properties();
13     
14     static{
15         try {
16             //配置资源文件
17             config.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
18             
19             //加载驱动
20             Class.forName(config.getProperty("driver"));
21         } catch (IOException e) {
22             e.printStackTrace();
23         } catch (ClassNotFoundException e) {
24             e.printStackTrace();
25         }
26     }
27     
28     public static  Connection getConnection(){
29         Connection connection=null;
30         try {
31             connection=DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password"));
32         } catch (SQLException e) {
33             e.printStackTrace();
34         }
35         
36         return connection;
37     }
38     //用以关闭连接,释放资源
39     public static void releaseConn(Connection connection, Statement statement,
40             ResultSet resultSet) {
41         if(resultSet!=null){
42             try {
43                 resultSet.close();
44             } catch (SQLException e) {
45                 e.printStackTrace();
46             }
47             resultSet=null;
48         }
49         
50         if(statement!=null){
51             try {
52                 statement.close();
53             } catch (SQLException e) {
54                 e.printStackTrace();
55             }
56             statement=null;
57         }
58         
59         if(connection!=null){
60             try {
61                 connection.close();
62             } catch (SQLException e) {
63                 e.printStackTrace();
64             }
65             connection=null;
66         }
67     }
68     
69 
70 }

3.使用实例:

 1         Connection connection = null;
 2         Statement statement = null;
 3         ResultSet resultSet = null;
 4         try {
 5             // 调用工具类中的静态方法来获取连接
 6             connection = JdbcUtil.getConnection();
 7             statement = connection.createStatement();
 8             resultSet = statement.executeQuery("select * from users");
 9             while (resultSet.next()) {
10                 int id = resultSet.getInt("id");
11                 String name = resultSet.getString("name");
12                 String psd = resultSet.getString("password");
13                 String email = resultSet.getString("email");
14                 String birthday = resultSet.getString("birthday");
15 
16                 System.out.println(id + " " + name + " " + psd + " " + email
17                         + " " + birthday);
18 
19             }
20         } catch (Exception e) {
21             e.printStackTrace();
22         } finally {
23             // 调用工具类中的静态方法来关闭连接,释放资源
24             JdbcUtil.releaseConn(connection, statement, resultSet);
25         }
26     }

 

web项目与数据库连接方法

标签:name   端口   方法   ted   oid   print   trace   creates   静态变量   

原文地址:https://www.cnblogs.com/superslow/p/8858713.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!