全文参考:https://www.cnblogs.com/Qian123/p/5339164.html#_label0
解决时区报错:https://blog.csdn.net/aayygg1234/article/details/78205240
package jnxyjsj.zs.po; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mysql.cj.jdbc.Driver; public class Customer { public static void main(String[] args) { try { // 不加?serverTimezone=UTC 会提示时区错误 Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.2:3306/market?serverTimezone=UTC", "testuser", "wif12!@13..D"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select productID, name, price from product"); while(rs.next()){//如果对象中有数据,就会循环打印出来 System.out.println(rs.getString("name")+","+rs.getInt("price")); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } // TODO 自动生成的方法存根 //Class.forName("com.mysql.cj.jdbc.Driver"); } }