Sebelumnya, anda harus mendownload JDBC terlebih dahulu, silahkan search pada google.
Ada code penting yang perlu diperhatikan yaitu :
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();
Lihatlah contoh Code dibawah ini :
JDBCConnect.java
package org.redeagle.sqlconn;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCConnect{
public static void main(String[] argv) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Driver tidak ada");
e.printStackTrace();
return;
}
System.out.println("JDBC Driver ditemukan.");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/namaDB","root", "password");
} catch (SQLException e) {
System.out.println("Connect ke database gagal.");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("Connect ke database berhasil.");
} else {
System.out.println("Gagal Connect");
}
}
}
0 comments:
Post a Comment