Created
November 18, 2011 07:00
-
-
Save khannedy/1375800 to your computer and use it in GitHub Desktop.
Tutorial Java : Membuat Login Form untuk Aplikasi Destop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (LoginManager.getDefault().login(jTextFieldNamaPengguna.getText(), | |
new String(jPasswordFieldKataSandi.getPassword()))) { | |
JOptionPane.showMessageDialog(this, "Selamat datang"); | |
setVisible(false); | |
} else { | |
JOptionPane.showMessageDialog(this, "Maaf, anda bukan pengguna aplikasi ini"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
* | |
* http://stripbandunk.com/ | |
* | |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
*/ | |
package com.stripbandunk.formlogin; | |
import com.stripbandunk.formlogin.form.FormApplication; | |
import com.stripbandunk.formlogin.form.FormLogin; | |
import com.stripbandunk.formlogin.helper.HibernateHelper; | |
import javax.swing.SwingUtilities; | |
public class App { | |
private FormApplication formApplication; | |
private FormLogin formLogin; | |
public App() { | |
formApplication = new FormApplication(); | |
formLogin = new FormLogin(formApplication, true); | |
} | |
public void startApp() { | |
formLogin.setLocationRelativeTo(formApplication); | |
formApplication.setVisible(true); | |
formLogin.setVisible(true); | |
} | |
public static void main(String[] args) { | |
// init hibernate | |
HibernateHelper.getSessionFactory().openSession().close(); | |
// jalankan aplikasi | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
App app = new App(); | |
app.startApp(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE stripbandunk_formlogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
* | |
* http://stripbandunk.com/ | |
* | |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
*/ | |
package com.stripbandunk.formlogin.helper; | |
import com.stripbandunk.formlogin.entity.Pengguna; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.cfg.Configuration; | |
import org.hibernate.dialect.MySQL5InnoDBDialect; | |
public class HibernateHelper { | |
private final static SessionFactory sessionFactory; | |
static { | |
Configuration configuration = new Configuration(); | |
// konfigurasi connection | |
configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); | |
configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/stripbandunk_formlogin"); | |
configuration.setProperty("hibernate.connection.username", "root"); | |
configuration.setProperty("hibernate.connection.password", "root"); | |
// konfigurasi hibernate | |
configuration.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class.getName()); | |
configuration.setProperty("hibernate.hbm2ddl.auto", "update"); | |
// mapping entitas | |
configuration.addAnnotatedClass(Pengguna.class); | |
// membuat session factory | |
sessionFactory = configuration.buildSessionFactory(); | |
// pastikan di-close saat aplikasi ditutup | |
HibernateHelper.registerShutdownHook(); | |
} | |
public static SessionFactory getSessionFactory() { | |
return sessionFactory; | |
} | |
private static void registerShutdownHook() { | |
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | |
public void run() { | |
sessionFactory.close(); | |
} | |
})); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO | |
tabel_pengguna( nama_pengguna, kata_sandi ) | |
VALUES ( | |
'stripbandunk', ENCODE( 'stripbandunk', 'password' ) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
* | |
* http://stripbandunk.com/ | |
* | |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
*/ | |
package com.stripbandunk.formlogin.helper; | |
import com.stripbandunk.formlogin.entity.Pengguna; | |
import org.hibernate.Session; | |
/** | |
* | |
* @author echo | |
*/ | |
public class LoginManager { | |
private static LoginManager loginManager; | |
public static LoginManager getDefault() { | |
if (loginManager == null) { | |
loginManager = new LoginManager(); | |
} | |
return loginManager; | |
} | |
private Pengguna pengguna; | |
private LoginManager() { | |
// singleton class | |
} | |
public Pengguna getPengguna() { | |
return pengguna; | |
} | |
public void logout() { | |
this.pengguna = null; | |
} | |
public boolean login(String namaPengguna, String kataSandi) { | |
Session session = HibernateHelper.getSessionFactory().openSession(); | |
Pengguna db = (Pengguna) session.get(Pengguna.class, namaPengguna); | |
session.close(); | |
if (db == null) { | |
return false; | |
} else if (!db.getKataSandi().equals(kataSandi)) { | |
return false; | |
} else { | |
this.pengguna = db; | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
* | |
* http://stripbandunk.com/ | |
* | |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
*/ | |
package com.stripbandunk.formlogin.entity; | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import org.hibernate.annotations.ColumnTransformer; | |
@Entity | |
@Table(name = "tabel_pengguna") | |
public class Pengguna implements Serializable { | |
@Id | |
@Column(name = "nama_pengguna") | |
private String namaPengguna; | |
// ubah kata password menjadi kata yang rahasia menurut Anda | |
@ColumnTransformer(read = "decode(kata_sandi, 'password')", write = "encode(?, 'password')") | |
@Column(name = "kata_sandi") | |
private String kataSandi; | |
// getter dan setter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-core</artifactId> | |
<version>3.6.7.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-jdk14</artifactId> | |
<version>1.6.1</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>5.1.17</version> | |
</dependency> | |
<dependency> | |
<groupId>javassist</groupId> | |
<artifactId>javassist</artifactId> | |
<version>3.12.1.GA</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment