Koneksi Database Java dengan MySQL KE-1

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package db;

import java.sql.*;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.sql.*;
import java.sql.DriverManager;

public class Db {
//Untuk koneksi ke MySQL
private String userid = "root";
private String password = "root";

String url = "jdbc:mysql://127.0.0.1:3306/soalonline";

public Connection con;

// constructor
public Db(){
getConnection();
}

public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (java.lang.ClassNotFoundException e){
System.err.print("Koneksi dengan Database gagal: ");
System.err.println(e.getException());
}

try {
con = DriverManager.getConnection(url, userid,password);
System.out.print("Nyambung");

}catch(SQLException e) {
System.err.println("ID dan Password Anda gagal " + e.getMessage());
}

return con;
}

public static void main(String[] args){
new Db();
}
}

0 komentar: