You are on page 1of 11

Form Register

package pbo1;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JOptionPane;

import java.sql.DriverManager;

import pbo1.koneksi;

/**

* @author Wibowo

*/

public class register extends javax.swing.JFrame {

public register() {

initComponents();

txID.setEnabled(false);

autonumber();

private void autonumber(){

try{

Connection c = koneksi.getkoneksi();

Statement s = c.createStatement();

String sql = "SELECT * FROM db_user ORDER By id DESC";


ResultSet r = s.executeQuery(sql);

if (r.next()) {

String NoID = r.getString("id").substring(2);

String ID = "" +(Integer.parseInt(NoID)+1);

String Zero = "";

if (ID.length()==1)

{Zero = "00";}

else if (ID.length()==2)

{Zero = "0";}

else if (ID.length()==3)

{Zero = "";}

txID.setText("AD" + Zero + ID);

}else{

txID.setText("AD001");

r.close();

s.close();

}catch(Exception e){

System.out.println("gagal");

private void submitActionPerformed(java.awt.event.ActionEvent evt) {

String id = txID.getText().toString().trim();

String username = txUsername.getText().toString().trim();


String password = new String(txPassword.getPassword());

String conPassword = new String(txConPassword.getPassword());

if (!password.equals(conPassword)) {

JOptionPane.showMessageDialog(null, "Password tidak sesuai");

} else if (password.isEmpty() || username.isEmpty()) {

JOptionPane.showMessageDialog(null, "Username atau Password tidak boleh kosong");

} else {

try {

Connection c = koneksi.getkoneksi();

String sql = "INSERT INTO db_user (id, username, password) VALUES (?, ?, ?)";

try (PreparedStatement p = c.prepareStatement(sql)) {

p.setString(1, id);

p.setString(2, username);

p.setString(3, password);

int affectedRows = p.executeUpdate();

if (affectedRows > 0) {

JOptionPane.showMessageDialog(null, "Registrasi berhasil");

} else {

JOptionPane.showMessageDialog(null, "Registrasi gagal");

} catch (SQLException e) {

System.out.println("Error: " + e.getMessage());

JOptionPane.showMessageDialog(null, "Registrasi gagal");

} finally {

this.dispose();

login a = new login();

a.setVisible(true);
}

private void txUsernameActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void txIDActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}
}

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(register.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(register.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(register.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(register.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new register().setVisible(true);

});

}
Form Login

package pbo1;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import pbo1.koneksi;
import pbo1.MenuUtama;
/**
*
* @author Wibowo
*/
public class login extends javax.swing.JFrame {
public login() {
initComponents();
}

private void loginActionPerformed(java.awt.event.ActionEvent evt) {


try {
Connection c = koneksi.getkoneksi();
Statement s = c.createStatement();

String sql = "SELECT * FROM db_user WHERE username='" + username.getText()


+"' and password='" + new String(password.getPassword()) + "'";
ResultSet r = s.executeQuery(sql);

if (r.next()) {
JOptionPane.showMessageDialog(null, "Login Berhasil");
this.dispose();
book1 menu = new book1(); // Ganti dengan nama class menuUtama yang sesuai
menu.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "Password atau username salah");
password.requestFocus();
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}

private void usernameActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
register a = new register();
a.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new login().setVisible(true);
}
});
}
Koneksi

package pbo1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/* @author : Abi*/

public class koneksi {


private static java.sql.Connection koneksi;

public static java.sql.Connection getkoneksi(){


if(koneksi == null){
try{
String url="jdbc:mysql://localhost:3306/login";
String user="root";
String password="";
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
koneksi = DriverManager.getConnection(url, user, password);
System.out.println("koneksi berhasil");
}catch (Exception e){
System.out.println("Error");
}
}
return koneksi;
}
public static void main (String args[]){
getkoneksi();
}
static Connection getKoneksi() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of
generated methods, choose Tools | Templates.
}
}

You might also like