1、代码
import java.sql.*;
public class DBUtil {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc", "root", "123");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select user_name, age from imooc_gaddess");
while (resultSet.next()){
System.out.println(resultSet.getString("user_name") + "年龄:" + resultSet.getInt("age"));
}
}
}
2、依赖
![](https://img2018.cnblogs.com/common/1178951/201912/1178951-20191208143323973-1893674052.png)
3、数据库
![](https://img2018.cnblogs.com/common/1178951/201912/1178951-20191208143437691-1511325014.png)
![](https://img2018.cnblogs.com/common/1178951/201912/1178951-20191208142919942-1352127225.png)
4、运行结果
![](https://img2018.cnblogs.com/common/1178951/201912/1178951-20191208143236684-1330598811.png)