Java入门-获取MySQL数据

Java入门-获取MySQL数据

java代码

/**
 * @Author 编程无忧
 * @Date 2022/1/13 13:17
 * @Desc
 */
public class mysqlTest {

    @Test
    public void getMysqlCon(){
        // 声明Connection对象
        Connection con;
        // 驱动程序名
        String driver = "com.mysql.cj.jdbc.Driver";
        // URL指向要访问的数据库名 test
        String url = "jdbc:mysql://localhost:3306/spider?serverTimezone=UTC";
        // MySQL配置时的用户名
        String user = "root";
        // MySQL配置时的密码
        String password = "123456";
        // 遍历查询结果集
        try {
            // 加载驱动程序
            Class.forName(driver);
            // 1.getConnection()方法,连接MySQL数据库!!
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed())
                System.out.println("\n\t\t成功以 " + user + " 身份连接到数据库!!!");

            // 2.创建statement类对象,用来执行SQL语句!!
            Statement statement = con.createStatement();
            // 要执行的SQL语句
            String sql = "select * from data_db";
            // 3.ResultSet类,用来存放获取的结果集!!
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("\n\t\t执行结果如下所示:");
            System.out.println("\t\t-----------------------------------------------------------------");
            System.out.println("\t\t|\t" + "Id" + "\t" + "Url" + "\t" + "Title" + "\t" + "Date\t|");
            System.out.println("\t\t-----------------------------------------------------------------");

            Integer id = null;
            String href = null;
            String title = null;
            //int Age = 0;
            String date = null;

            while (rs.next()) {
                // 获取 ID 这列数据
                //phone_no = rs.getInt("phone_no");
                id = rs.getInt("id");
                // 获取 Name 这列数据
                href = rs.getString("url");
                // 获取 Sex 这列数据
                title = rs.getString("title");
                // 获取 Age 这列数据
                //Age = rs.getInt("Age");
                // 获取 Phone 这列数据
                date = rs.getString("date");

                // 输出结果
                System.out.println("\t\t|\t" + id + "\t" + href + "\t" + title + "\t" + date+ "\t|\t\t");
            }
            System.out.println("\t\t-----------------------------------------------------------------");
            rs.close();
            con.close();
        }
        catch (ClassNotFoundException e) {
            // 数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        }
        catch (SQLException e) {
            // 数据库连接失败异常处理
            e.printStackTrace();
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally {
            System.out.println("\t\t\t\t\t\t\t\t获取数据库数据完毕!!!");
        }
    }
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值