javaweb中mysql数据库连接方法
来源:互联网 发布:js判断两个日期大小 编辑:程序博客网 时间:2024/05/18 02:11
一、直接连接,不封装到工具类中,主要步骤:
先导包:mysql-connector-java-5.0.8-bin.jar(点击跳转到下载界面),放在WebRoot/WEB-INF/lib/下
1.加载驱动//com.MySQL.jdbc.Driver
2.获取连接 Connection对象
3.获取用于向数据库发送SQL的Statement对象
4.执行sql,获取数据,解析数据
5.关闭连接,释放资源
/* 协议:子协议://主机:端口/数据库名 */ String url = "jdbc:mysql://localhost:3306/jdbctest"; // mysql数据库的用户名与密码,安装时自己设置,一般默认为root String user = "root"; String password = "root"; Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { // 1.加载驱动//com.mysql.jdbc.Driver /* * DriverManager.registerDriver(new * Driver());用这种方法会加载两次驱动,也就是说会创建两个drive对象 */ Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 connection = DriverManager.getConnection(url, user, password); // 3.获取用于向数据库发送SQL的Statement对象 statement = connection.createStatement(); // 4.执行sql,获取数据 resultSet = statement.executeQuery("SELECT * FROM users;"); // 解析数据 while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String psd = resultSet.getString("password"); String email = resultSet.getString("email"); String birthday = resultSet.getString("birthday"); System.out.println(id + " " + name + " " + psd + " " + email + " " + birthday); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //5.关闭连接,释放资源 if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } resultSet = null; } if (statement != null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } statement = null; } if (connection != null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } connection = null; }
二、将数据库连接封装成一个工具类
这样做的好处是,在实际开发中,就能做到,改一处即可修改全局。
1.建一个名为db.properties的配置文件,放于src/
[html] view plain copy url=jdbc:mysql://localhost:3306/jdbctest username=root password=root driver=com.mysql.jdbc.Driver
2.工具类:
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JdbcUtil { //私有静态变量,用以读取配置文件 private static Properties config=new Properties(); static{ try { //配置资源文件 config.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties")); //加载驱动 Class.forName(config.getProperty("driver")); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection(){ Connection connection=null; try { connection=DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password")); } catch (SQLException e) { e.printStackTrace(); } return connection; } //用以关闭连接,释放资源 public static void releaseConn(Connection connection, Statement statement, ResultSet resultSet) { if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } resultSet=null; } if(statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } statement=null; } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection=null; } } }
3.使用实例:
Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { // 调用工具类中的静态方法来获取连接 connection = JdbcUtil.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from users"); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String psd = resultSet.getString("password"); String email = resultSet.getString("email"); String birthday = resultSet.getString("birthday"); System.out.println(id + " " + name + " " + psd + " " + email + " " + birthday); } } catch (Exception e) { e.printStackTrace(); } finally { // 调用工具类中的静态方法来关闭连接,释放资源 JdbcUtil.releaseConn(connection, statement, resultSet); }
来源:http://blog.csdn.net/beauxie/article/details/52748051#
0 0
- javaweb中mysql数据库连接方法
- javaweb中mysql数据库连接方法
- C#中MySql数据库连接
- 在glassfish v2中配置MySql数据库连接池都方法
- php中mysql数据库连接的三种常用的方法
- javaweb JDBC 数据库连接mysql 配置代码 (直接可用)
- eclipse中数据库连接mysql配置
- FineReport中Domino数据库连接方法
- JavaWeb之JDBC(数据库连接)
- javaweb获取数据库连接
- javaweb之数据库连接池
- VC 与MySQL数据库连接的方法
- MySQL加载 数据库连接的两个方法:
- javaweb连接mysql的两种方法
- MySql 中Java的数据库连接字符串
- Java中通过配置文件创建Mysql数据库连接
- C#中MYSQL数据库连接并显示
- node.js中mysql数据库连接池
- 获取坐标的总结
- 机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾
- 我的博客网站重写历程--1.引言:说在前面的话与暂定开发进程
- 九度 oj 题目1038:Sum of Factorials
- 机房那些事儿---配置ODBC问题集锦
- javaweb中mysql数据库连接方法
- SpringMVC数据验证——第七章 注解式控制器的数据验证、类型转换及格式化
- Python变长参数,参数解包
- android_app开发微信支付集成
- 文章标题
- 使用pull解析器解析和生成xml文件
- spring-boot整合websocket
- 安全终极:读取人类大脑
- 机器学习系列(4)_机器学习算法一览,应用建议与解决思路