|
|
|
java gui เก็บค่าไว้ใน Class แล้วส่งค่าข้ามฟอร์มยังไง เคยอ่านในกระทู้แล้วแต่ก็ไม่เข้าใจ |
|
|
|
|
|
|
|
ผมต้องการ login เเล้วเก็บค่าไว้ใช่ใน form ต่างๆ
ระบบมีดังนี้ login เข้าไปผ่านหน้า menu เเล้วเลือกหน้า Withdraw เเสดงข้อมูลต่างๆของคนที่ login เหมือนกับ Session
เมื่อก่อนใช่การพิมพ์ เลขบัญชีแล้วตรวจสอบเพื่อให้ข้อมูลแสดง
Code (Java)
public class MainLogin extends javax.swing.JFrame {
public MainLogin() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","1234");
String sql = " SELECT * FROM account WHERE Acc = ? AND Pin = ? ";
PreparedStatement pst = con.prepareStatement(sql);
pst = con.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
ResultSet rs = pst.executeQuery();
rs = pst.executeQuery();
if(rs.next()){
MainMenu menu = new MainMenu();
menu.setVisible(true);
setVisible(false);
}
else{
JOptionPane.showMessageDialog(null,"เลขบัญชีหรือรหัสผ่านผิดพลาด");
jTextField1.setText("");
jTextField2.setText("");
}
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
Code (Java)
public class MainLogin extends javax.swing.JFrame {
public MainLogin() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","1234");
String sql = " SELECT * FROM account WHERE Acc = ? AND Pin = ? ";
PreparedStatement pst = con.prepareStatement(sql);
pst = con.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
ResultSet rs = pst.executeQuery();
rs = pst.executeQuery();
if(rs.next()){
MainMenu menu = new MainMenu();
menu.setVisible(true);
setVisible(false);
Variable user = new Variable();
user.settest1(rs.getString("Acc"));
}
else{
JOptionPane.showMessageDialog(null,"เลขบัญชีหรือรหัสผ่านผิดพลาด");
jTextField1.setText("");
jTextField2.setText("");
}
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
Code (Java)
public class Withdraw extends javax.swing.JFrame {
Connection conn; // Connection to DB
ResultSet rs; // Resultset for Push
PreparedStatement pst; // Set
public Withdraw() {
super("withdrwal");
initComponents();
conn =Connect.ConnecrDB();
jButton1.setEnabled(false);
jButton2.setEnabled(false);
jButton3.setEnabled(false);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// exit Button
JOptionPane.showMessageDialog(null,"กลับหน้าเมนู");
new MainMenu().setVisible(true);
this.setVisible(false);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// Withdraw Button
try{
if (jTextField7.getText()!="ไม่สามารถถอนเงินได้"){
String v1=jTextField1.getText();
String v2=jTextField7.getText();
// String value3=jTextField3.getText();
String sql="update Account set Balance ='"+ v2 +"'where Acc='"+ v1 +"'";
pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null,"จำนวนเงินถูกถอนแล้ว!");
}
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
} catch (Exception e){
JOptionPane.showMessageDialog(null,e);
}
try{
rs.close();
pst.close();
}catch(Exception e){ // fetcher for basura codes and exception for java
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// View Button
// Compute Button
try{
if (Integer.valueOf(jTextField6.getText())<100){
jTextField7.setText("ไม่สามารถถอนเงินได้");
JOptionPane.showMessageDialog(null, "ไม่สามารถถอนเงินได้ ยอดเงินต่ำกว่า 100 บาท");
}else{
//String v1=jTextField5.getText();
//String v2=jTextField6.getText();
//int sub = Integer.parseInt(v2)- Integer.parseInt(v1);
//int sub = (Integer.parseInt(v2))- (Integer.parseInt(v1));
int sub = Integer.valueOf(jTextField5.getText()) - Integer.valueOf(jTextField6.getText());
if (sub<0){
jTextField7.setText("ไม่สามารถถอนเงินได้");
JOptionPane.showMessageDialog(null, "ไม่สามารถถอนเงินได้");
}else{
String sub1 = String.valueOf(sub);
jTextField7.setText(sub1);
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
jButton1.setEnabled(false);
jButton2.setEnabled(true);
jButton3.setEnabled(true);
}
private void jTextField6KeyTyped(java.awt.event.KeyEvent evt) {
// event that will only accept Numbers
char a=evt.getKeyChar();
if(!(Character.isDigit(a) || (a==KeyEvent.VK_BACK_SPACE) || a==KeyEvent.VK_DELETE)){
getToolkit().beep();
evt.consume();
}
}
private void jTextField6KeyReleased(java.awt.event.KeyEvent evt) {
// enable view button
jButton1.setEnabled(false);
jButton2.setEnabled(true);
jButton3.setEnabled(false);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Check Button for Balance Inquiry
String sql="select * from Account where Acc=?";
try{
pst=conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
rs=pst.executeQuery();
if(rs.next()){
String add1=rs.getString("Name"); // this is name of the DB Variable
jTextField2.setText(add1);
String add2=rs.getString("Cardno"); // this is name of the DB Variable
jTextField3.setText(add2);
String add3=rs.getString("Ctype"); // this is name of the DB Variable
jTextField4.setText(add3);
String add4=rs.getString("Balance"); // this is name of the DB Variable
jTextField5.setText(add4);
}
rs.close();
pst.close();
}catch (Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// enable when mouse click
jButton1.setEnabled(false);
jButton2.setEnabled(false);
jButton3.setEnabled(false);
}
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
// event that will only accept Numbers
char a=evt.getKeyChar();
if(!(Character.isDigit(a) || (a==KeyEvent.VK_BACK_SPACE) || a==KeyEvent.VK_DELETE)){
getToolkit().beep();
evt.consume();
}
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
// activate button when characters are present
jButton1.setEnabled(true);
jButton2.setEnabled(false);
jButton3.setEnabled(false);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// Clear Button for change pin
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
}
Tag : Java, MySQL, JavaScript, JAVA
|
ประวัติการแก้ไข 2020-02-04 21:14:17 2020-02-04 21:16:52
|
|
|
|
|
Date :
2020-02-04 21:12:30 |
By :
poom123456 |
View :
1483 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
กระทู้ที่อ่านมาเเล้ว
https://www.thaicreate.com/java/java-gui-example-pass-variable-between-frame.html
|
|
|
|
|
Date :
2020-02-04 21:20:43 |
By :
poom123456 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แบบที่ 1 ประกาศตัวแปรเป็นแบบ GLOBAL ซึ่ง Class ไหนๆ ก็เรียกใช้ได้
แบบที่ 2 ส่งตัวแปร ในรูปแบบ Parameter ไปยัง Class ต่างๆ
|
|
|
|
|
Date :
2020-02-04 22:52:06 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|