Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package koneksi;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class KoneksiDB {
- private Connection koneksi;
- private ResultSet rs;
- private PreparedStatement ps;
- public Connection getKoneksi() {
- if (koneksi == null) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- try {
- String url = "jdbc:mysql://localhost:3306/coffedb";
- koneksi = DriverManager.getConnection(url, "root", "");
- System.out.println("YEY, Koneksi Sukses");
- } catch (SQLException se) {
- System.out.println("YAHH, Koneksi Gagal" + se);
- System.exit(0);
- }
- } catch (ClassNotFoundException cnfe) {
- System.out.println("Maaf, Class tidak ditemukan !" + cnfe);
- System.exit(0);
- }
- }
- return koneksi;
- }
- public ResultSet getRs() {
- return rs;
- }
- public boolean eksekusiQuery(String query, boolean baris) {
- try {
- ps = koneksi.prepareStatement(query);
- if (baris) {
- rs = ps.executeQuery();
- } else {
- ps.executeUpdate();
- }
- return true;
- } catch (SQLException e) {
- return false;
- }
- }
- public static void main(String[] coba) {
- new KoneksiDB().getKoneksi();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement