day05-14-jdbc-demo

package day05.jdbc;

import java.sql.*;

public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // driver驱动类的全名
        String driver ="com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/automatic1";
        String userName = "root";
        String password = "123456";

        //1、加载驱动,这里是反射(新版本已经不需要我们去加载驱动了,它会自动加载,可以不用写了)
        Class.forName(driver);
        //2、建立连接
        Connection connection = DriverManager.getConnection(url, userName, password);
        //这里先打印一下链接,只要不是空就是链接上了
//        System.out.println(connection);
        //3、写sql
        String sql = "select * from auth_permission";
        //4、获取statement
        Statement st = connection.createStatement();
        //5、执行查询
        ResultSet resultSet = st.executeQuery(sql);
        //6、处理结果集
        while (resultSet.next()){
            System.out.println("id:"+resultSet.getLong("id"));//id是int类型,这里用getLong
            System.out.println("name:"+resultSet.getString("name"));//name是String类型,用getString
            System.out.println("content_type_id:"+resultSet.getInt("content_type_id"));
            System.out.println("codename:"+resultSet.getString("codename"));
            System.out.println("===============");

        }
        //7、关闭连接(倒着关)
        resultSet.close();
        st.close();
        connection.close();




    }
}

上一篇:MySQL InnoDB Engine--自适应哈希索引代码瞎猜01


下一篇:什么是单元测试?为什么要做?