1、下载JDBC,下载地址
https://dev.mysql.com/downloads/connector/j/
3、引入包,jdk是1.8 的,选用jdbc 8.0.18
4、添加源代码,直接选择ZIP包,源代码自动添加,选中Driver,按住ctrl,再点击鼠标左键
5、静态代码块
6、连接数据库代码
public static void main(String[] args) { //DriverManager.getConnection("jdbc:mysql://localhost:3306/netsafe&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false") Connection connection = null; Statement statement= null; ResultSet resultSet = null; //注册驱动 try { //DriverManager.deregisterDriver(new Driver()); Class.forName("com.mysql.cj.jdbc.Driver"); connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/8888?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "root", "root"); statement = connection.createStatement(); String sqlString = "SELECT * from dept"; resultSet = statement.executeQuery(sqlString); while (resultSet.next()) { String deparString = resultSet.getString("DeptNo"); System.out.println(deparString); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("测试34"); }