import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class JMySQL {
public static Connection cn;
public static PreparedStatement pr;
public static ResultSet rs;
public static DefaultTableModel tblMode = null;
public static Connection getKoneksi(){
String hostDbDriver = "jdbc:mysql://localhost/nama_databasenya";
String userDb = "user_databasenya";
String passDb = "password_databasenya";
JMySQL.cn = null;
try {
//buat koneksi
JMySQL.cn = DriverManager.getConnection(hostDbDriver,userDb,passDb);
}catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Database error" +ex);
}
return JMySQL.cn;
}
public static void tutupKoneksi(Connection cn, PreparedStatement pr, ResultSet rs){
try {
if(pr != null){pr.close();}
if(rs != null){rs.close();}
if(cn != null){cn.close();}
}catch(SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
}
Untuk menggunakan class diatas dengan perintah :
JMySQL.getKoneksi();
0 Komentar