芯が強い人になるESTJ-A

# JDBC利用反射版本

IT開発 Tags: 无标签 阅读: 335
package com.betsy.pojo;

import java.sql.*;

/**
 * author:xuwen
 * Created on 2021/7/20
 */
public class Testjdbc {
    //main
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        String url = "jdbc:mysql://rm-uf6t9m0yx22dxrya5no.mysql.rds.aliyuncs.com:3306/koa2weibo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF8&useSSL=false";
        String username = "koa2weibo";
        String password = "passMYblog123";

        //第一步:加载驱动
        //反射带上全包名
        Class.forName("com.mysql.jdbc.Driver");

        //第二步:连接数据库
        Connection connection = DriverManager.getConnection(url,username,password);

        //第三步:向数据库发送SQL的对象Statement
        Statement statement = connection.createStatement();

        //第四步:SQL
        String sql = "select * from users";

        //执行,返回一个ResultSet
        ResultSet res = statement.executeQuery(sql);
        while(res.next()){
            System.out.println("id="+res.getObject("id"));
            System.out.println("name="+res.getObject("name"));
            System.out.println("password="+res.getObject("password"));
            System.out.println("email="+res.getObject("email"));
            System.out.println("birthday="+res.getObject("birthday"));
            System.out.println("===================");
        }
        //6.关闭连接,释放资源,先开的后关
        res.close();
        statement.close();
        connection.close();
    }


}

截屏2021-07-20 14.50.47.jpg