60 lines
2.4 KiB
Java
60 lines
2.4 KiB
Java
/*
|
|
HOW TO USE IT in 192.168.0.120 (MINI PC PACS):
|
|
1. cd to `cd /pacs/pacs_installer/`
|
|
2. `export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_144/bin/`
|
|
3. Compile : `javac -cp .:mysql-connector-java-8.0.22.jar MySQLConnectionTest.java`
|
|
4. Run : `java -cp .:mysql-connector-java-8.0.22.jar MySQLConnectionTest 192.168.0.120 3306 pacsdb pacs pacs`
|
|
4. Run : `java -cp .:mysql-connector-java-8.0.22.jar MySQLConnectionTest dev.sismedika.online 3306 rsabt201107 hispacs s1sm3d1k4123!`
|
|
*/
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
|
|
public class MySQLConnectionTestV2 {
|
|
public static void main(String[] args) {
|
|
// MySQL database details from command line arguments
|
|
if (args.length < 5) {
|
|
System.out.println("Usage: java MySQLConnectionTest <host> <port> <database> <user> <password>");
|
|
return;
|
|
}
|
|
|
|
String host = args[0];
|
|
String port = args[1];
|
|
String dbName = args[2];
|
|
String user = args[3];
|
|
String password = args[4];
|
|
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;
|
|
|
|
Connection connection = null;
|
|
try {
|
|
// Load MySQL JDBC Driver (MySQL Connector 8.0.22 and above auto-registers the driver)
|
|
Class.forName("com.mysql.cj.jdbc.Driver"); // Optional for JDBC 4.0+
|
|
|
|
// Add timezone configuration to URL if needed
|
|
url += "?serverTimezone=Asia/Jakarta";
|
|
|
|
// Establish a connection
|
|
connection = DriverManager.getConnection(url, user, password);
|
|
System.out.println("Connection successful!");
|
|
} catch (ClassNotFoundException e) {
|
|
System.out.println("MySQL JDBC Driver not found. Include the MySQL Connector jar file.");
|
|
e.printStackTrace();
|
|
} catch (SQLException e) {
|
|
System.out.println("Failed to connect to MySQL database.");
|
|
System.out.println("Error Message: " + e.getMessage());
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (connection != null) {
|
|
try {
|
|
connection.close();
|
|
System.out.println("Connection closed.");
|
|
} catch (SQLException e) {
|
|
System.out.println("Failed to close the connection.");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|