Posted in MySQL 12 years ago 2 min read
Add JAR[/caption]
Untuk mengecek apakah Driver dapat di load dan terkoneksi dengan baik dengan database gunakan source code berikut:
LoadDriver.java
public class LoadDriver {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
}
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/test?"+"user=root&password=");
} catch (SQLException e) {
System.out.println("SQLException: "+e.getMessage());
System.out.println("SQLState: "+e.getSQLState());
System.out.println("VendorError: "+e.getErrorCode());
}
}
}
Jika anda belum menambahkan library (Add JAR) maka error yang muncul adalah:
SQLException: No suitable driver found for jdbc:mysql://localhost/test?user=root&password= SQLState: 08001 VendorError: 0Sedangkan error yang terjadi karena MySQL belum dihidupkan adalah:
SQLException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. SQLState: 08S01 VendorError: 0jika sudah benar, maka LoadDriver.java tidak akan menghasilkan apapun. (Build Successful). Untuk konfigurasi koneksi ada pada baris:
conn = DriverManager.getConnection("jdbc:mysql://localhost/test?"+"user=root&password=");
Silakan sesuaikan dengan database, user dan password di MySQL anda.
Semoga bermanfaat.