JDBC connection Mysql database steps (Mysql8.0 and above) and MySql database connection DriverManager.getConnection (url, user, pass) error solution at the end of the article, please watch patiently

Recently we learned JSP database application development, and encountered some problems when running the program. Let's first look at the steps of JDBC connection to Mysql database.

1. Load the JDBC driver

Realized by forName (String className)

 The driver of the MySQL database is:

String driverClass="com.mysql.jdbc.Driver";

The packages needed to connect to the MySQL database are:

mysql-connector-java-5.1.20-bin.jar

The method of citing the jar package directly downloads the jar package (note that no decompression is required)

1. We can directly drag it to lib under WEB-INF, or copy and paste it to lib under WEB-INF.

 2. Right-click the jar package, click Bulid Path, and then click Add to Bulud Path

 After that, there will be this red box, which means the import is successful.

Note that I encountered a problem here, because the teacher used the mysql5.5 version when he lectured. When I was learning mysql, I downloaded mysql8.0 from the official website. I didn’t expect to use it again after learning mysql. The teacher sent it to us It is the jar package of mysql-connector-java-5.1.20-bin.jar . An error was reported when connecting to the database, as shown in the following figure:

 I checked on the Internet, and it seems that mysql8.0 has changed when obtaining the password .

Differences between version 5.0.X and version 8.0.X
1. The driver address connected to version 5.0.X is: "com.mysql.jdbc.Driver", while version 8.0.X is changed to: "com.mysql.cj.jdbc .Driver"

2. Some parameters are added to the url when establishing a connection: The url of version 5.0.X is String url = "jdbc:mysql://localhost:3306/information"

The 8.0.X version needs to add a string of letters after it to become: String url = jdbc:mysql://local:3306/information

?&useSSL=false&serverTimezone=UTC"

For example, the name of my database is "information", and it doesn't matter why, just change the encoding format, authentication, and time zone, and version 8.0.X requires so much.
Original link: https://blog.csdn.net/sinat_41721615/article/details/84346249

This problem has not been solved (it can be solved by using the above method), so I found a stupid way, which is to download another mysql5.5, and finally it can run and produce results.

The URL to connect to the mysql database is:

String url="jdbc:mysql://localhost:3307/information";

information is the name of the database you created, and student is the name of the table you created.

Also, 3307 is the port number of the database (this is the custom port number of the mysql5.5 version I downloaded again), and 3306 is the default port number of the mysql database.

2. Create a database connection

By calling DriverManager.getConnection(url, username, password); to establish a connection, the three entry parameters are the path to connect to the database, username and password in turn, and the return value type of this defense is java.sql.Connection.

String url="jdbc:mysql://127.0.0.13306/information";

url is the main communication protocol: mysql is the secondary communication protocol ://127.0.0.1 3306 (can also be replaced with localhost:3306)

"127.0.0.1" is the IP address of the machine where the database is located, and this IP represents this machine. information is the name of the database you want to connect to

    String url="jdbc:mysql://localhost:3307/information";
    String username="root";
    String password="123456";

3. Execute the SQL statement

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select * from student");

I use Navicat to create information database and student table in turn. The types of fields in the table are student number char , name varchar, gender char, age int

4. Obtain query results

while(rs.next()){
        out.println("<br>姓名:  "+rs.getString(2)+"         性别:"+rs.getString(3));
    }

5. Close the connection

     rs.close();
    stmt.close();
    conn.close();

Attach the complete code diagram and implementation effect diagram:

It should be noted that we must create a database and a table (the field type in the table must be correct), and the database name and table name must correspond to those in the code. My database name and table name may be different from the one you created.

 

Finally, let's talk about the problem (Mysql8.0 and above) when an error is reported when connecting to the database, and the problem shown in the figure below appears. I read a lot of methods written by bloggers, but none of them succeeded!

Connection conn=DriverManager.getConnection(url, username, password);

 

Ahhh, I almost lost my confidence in learning JSP. After several twists and turns, I started to search for JDBC: Steps to connect to Mysql database, because I always searched for code errors before , and the reason is that MySQL8 is used in the project. There may be a time difference problem because of the time zone setting problem.

UTC represents the global standard time, but the time we use is the Beijing time zone, which is the East Eighth District, which is eight hours ahead of UTC.

UTC + (+0800) = local (Beijing) time.

Attach the reference URL: http://t.csdn.cn/iHYmf

Read JDBC: Steps to connect to Mysql database . I copied the blogger's line of code to my code, and the code ran directly to produce results, which made me overjoyed, and also told me a truth, searching for code error problems may not necessarily solve the problem, we You can try how to achieve the code running results, and there may be unexpected gains.

  Just add ?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC on it

The above code is for using third-party Api

My JSP can link to my Mysql8.0 database.

Attach the reference URL: http://t.csdn.cn/gcsJm

           

                                     I am really grateful to this blogger, and also to myself who never gave up and continued to explore!

              

               The rivers and lakes are far away, let us meet at the top!

Guess you like

Origin blog.csdn.net/m0_62059090/article/details/128085340