import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
public class ConnectiontoDB{
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL =
"jdbc:mysql://[主機位置]:3306/[資料庫名稱]?autoReconnect=true&useSSL=false";
public static void main(String args[]){
Connection connection = null;
try{
Class.forName(DRIVER);
}catch(ClassNotFoundException e){
System.out.println("DRIVER ERROR: "+e);}
try{
connection = DriverManager.getConnection(DATABASE_URL,"[帳號]","[密碼]");
}catch(Exception e){
System.out.println("forName: "+e);
}
}
}
加上藍色這段主要是因為不佳的時候會出現錯誤訊息
Tue Jan 24 23:17:26 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.
雖然是版本問題, 但是如果改成useSSL=false就像是跳過SSL連線依樣, 其實蠻糟糕的,
所以使用的時候還是記得用useSSL= true 安全一點.
至於現在用兩個try-catch主要是讓我更知道錯誤的訊息在哪出現,
雖然蠻多餘的 ㄎㄎ. 不過這樣就知道是DRIVER錯誤還是連線錯,
雖然這次的錯誤訊息是版本問題的錯誤....
沒有留言:
張貼留言