Java connection MySQL warning:establishing SSL connection without server ' s identity verification is not recommended

Source: Internet
Author: User
Tags ssl connection

1. Database 1.1 Creating tables

In the current database students, create the data table student:

1Mysql> Create TableStudent (2      -StudentIDChar(Ten), #学生ID3      -Namevarchar(Ten), #学生姓名4      -Agesmallint, #学生年龄5      -Gradesmallint) #成绩6      -;
1.2 Inserting information:
INSERT VALUES ("1*********", "Tom",",")

This is done after inserting:

2. Download connector and import 2.1 download unzip

Download the MySQL JDBC Drive in https://dev.mysql.com/downloads/connector/j/

After decompression :

The . jar file is the file we need.

2.2 Creating a Project Import jar package

The Eclipse IDE creates a Java project and right-clicks the Java project:

Select Configure Build Path

Both ways of adding jar files can be implemented.

The difference is:

Add jars need to copy the jar file into the project beforehand, click the Add Jars button and select the Jar file in the project . Use a relative path , even if you copy the item to another computer.

The add external Jars is the absolute path used to select the jar file in the Explorer . And then it appears:

3. Code 3.1 Write code:
ImportJava.sql.*;ImportCom.mysql.*; Public classTest1 { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubClass.forName ("Com.mysql.jdbc.Driver"); Java.sql.Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/students?usessl=true", "root", "11111"); Java.sql.Statement SQL=conn.createstatement (); Java.sql.ResultSet RS=sql.executequery ("SELECT * FROM Student");  while(Rs.next ()) {System.out.println (rs.getstring (1) + "+rs.getstring (2) +" "+rs.getstring (3) +" "+rs.getstring (4)); }    }}
3.2 Code Explanation:
    • Class.forName () is used to load the driver. The usage of Class.forName () is described in detail: http://blog.csdn.net/kaiwii/article/details/7405761
    • Usage of getconnection ():

Parameter URL, jdbc:mysql://localhost:3306/students

Note that the port number 3306 is the default port number , and if so, change it here. The right side of the slash is the database name .

The reason behind the code is added to the usessl=true, because if not added, the following warning will appear:

 
  

Answer See stackoverflow:http://stackoverflow.com/questions/34224970/. Warning-establishing-ssl-connection-without-servers-identity-verification-is-n

User is the username,password password.

You can also do this:

jdbc:mysql://localhost:3306/jspstudy?user=root&password=111111

  

The result of the query is inside the RS object, through the inside of the getString () method to get the string output, the integer in the parameter is the number of columns.

Code and Query results:

Java connection MySQL warning:establishing SSL connection without server ' s identity verification is not recommended

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.