Java链接mysql
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Dbcon { //mysql8.0 url confing serverTimezone //配置jdbc的URL:jdbc:mysql://[IP]:[PORT]/[DB]?characterEncoding=utf8&useSSL=false& //serverTimezone=UTC&rewriteBatchedStatements=true private static String URL = "jdbc:mysql://localhost:3306/dlt?serverTimezone=Asia/Shanghai"; //private static String URL = "jdbc:mysql://localhost:3306/dlt?serverTimezone=UTC"; private static String USER = "root"; private static String PASSWORD = "root"; private static Connection con = null; public Dbcon(){} public static void main(String[] args) throws Exception { //mysql-connector-java-8.0.20 Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection(URL, USER,PASSWORD); Statement stmt = con.createStatement(); //ResultSet rs = stmt.executeQuery("select q1,q2,q3,q4,q5,h1,h2 from detail where id = '7001'"); ResultSet rs = stmt.executeQuery("select d_id,d_now,d_stamp,d_mark from datm"); while(rs.next()){ System.out.println("Q1:" + rs.getString(1)); System.out.println("Q2:" + rs.getString(2)); System.out.println("Q3:" + rs.getString(3)); System.out.println("Q4:" + rs.getString(4)); /*System.out.println("Q5:" + rs.getString(5)); System.out.println("H1:" + rs.getString("h1")); System.out.println("H2:" + rs.getString("h2"));*/ } //int up = stmt.executeUpdate("insert into datm(d_now,d_stamp,d_mark) value(now(),current_timestamp(),'UTC')"); //System.out.println("Insert into:" + up); } }
Timestamp会根据服务器的设置的时区不同而显示不同的值,Datetime则不会。