Dasar implementasi yg paling gampang buat contoh koneksi java ke database digunakan untuk login dengan data berasal dari tabel di SQL Server. Syaratnya: JDK, SQL Server 2000, driver sql server.
1. Buat tabel untuk menyimpan user
- Database: dbKuliah
- Tabel: tbUser
- Field: fUser, fNama, fPass
- isikan data
2. Pilih jenis Koneksi
- Apabila memilih odbc, Buat DataSource untuk koneksi
+ Driver: SQL Server
+ Name: koneksi
+ server: .
+ Login ID: With Winndows NT auth.
+ Language: Italian
+ Connect to
- Apabila memilih driver sqlserver, install terlebih dahulu, pastikan tahu username dan password sa
download driver dari http://www.microsoft.com/downloads/details.aspx?FamilyID=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71
3. Buat Form frmLogin:
Komponen: JTextField -> tUser, JPasswordField -> tPass, JButton -> bLogin
4. Bagian Kode:
- Bagian import
import javax.swing.*;
import java.net.*;
- Variabel Global
Connection con;
Statement stmt;
String perintah;
String sqlUser="sa"; //UserName untuk administrator SQL server
String sqlPass="sa"; //Password untuk administrator SQL server
String sqlDb="dbKuliah"; //Database yang diakses
String koneksiOdbc="koneksi"; //Nama koneksi odbc
- Fungsi Tambahan
public void testDriver(){
String dbDrv1= "sun.jdbc.odbc.JdbcOdbcDriver";
String dbDrv2 = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbUrl1= "jdbc:odbc:"+koneksiOdbc;
String dbUrl2 = "jdbc:microsoft:sqlserver://localhost;DatabaseName="+sqlDb;
try{
//Pilih salah satu
Class.forName(dbDrv1); //menggunakan koneksi odbc
//Class.forName(dbDrv2); //menggunakan koneksi driver mssql
//Pilih salah satu
con = DriverManager.getConnection(dbUrl1,sqlUser,sqlPass); //menggunakan koneksi odbc
//con = DriverManager.getConnection(dbUrl2,sqlUser,sqlPass); //menggunakan koneksi driver mssql
stmt=con.createStatement();
}catch(Exception ce){
System.out.println(ce.toString());
}
}
- Bagian paling bawah dari constructor
testDriver();
- Bagian event actionPerformed Tombol bLogin
try{
perintah="SELECT count(*) FROM tbUser where fUser='"+tUser.getText()+"' AND fPass='"+tPass.getText()+"' ";
ResultSet hasil=stmt.executeQuery(perintah);
hasil.next();
if(Integer.parseInt(hasil.getObject(1).toString())>=1){
JOptionPane.showMessageDialog(this,"Login Berhasil");
}else{
JOptionPane.showMessageDialog(this,"Login Gagal");
}
}catch(Exception e){System.err.println(e.getMessage());}
Sorce Lengkapnya:
import javax.swing.*;
import java.sql.*;
public class frmLogin extends javax.swing.JFrame {
/** Creates new form frmLogin */
public frmLogin() {
initComponents();
testDriver();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
tUser = new javax.swing.JTextField();
tPass = new javax.swing.JPasswordField();
bLogin = new javax.swing.JButton();
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(tUser);
tUser.setBounds(110, 70, 80, 20);
getContentPane().add(tPass);
tPass.setBounds(110, 110, 80, 24);
bLogin.setText("Login");
bLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bLoginActionPerformed(evt);
}
});
getContentPane().add(bLogin);
bLogin.setBounds(110, 160, 59, 23);
pack();
}//GEN-END:initComponents
private void bLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoginActionPerformed
try{
perintah="SELECT count(*) FROM tbUser where fUser='"+tUser.getText()+"' AND fPass='"+tPass.getText()+"' ";
ResultSet hasil=stmt.executeQuery(perintah);
hasil.next();
if(Integer.parseInt(hasil.getObject(1).toString())>=1){
JOptionPane.showMessageDialog(this,"Login Berhasil");
}else{
JOptionPane.showMessageDialog(this,"Login Gagal");
}
}catch(Exception e){System.err.println(e.getMessage());}
}//GEN-LAST:event_bLoginActionPerformed
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new frmLogin().show();
}
public void testDriver(){
String dbDrv1 = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbDrv2 = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbUrl1 = "jdbc:odbc:"+koneksiOdbc;
String dbUrl2 = "jdbc:microsoft:sqlserver://localhost;DatabaseName="+sqlDb;
try{
//Pilih salah satu
Class.forName(dbDrv1); //menggunakan koneksi odbc
//Class.forName(dbDrv2); //menggunakan koneksi driver mssql
//Pilih salah satu
con = DriverManager.getConnection(dbUrl1,sqlUser,sqlPass); //menggunakan koneksi odbc
//con = DriverManager.getConnection(dbUrl2,sqlUser,sqlPass); //menggunakan koneksi driver mssql
stmt=con.createStatement();
}catch(Exception ce){
System.out.println(ce.toString());
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bLogin;
private javax.swing.JPasswordField tPass;
private javax.swing.JTextField tUser;
// End of variables declaration//GEN-END:variables
Connection con;
Statement stmt;
String perintah;
String sqlUser="sa"; //UserName untuk administrator SQL server
String sqlPass="sa"; //Password untuk administrator SQL server
String sqlDb="dbKuliah"; //Database yang diakses
String koneksiOdbc="koneksi"; //Nama koneksi odbc
}
1. Buat tabel untuk menyimpan user
- Database: dbKuliah
- Tabel: tbUser
- Field: fUser, fNama, fPass
- isikan data
2. Pilih jenis Koneksi
- Apabila memilih odbc, Buat DataSource untuk koneksi
+ Driver: SQL Server
+ Name: koneksi
+ server: .
+ Login ID: With Winndows NT auth.
+ Language: Italian
+ Connect to
- Apabila memilih driver sqlserver, install terlebih dahulu, pastikan tahu username dan password sa
download driver dari http://www.microsoft.com/downloads/details.aspx?FamilyID=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71
3. Buat Form frmLogin:
Komponen: JTextField -> tUser, JPasswordField -> tPass, JButton -> bLogin
4. Bagian Kode:
- Bagian import
import javax.swing.*;
import java.net.*;
- Variabel Global
Connection con;
Statement stmt;
String perintah;
String sqlUser="sa"; //UserName untuk administrator SQL server
String sqlPass="sa"; //Password untuk administrator SQL server
String sqlDb="dbKuliah"; //Database yang diakses
String koneksiOdbc="koneksi"; //Nama koneksi odbc
- Fungsi Tambahan
public void testDriver(){
String dbDrv1= "sun.jdbc.odbc.JdbcOdbcDriver";
String dbDrv2 = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbUrl1= "jdbc:odbc:"+koneksiOdbc;
String dbUrl2 = "jdbc:microsoft:sqlserver://localhost;DatabaseName="+sqlDb;
try{
//Pilih salah satu
Class.forName(dbDrv1); //menggunakan koneksi odbc
//Class.forName(dbDrv2); //menggunakan koneksi driver mssql
//Pilih salah satu
con = DriverManager.getConnection(dbUrl1,sqlUser,sqlPass); //menggunakan koneksi odbc
//con = DriverManager.getConnection(dbUrl2,sqlUser,sqlPass); //menggunakan koneksi driver mssql
stmt=con.createStatement();
}catch(Exception ce){
System.out.println(ce.toString());
}
}
- Bagian paling bawah dari constructor
testDriver();
- Bagian event actionPerformed Tombol bLogin
try{
perintah="SELECT count(*) FROM tbUser where fUser='"+tUser.getText()+"' AND fPass='"+tPass.getText()+"' ";
ResultSet hasil=stmt.executeQuery(perintah);
hasil.next();
if(Integer.parseInt(hasil.getObject(1).toString())>=1){
JOptionPane.showMessageDialog(this,"Login Berhasil");
}else{
JOptionPane.showMessageDialog(this,"Login Gagal");
}
}catch(Exception e){System.err.println(e.getMessage());}
Sorce Lengkapnya:
import javax.swing.*;
import java.sql.*;
public class frmLogin extends javax.swing.JFrame {
/** Creates new form frmLogin */
public frmLogin() {
initComponents();
testDriver();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
tUser = new javax.swing.JTextField();
tPass = new javax.swing.JPasswordField();
bLogin = new javax.swing.JButton();
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(tUser);
tUser.setBounds(110, 70, 80, 20);
getContentPane().add(tPass);
tPass.setBounds(110, 110, 80, 24);
bLogin.setText("Login");
bLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bLoginActionPerformed(evt);
}
});
getContentPane().add(bLogin);
bLogin.setBounds(110, 160, 59, 23);
pack();
}//GEN-END:initComponents
private void bLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoginActionPerformed
try{
perintah="SELECT count(*) FROM tbUser where fUser='"+tUser.getText()+"' AND fPass='"+tPass.getText()+"' ";
ResultSet hasil=stmt.executeQuery(perintah);
hasil.next();
if(Integer.parseInt(hasil.getObject(1).toString())>=1){
JOptionPane.showMessageDialog(this,"Login Berhasil");
}else{
JOptionPane.showMessageDialog(this,"Login Gagal");
}
}catch(Exception e){System.err.println(e.getMessage());}
}//GEN-LAST:event_bLoginActionPerformed
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new frmLogin().show();
}
public void testDriver(){
String dbDrv1 = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbDrv2 = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbUrl1 = "jdbc:odbc:"+koneksiOdbc;
String dbUrl2 = "jdbc:microsoft:sqlserver://localhost;DatabaseName="+sqlDb;
try{
//Pilih salah satu
Class.forName(dbDrv1); //menggunakan koneksi odbc
//Class.forName(dbDrv2); //menggunakan koneksi driver mssql
//Pilih salah satu
con = DriverManager.getConnection(dbUrl1,sqlUser,sqlPass); //menggunakan koneksi odbc
//con = DriverManager.getConnection(dbUrl2,sqlUser,sqlPass); //menggunakan koneksi driver mssql
stmt=con.createStatement();
}catch(Exception ce){
System.out.println(ce.toString());
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bLogin;
private javax.swing.JPasswordField tPass;
private javax.swing.JTextField tUser;
// End of variables declaration//GEN-END:variables
Connection con;
Statement stmt;
String perintah;
String sqlUser="sa"; //UserName untuk administrator SQL server
String sqlPass="sa"; //Password untuk administrator SQL server
String sqlDb="dbKuliah"; //Database yang diakses
String koneksiOdbc="koneksi"; //Nama koneksi odbc
}
Lah kalau Usernya banyak terus beda hak level buat nya gimana , mohon petunjuknya mas...
ReplyDelete@ Anonim
ReplyDeleteHak level yang berbeda bisa dilakukan dengan pengelompokkan user. Setiap user mempunyai group, setiap group bisa diset hak aksesnya. Jadi setiap user mempunyai hak akses sesuai dengan group yang diberikan.