• JDBC的初步认识


    JDBC是什么?JDBC英文名为:Java Data Base Connectivity(Java数据库连接),数据库是由不同生产产商决定的,例如Mysql、Oracle、SQL Server,JAVA JDK不可能提供对不同数据库的实现,因此,它就提供了JDBC的接口API,具体的实现由不同的生产产商决定。这样,数据库生产产商都根据JAVA API去实现各自的应用驱动,这问题就迎刃而解了

    常用接口

    • DriverManager:这个类管理数据库驱动程序的列表,查看加载的驱动是否符合JAVA Driver API的规范

    • Connection:与数据库中的连接对象,通过该对象与数据库进行通信

    • Statement:把创建的SQL对象,转而存储到数据库当中

    • ResultSet:它是一个迭代器,用于检索查询数据

    JDBC的使用步骤

    1. import cn.kgc.jdbc.entity.User;
    2. import com.mysql.jdbc.Driver;
    3. import java.sql.*;
    4. import java.util.ArrayList;
    5. public class JDBCDemo01 {
    6. /*
    7. java操作数据库的基本步骤
    8. 1. 引入依赖 注册驱动
    9. 2.获取连接对象 如何获取数据库的连接?
    10. 3.获取发送sql语句的对象 发送sql语句
    11. 4.处理结果集
    12. 5.释放资源
    13. * */
    14. public static void main(String[] args) throws SQLException {
    15. // 创建驱动对象
    16. Driver driver = new Driver();
    17. // 注册驱动
    18. DriverManager.registerDriver(driver);
    19. /**
    20. * 获取连接 url user password http://www.baidu.com/login.html
    21. * url: 数据库的通信地址 jdbc:mysql://localhost:3306/java2216
    22. * user: 数据的用户名 root
    23. * password: 用户密码
    24. */
    25. String url = "jdbc:mysql://localhost:3306/java2216?useSSL=false";
    26. String username = "root";
    27. String password = "***";
    28. //获取数据库的连接
    29. Connection connection = DriverManager.getConnection(url, username, password);
    30. System.out.println("connection = " + connection);
    31. // 通过连接获取发送sql语句的对象 Statement
    32. Statement statement = connection.createStatement();
    33. //编写sql
    34. //String sql = "SELECT * FROM user WHERE id = 1";
    35. String sql = "SELECT * FROM user ";
    36. // 发送sql语句到mysql数据库
    37. ResultSet resultSet = statement.executeQuery(sql);
    38. // 处理结果集 resultSet resultSet.next() 查询数据结果集中是否还有下一条数据
    39. // 创建集合 存储查询结果中封装的数据
    40. ArrayList users = new ArrayList<>();
    41. while (resultSet.next()){
    42. //获取当前记录中字段值 根据字段值的数据类型获取
    43. int id = resultSet.getInt("id"); // 通过字段名获取
    44. String uname = resultSet.getString(2); // 通过字段的列索引
    45. String pwd = resultSet.getString("password");
    46. User user = new User().setId(id).setPassword(pwd).setUsername(uname);
    47. users.add(user);
    48. //System.out.println("user = " + user);
    49. }
    50. // 遍历集合
    51. //users.forEach(user-> System.out.println("user = " + user));
    52. for (User user : users) {
    53. System.out.println(user);
    54. }
    55. // 释放资源
    56. resultSet.close();
    57. statement.close();
    58. connection.close();
    59. }
    60. }

     测试JDBC

    1. package cn.kgc.jdbc;
    2. import com.mysql.jdbc.Driver;
    3. import org.junit.Test;
    4. import java.sql.Connection;
    5. import java.sql.DriverManager;
    6. import java.sql.SQLException;
    7. import java.sql.Statement;
    8. public class TestJDBC {
    9. // jdbc 添加操作
    10. @Test
    11. public void addUser(){
    12. // jdbc 6步
    13. Connection connection =null;
    14. Statement statement = null;
    15. try {
    16. Driver driver = new Driver();
    17. DriverManager.registerDriver(driver);
    18. String url = "jdbc:mysql://localhost:3306/java2216?useSSL=false";
    19. String username = "root";
    20. String password = "***";
    21. connection = DriverManager.getConnection(url, username, password);
    22. statement = connection.createStatement();
    23. String sql = "insert into user(username,password) values('tom','12345')";
    24. int i = statement.executeUpdate(sql);
    25. System.out.println(i>0?"ok":"error");
    26. } catch (SQLException throwables) {
    27. throwables.printStackTrace();
    28. }finally {
    29. // 释放资源
    30. try {
    31. if (statement!=null){ // 避免空指针异常
    32. statement.close();
    33. }
    34. } catch (SQLException throwables) {
    35. throwables.printStackTrace();
    36. }
    37. try {
    38. if ( connection!=null){ // 避免空指针异常
    39. connection.close();
    40. }
    41. } catch (SQLException throwables) {
    42. throwables.printStackTrace();
    43. }
    44. }
    45. }
    46. // jdbc 删除操作
    47. @Test
    48. public void deleteUser(){
    49. // jdbc 6步
    50. Connection connection =null;
    51. Statement statement = null;
    52. try {
    53. Driver driver = new Driver();
    54. DriverManager.registerDriver(driver);
    55. String url = "jdbc:mysql://localhost:3306/java2216?useSSL=false";
    56. String username = "root";
    57. String password = "***";
    58. connection = DriverManager.getConnection(url, username, password);
    59. statement = connection.createStatement();
    60. String sql = " delete from user where id = 1";
    61. int i = statement.executeUpdate(sql);
    62. System.out.println(i>0?"ok":"error");
    63. } catch (SQLException throwables) {
    64. throwables.printStackTrace();
    65. }finally {
    66. // 释放资源
    67. try {
    68. if (statement!=null){ // 避免空指针异常
    69. statement.close();
    70. }
    71. } catch (SQLException throwables) {
    72. throwables.printStackTrace();
    73. }
    74. try {
    75. if ( connection!=null){ // 避免空指针异常
    76. connection.close();
    77. }
    78. } catch (SQLException throwables) {
    79. throwables.printStackTrace();
    80. }
    81. }
    82. }
    83. // jdbc 修改操作
    84. @Test
    85. public void updateUser(){
    86. // jdbc 6步
    87. Connection connection =null;
    88. Statement statement = null;
    89. try {
    90. Driver driver = new Driver();
    91. DriverManager.registerDriver(driver);
    92. String url = "jdbc:mysql://localhost:3306/java2216?useSSL=false";
    93. String username = "root";
    94. String password = "***";
    95. connection = DriverManager.getConnection(url, username, password);
    96. statement = connection.createStatement();
    97. String sql = "update user set password = '8888' where id = 2";
    98. int i = statement.executeUpdate(sql);
    99. System.out.println(i>0?"ok":"error");
    100. } catch (SQLException throwables) {
    101. throwables.printStackTrace();
    102. }finally {
    103. // 释放资源
    104. try {
    105. if (statement!=null){ // 避免空指针异常
    106. statement.close();
    107. }
    108. } catch (SQLException throwables) {
    109. throwables.printStackTrace();
    110. }
    111. try {
    112. if ( connection!=null){ // 避免空指针异常
    113. connection.close();
    114. }
    115. } catch (SQLException throwables) {
    116. throwables.printStackTrace();
    117. }
    118. }
    119. }
    120. }

    JDBC工具类

    在IDEA中建立resource资源建立jdbc.properties文件

    1. driverClassName=com.mysql.jdbc.Driver
    2. url=jdbc:mysql://localhost:3306/java2216?useSSL=false
    3. username=root
    4. password=***

    使用JDBC的步骤中连接数据库和释放资源重复性使用,可以提取出来单独设立JDBCUtil工具类

    1. import com.mysql.jdbc.Driver;
    2. import javax.sql.DataSource;
    3. import java.sql.*;
    4. import java.util.ResourceBundle;
    5. public class JDBCUtil {
    6. private JDBCUtil(){}
    7. private static String username;
    8. private static String driverClassName;
    9. private static String password;
    10. private static String url;
    11. static { // 执行一次 类加载时
    12. ResourceBundle file = ResourceBundle.getBundle("jdbc");
    13. driverClassName = file.getString("driverClassName");
    14. url = file.getString("url");
    15. username = file.getString("username");
    16. password = file.getString("password");
    17. }
    18. //获取数据库连接的方法
    19. public static Connection getConnection(){
    20. //通过反射注册驱动
    21. Connection connection = null;
    22. try {
    23. Class.forName(driverClassName);
    24. connection = DriverManager.getConnection(url, username, password);
    25. } catch (Exception e) {
    26. e.printStackTrace();
    27. }
    28. return connection;
    29. }
    30. //释放资源
    31. public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
    32. if(resultSet!=null){
    33. try {
    34. resultSet.close();
    35. } catch (SQLException throwables) {
    36. throwables.printStackTrace();
    37. }
    38. }
    39. if(statement!=null){
    40. try {
    41. statement.close();
    42. } catch (SQLException throwables) {
    43. throwables.printStackTrace();
    44. }
    45. }
    46. if(connection!=null){
    47. try {
    48. connection.close();
    49. } catch (SQLException throwables) {
    50. throwables.printStackTrace();
    51. }
    52. }
    53. }
    54. }
    1. package cn.kgc.jdbc;
    2. import cn.kgc.jdbc.util.JDBCUtil;
    3. import org.junit.Test;
    4. import java.sql.Connection;
    5. import java.sql.SQLException;
    6. public class TestJDBCUtil {
    7. @Test
    8. public void test1(){ // 测试工具类获取连接的方法
    9. Connection connection = JDBCUtil.getConnection();
    10. System.out.println("connection = " + connection);
    11. }
    12. }

  • 相关阅读:
    SpringCloud链路追踪SkyWalking-第六章-日志采集
    springboot 导出word模板
    Go for循环中的defer
    显示今天的年、月、日日期、时间的数据处理timetuple()
    C++ 运算符重载详解
    【虚拟化生态平台】虚拟化平台搭建
    一幅长文细学HTML5和CSS3——一幅长文系列
    Linux 系统服务日志查询 journalctl:查询 systemd 日记
    Elasticsearch系列之:Centos7安装部署Elasticsearch详细步骤
    CSS的伪类选择器:nth-child()
  • 原文地址:https://blog.csdn.net/qq_69761234/article/details/126607109