MySQL8.0.19 JDBC连接

package dao;

import domain.Stu;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StuDao implements StuDaoIn {


    public List<Stu> findAll() throws Exception {
        List<Stu> stuList = new ArrayList<Stu>();

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2?useSSL=false&serverTimezone=UTC","root","totoro");
        PreparedStatement preparedStatement = connection.prepareCall("select * from stu");
        ResultSet resultSet=preparedStatement.executeQuery();

        while (resultSet.next()){
            Stu stu = new Stu();
            stu.setId(resultSet.getInt("id"));
            stu.setName(resultSet.getString("name"));
            stuList.add(stu);
        }
        connection.close();
        preparedStatement.close();
        resultSet.close();



        return stuList;
    }
}

你可能感兴趣的:(随手记)