由於是連到遠端架的mysql database,所以該台電腦的port要記得設防火牆,
不然會被擋下的。
不然會被擋下的。
import java.sql.*;
public class DBConnectionDemo {
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://host:3306/tablename";
String user = "xxx";
String password = "xxx";
try {
Class.forName(driver);
Connection conn =
DriverManager.getConnection(url,
user, password);
if(conn != null && !conn.isClosed()) {
System.out.println("資料庫連線測試成功!");
conn.close();
}
}
catch(ClassNotFoundException e) {
System.out.println("找不到驅動程式類別");
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}