JDBC連接數據庫demo

JDBC連接數據庫demo

創建database和相應的table
create database eesy;
create table account(id int primary key
 auto_increment,name varchar(40),money float)
 character set utf8 collate utf8_general_ci;
 insert into account(name,money) values('aaa',100);
 insert into account(name,money) values('bbb',1000);

新建maven工程,在src/main/java下新建class

注意需要導入mysql-connectioner-java-***.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcDemo1 {
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		//註冊驅動
		DriverManager.registerDriver(new com.mysql.jdbc.Driver());
		//獲取連接
		Connection conn = DriverManager.getConnection(  "jdbc:mysql://localhost:3306/eesy","root","westos" ); //(url,user,password)
		//獲取操作數據庫的預處理對象
		PreparedStatement pstem = conn.prepareStatement("select * from account");   
		//執行SQL,得到結果集
		ResultSet rs = pstem.executeQuery();
		//遍歷結果集
		while(rs.next()) {
			System.out.println(rs.getString("name"));
		}
		//釋放資源
		rs.close();
		pstem.close();
		conn.close();
	}
}

Problems:

1.Error getting a new connection. Cause: java.sql.SQLException: The server time zone value

直接指定時區爲UTC
jdbc:mysql://000.000.000.000:3306/etc?serverTimezone=UTC

2.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.

添加:
jdbc:mysql://000.000.000.000:3306/etc?serverTimezone=UTC&useSSL=false

3.The server time zone value ‘��������ʱ’ is unrecognized 記錄…

修改數據庫時區:

mysql> set global time_zone = '-4:00';       //修改mysql全局時區NJ
Query OK, 0 rows affected (0.00 sec)

mysql>  set time_zone = '-4:00';             //修改當前會話時區
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)

4.java.sql.SQLException: The server time zone value ‘������׼ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

//		String jdbcUrl = "jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false";
//		String jdbcUrl ="jdbc:mysql://localhost:3306/hb_student_tracker?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
String jdbcUrl = "jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=GMT%2B8";	
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章