• Java连接数据库并查询表中的全部数据


    1、导入相关jar包

    这里创建简单的maven项目,我们导入相关的jar包
    相关依赖:

            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.47version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、连接数据库,执行查询语句

    package JdbcTest;
    import java.sql.*;
    
    public class DataJdbcTest {
        public static void main(String[] args) {
            Connection connection = null;
            try {
                // 加载MySQL JDBC驱动程序  
                Class.forName("com.mysql.jdbc.Driver");
    
                // 连接数据库  
                String url = "jdbc:mysql://localhost:3306/cloud_user";
                String username = "root";
                String password = "123456";
                connection = DriverManager.getConnection(url, username, password);
    
                // 连接成功后输出一条消息  
                System.out.println("成功连接到数据库!");
    
                // 在这里可以执行SQL查询和其他数据库操作...
                //1.创建Statement对象
                Statement statement = connection.createStatement();
                //2.执行查询语句
                String sql = "select * from tb_user";
                ResultSet resultSet = statement.executeQuery(sql);
    
                //3.处理查询结果
                while (resultSet.next()) {
                    // 读取结果集中的数据
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("username");
                    String address = resultSet.getString("address");
                    System.out.println("id= "+ id + "username= "+ name + "address= "+address);
                }
    
    
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("无法加载MySQL JDBC驱动程序");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("无法连接到数据库");
            } finally {
                // 关闭连接前输出一条消息  
                System.out.println("正在关闭数据库连接...");
    
                // 关闭连接  
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("数据库连接已关闭!");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    运行结果:
    在这里插入图片描述

  • 相关阅读:
    CSDN编程挑战赛第六期—参赛心得+题解
    数字图像处理—— 实验五 基于图像分割的车牌定位识别
    VR数字展厅在企业中应用的优势有哪些?
    【prometheus上报和使用】
    Git安装使用gitee(码云)记录2208201631
    简单的小复习(一)
    Linux学习-36-文件系统管理-硬盘结构
    MAX9295配置说明
    idea启动vue项目:Error:0308010C:digital envelope routines::unsupported
    计算神经科学和人工智能,人工智能神经网络算法
  • 原文地址:https://blog.csdn.net/N16696796429/article/details/134278283