Java GUI - ขอปรึกษาโปรเจคหน่อยครับ ของผมเป็นโปรเจคที่ทำไม่ยากมาก
Code (Java)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package authentication;
import java.awt.event.*;
import javax.swing.*;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.security.*;
/**
*
* @author Kiattisak-Petvisoot
*/
public class register extends javax.swing.JFrame {
/**
* Creates new form register
*/
public register() {
initComponents();
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lb_register = new javax.swing.JLabel();
lb_username = new javax.swing.JLabel();
lb_password = new javax.swing.JLabel();
lb_confirm = new javax.swing.JLabel();
lb_name = new javax.swing.JLabel();
lb_address = new javax.swing.JLabel();
lb_email = new javax.swing.JLabel();
lb_mobile = new javax.swing.JLabel();
txt_username = new javax.swing.JTextField();
txt_password = new javax.swing.JPasswordField();
txt_confirm = new javax.swing.JPasswordField();
txt_name = new javax.swing.JTextField();
txt_email = new javax.swing.JTextField();
txt_mobile = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
txt_address = new javax.swing.JTextArea();
bt_register = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("New Register");
lb_register.setFont(new java.awt.Font("Malayalam MN", 0, 24)); // NOI18N
lb_register.setText("New Register");
lb_username.setText("Username :");
lb_password.setText("Password :");
lb_confirm.setText("Confirm Password :");
lb_name.setText("Name :");
lb_address.setText("Address :");
lb_email.setText("E-mail :");
lb_mobile.setText("Mobile :");
txt_username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt_usernameActionPerformed(evt);
}
});
txt_address.setColumns(20);
txt_address.setRows(5);
jScrollPane1.setViewportView(txt_address);
bt_register.setText("Sign up");
bt_register.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_registerActionPerformed(evt);
}
});
jButton1.setText("Cancle");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(lb_register))
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lb_confirm)
.addComponent(lb_password)
.addComponent(lb_username)
.addComponent(lb_name)
.addComponent(lb_address)
.addComponent(lb_email)
.addComponent(lb_mobile))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(bt_register)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(txt_username)
.addComponent(txt_password)
.addComponent(txt_confirm)
.addComponent(txt_name)
.addComponent(txt_email)
.addComponent(txt_mobile)))))
.addContainerGap(19, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(lb_register)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lb_username)
.addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lb_password)
.addComponent(txt_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lb_confirm)
.addComponent(txt_confirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lb_name)
.addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lb_address)
.addGap(78, 78, 78)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lb_email)
.addComponent(txt_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_mobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lb_mobile))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bt_register)
.addComponent(jButton1))
.addGap(16, 16, 16))
);
pack();
}// </editor-fold>
private void bt_registerActionPerformed(java.awt.event.ActionEvent evt) {
// เช็คเงื่อนไข
String username = new String(txt_username.getText());
String password = new String(txt_password.getPassword());
String confirm = new String(txt_confirm.getPassword());
String name = new String(txt_name.getText());
String address = new String(txt_address.getText());
String email = new String(txt_email.getText());
String mobile = new String(txt_mobile.getText());
if (username.equals("")) {
JOptionPane.showMessageDialog(null, "Please,enter Username");
} else if (password.equals("")) {
JOptionPane.showMessageDialog(null, "Please,enter Password");
} else if (confirm.equals("")) {
JOptionPane.showMessageDialog(null, "Please,enter Confirm Password");
} else if (name.equals("")) {
JOptionPane.showMessageDialog(null, "Please,enter Name");
} else if (address.equals("")) {
JOptionPane.showMessageDialog(null, "Please,enter Adress");
} else if (email.equals("")) {
JOptionPane.showMessageDialog(null, "Please,enter Email");
} else if (mobile.equals("")){
JOptionPane.showMessageDialog(null, "Please,enter Mobile and 10 characters");
} else {
// เพิ่มข้อมูลลงฐานข้อมูล
Connection connect = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection(""
+ "jdbc:mysql://localhost:8889/project"
+ "?user=root&password=1234");
s = connect.createStatement();
// SQL Insert
String sql = "INSERT INTO authentication "
+ "(username,password,name,address,email,mobile) "
+ "VALUES ('" + username + "','"
+ password + "','"
+ name + "'" + ",'"
+ address + "','"
+ email + "','"
+ mobile + "') ";
s.execute(sql);
// Reset Text Fields
txt_username.setText("");
txt_password.setText("");
txt_confirm.setText("");
txt_name.setText("");
txt_address.setText("");
txt_email.setText("");
txt_mobile.setText("");
JOptionPane.showMessageDialog(null,
"Record Inserted Successfully");
} catch (Exception e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
try {
if (s != null) {
s.close();
connect.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int n = JOptionPane.showConfirmDialog(null, "Exit ?", "Would you want to exit?", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
System.exit(n);
} else {
System.out.print(n); // Use n for response
}
}
private void txt_usernameActionPerformed(java.awt.event.ActionEvent evt) {
// เช็ค username ว่าว่างหรือไม่
}
/**
* @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);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bt_register;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lb_address;
private javax.swing.JLabel lb_confirm;
private javax.swing.JLabel lb_email;
private javax.swing.JLabel lb_mobile;
private javax.swing.JLabel lb_name;
private javax.swing.JLabel lb_password;
private javax.swing.JLabel lb_register;
private javax.swing.JLabel lb_username;
private javax.swing.JTextArea txt_address;
private javax.swing.JPasswordField txt_confirm;
private javax.swing.JTextField txt_email;
private javax.swing.JTextField txt_mobile;
private javax.swing.JTextField txt_name;
private javax.swing.JPasswordField txt_password;
private javax.swing.JTextField txt_username;
// End of variables declaration
}
จาก code ข้างต้นน่ะครับ ผมต้องการที่จะทำดังนี้ แต่ไม่ได้ก็ไม่เป็นไรครับ ผมได้ศึกษาจากหลายๆที่แล้วน่ะครับ จนมาถึงสิ่งที่ผมจะทำต่อแล้วมันติดขัดไปไม่ถูกหรือเป็นเพราะบางทีอาจจไม่เก่งภาษาอังกฤษน่ะครับ รบกวนด้วยน่ะครับ
1 ผมต้องการให้ข้อมูลที่จะเข้าสู่ mysql ถูกเข้ารหัสน่ะครับ (อาจจะเป็น md5 sha1 หรืออะไรก็ได้ครับ ขอตัวอย่าง)
2 ผมยังไม่สามารถที่จะบันทึกให้เป็นภาษาไทยได้ เมื่อบันทึกเป็นภาษาไทย พบว่า เป็นภาษา ????
3 ผมไม่สามารถทำการเปลี่ยนเทียบระหว่าง password 2 ช่องได้ครับ ว่าตรงกันหรือไม่
รบกวนขอแนวทางด้วยน่ะครับ ขอบคุณล้วงหน้าครับ
ภาพประกอบนิดหน่อย
Tag : Java, MySQL
Date :
2015-06-01 18:20:35
By :
illmndraft
View :
3031
Reply :
7
ขอบคุณมากครับ ผมจะลองศึกษาทำดูน่ะครับ หากมีข้อสงสัยผมจะเข้ามาเม้นถามเพื่อความกระจ่างน่ะครับ
Date :
2015-06-02 11:52:36
By :
illmndraft
ข้อที่ 1 และ 2 ทำได้แล้วครับ แต่ข้อที่ 3 ยังทำไม่ได้เลยครับ ไม่ค่อยเข้าใจ
Date :
2015-06-02 15:20:26
By :
illmndraft
Load balance : Server 01