基本连接语句
教程 https://www.bilibili.com/video/BV1pw41147jm
导jar包 https://blog.csdn.net/qq_40893824/article/details/129118784
import java.sql.*;public class test {public static void main(String[] args) throws SQLException {//用户信息和urlString url = "jdbc:mysql://localhost:3306/test";String username = "root"; //数据库用户名String password = "123456";//数据库密码Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {//获取数据库连接connection = DriverManager.getConnection(url, username, password);//操作数据库statement = connection.createStatement();//获取操作数据库的对象String sql = "select * from EMP";resultSet = statement.executeQuery(sql);//执行sql,获取结果集while (resultSet.next()) {//遍历结果集,取出数据String name = resultSet.getString("name");String sex = resultSet.getString("sex");String age = resultSet.getString("age");//输出数据System.out.print("姓名:" + name);System.out.print("性别:" + sex);System.out.print("年龄" + age);System.out.println();}} catch (SQLException e) {throw new RuntimeException(e);} finally {resultSet.close();statement.close();connection.close();}}
}