Solve the mysql8.0. 15 configuration problem of javaweb in idea

  • 2021-07-26 07:46:30
  • OfStack

mysql after 8.0. x is somewhat different when connecting to the database.

First:

Class.forName("com.mysql.cj.jdbc.Driver");

Secondly:


DriverManager.getConnection("jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC", "root", "passwd");

In addition, it is worth noting that the javaweb project created in idea lacks 1 folder, such as an important lib folder (I don't know if I don't have it), and then the jar driver package can be used normally in lib.

Finally, put a complete code for reference.


<%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>jdbc</title>
</head>
<body>
<%
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  int age = -1;
  String name = null;
  int id = -1;
  try{
    Class.forName("com.mysql.cj.jdbc.Driver");
  }catch (Exception e){
    out.print(" Connection failed ");
  }
  try{
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC", "root", "1");
    out.print(" Connection succeeded ");
    ps = conn.prepareStatement("select * from name ");
    rs = ps.executeQuery();
    while (rs.next()){
      age = rs.getInt("age");
      name = rs.getString("name");
      out.print("<br>" + "age:" + age + " "+ "name:" + name);
    }
  }catch (SQLException e){
    out.println(e.getMessage());
    out.println(e.getSQLState());
    out.println(e.getErrorCode());
  }
%>
</body>
</html>

Summarize


Related articles: