今天写了一段代码:出问题了,不要慌,百度,解决了,哈哈。得劲。
1 packagecn.zpoor.DBTest;2
3 importjava.sql.DriverManager;4 importjava.sql.ResultSet;5 importjava.sql.SQLException;6
7 importcom.mysql.jdbc.Connection;8 importcom.mysql.jdbc.PreparedStatement;9
10 public classStudents {11 private intid;12 privateString name;13 privateString sex;14 private intage;15 public Students(String name, String sex, intage) {16 this.id = 0;17 this.name =name;18 this.sex =sex;19 this.age =age;20 }21 public intgetId() {22 returnid;23 }24 public void setId(intid) {25 this.id =id;26 }27 publicString getName() {28 returnname;29 }30 public voidsetName(String name) {31 this.name =name;32 }33 publicString getSex() {34 returnsex;35 }36 public voidsetSex(String sex) {37 this.sex =sex;38 }39 public intgetAge() {40 returnage;41 }42 public void setAge(intage) {43 this.age =age;44 }45
46
47 private staticConnection getConn() {48 String driver = "com.mysql.jdbc.Driver";49 String url = "jdbc:mysql://localhost:3306/school";50 String username = "root";51 String password = "zhuqiong520";52 Connection conn = null;53 try{54 Class.forName(driver);//加载驱动
55 conn =(Connection) DriverManager.getConnection(url, username, password);56 } catch(ClassNotFoundException e) {57 e.printStackTrace();58 } catch(SQLException e) {59 e.printStackTrace();60 }61 returnconn;62 }63
64 private staticInteger getAll() {65 Connection conn =getConn();66 String sql = "select * from students";67 PreparedStatement pstmt;68
69 try{70 pstmt =(PreparedStatement) conn.prepareStatement(sql);71 ResultSet rs =pstmt.executeQuery();72 int col =rs.getMetaData().getColumnCount();73 System.out.println("=============");74 while(rs.next()) {75 for(int i = 1;i<=col;i++) {76 System.out.println(rs.getString(i)+"\t");77 if (i==2 && rs.getString(i).length()<8) {78 System.out.println("\t");79 }80 }81 System.out.println("");82 }83 System.out.println("============");84 } catch(SQLException e) {85 e.printStackTrace();86 }87 return null;88 }89
90 public static voidmain(String[] args) {91 Students.getAll();92 }93 }
Wed Oct 11 22:20:41 CST 2017 WARN: Establishing SSL connection
without server‘s identity verification is not recommended. According
to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must
be established by default if explicit option isn‘t set. For compliance
with existing applications not using SSL the verifyServerCertificate
property is set to ‘false‘. You need either to explicitly disable SSL by
setting useSSL=false, or set useSSL=true and provide truststore for server
certificate verification.=============
1朱琼
男22
2王珊珊
女21
============
用的是java-connector-5.1.42-bin.jar
当然结果是对的,但是上面一行说的什么useSSl没有设置,百度了一下,是这样的。
冷静分析:主要是我的jar包版本过高造成的。用以前的旧版本没什么问题,而且新版本的MySQL要求是否进行ssl连接。
解决方法:这样写就可以了 String url = "jdbc:mysql://localhost:3306/school?useSSL=false";
拓展:conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test_db?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","password");
useUnicode设置主要是设置字符编码为utf-8,也可以在eclipse中设置默认的字符编码。
原文:http://www.cnblogs.com/zpoor/p/7653455.html