Java connection data failed, the version of Mysql I used is 8.0 and I need to rewrite Url to add the default character set, as shown in the figure below
package JDBC_connection;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class Connection_test {
@Test
public void testConnection1() throws SQLException {
Driver driver=new com.mysql.jdbc.Driver();
// jdbc:mysql 数据库协议
//localhost: ip地址
// 3306: 默认端口号
//test:连接的数据库的名称
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" ;
// 以键值对的形式存储用户名和密码
Properties info=new Properties();
// 将用户名和密码封装在propertise中
info.setProperty("user", "root");
info.setProperty("password", "123456");
try {
Connection connection=driver.connect(url, info);
System.out.println(connection);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}