2015-04-02 126 views
0

我有兩個窗口。一個登錄窗口(稱爲main;我仍然只是測試)和一個MainWindow。我正在從MS Access數據庫讀取登錄數據(用戶名,密碼)。該數據庫有四個表格,Customer,Accounts,Mortgage,CreditCard。目前爲止一切正常,但我試圖在成功登錄時將用戶的名字和姓氏傳遞給主窗口中的JLabel。我該怎麼做呢?用戶登錄時是否需要客戶索引?在我可以在幀之間傳遞它之前,是否需要將它存儲在數組中?如果是這樣,我該怎麼做?我仍然在學習,所以一個例子會很棒。使用java swing將數據庫存儲到數組中

我假設我的數組看起來像這樣?

Account accounts[] = new Account[2]; 
Customer people[] = new Customer[2]; 
CreditCard credit[] = new CreditCard[2]; 
Mortgage mortgage[] = new Mortgage[2]; 
int index = 0; 

這是我的登錄類

import java.sql.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class Login extends JFrame 
{ 
Connection con; 
Statement st; 
ResultSet rs; 

JFrame f = new JFrame("User Login"); 
JLabel l = new JLabel("Username"); 
JLabel l1 = new JLabel("Password"); 
JTextField t = new JTextField(10); 
JTextField t1 = new JTextField(10); 
JButton b = new JButton("Login"); 

public Login() 
{ 
    connect(); 
    frame(); 

} 

public void connect() 
{ 
    try 
    { 

     //Connect to the database 
     con = DriverManager.getConnection("jdbc:ucanaccess://G:/JSProject/prjTIMS_JS/Bank.mdb"); 
     st = con.createStatement(); 
     System.out.println("Connected database successfully ..."); 
    } 
    catch(Exception ex) 
    { 
    } 
} 

public void frame() 
{ 
    f.setSize(600,100); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setVisible(true); 

    JPanel p = new JPanel(); 
    p.add(l); 
    p.add(t); 
    p.add(l1); 
    p.add(t1); 
    p.add(b); 

    f.add(p); 

    b.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      try 
      { 
      String username = t.getText().trim(); 
      String password = t1.getText().trim(); 

      String sql = "select username, password from tblCustomer where username = '" +username+"'and password = '"+password+"'" ; 
      rs = st.executeQuery(sql); 

      int count = 0; 
      while(rs.next()) 
      { 
       count ++; 
      } 

      if(count == 1) 
      { 
       setVisible(false); 
       new MainWindow(); 
      } 
      else if(count > 1) 
      { 
       JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied!"); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, "User Not Found!"); 
      } 




      } 

      catch(Exception ex) 
      { 
       JOptionPane.showMessageDialog(null, "Catch Error");   
      } 
     } 
    }); 
} 

public static void main(String[] args) { 

    new Login(); 

}} 

主窗口

import java.awt.*; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 


public class MainWindow extends JFrame { 

MainWindow() { 
    //Create a new frame container 
    setTitle("Jo's Dealership: Main Menu"); 


    //Give the frame an initial size and center the window 
    setSize(390, 270); 
    setLocationRelativeTo(null); 

    //Terminate the program when the user closes the application 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    ImageIcon image = new ImageIcon("pc.png"); 
    JLabel lblImage = new JLabel(image); 
    lblImage.setPreferredSize(new Dimension(48, 48)); 

    JLabel lblOptionMsg = new JLabel(" choose an option below."); 
    lblOptionMsg.setFont(new Font("Helvetica", Font.PLAIN, 11)); 
    lblOptionMsg.setForeground(new Color(37, 75, 124)); 

    //Make two buttons 
    JButton okButton = new JButton ("OK"); 
    okButton.setFont(new Font("Helvetica", Font.BOLD, 12)); 
    okButton.setOpaque(false); 
    okButton.setPreferredSize(new Dimension(85, 22)); 
    okButton.setForeground(new Color(37, 75, 124)); 

    JButton cancelButton = new JButton ("Logout"); 
    cancelButton.setFont(new Font("Helvetica", Font.BOLD, 12)); 
    cancelButton.setOpaque(false); 
    cancelButton.setPreferredSize(new Dimension(85, 22)); 
    cancelButton.setForeground(new Color(37, 75, 124)); 

    JRadioButton buyButton = new JRadioButton("Buy Car", true); 
    buyButton.setMnemonic(KeyEvent.VK_B); 
    buyButton.setActionCommand("Buy"); 
    buyButton.setFont(new Font("Helvetica", Font.BOLD, 11)); 
    buyButton.setOpaque(true); 
    buyButton.setPreferredSize(new Dimension(120, 22)); 
    buyButton.setForeground(new Color(37, 75, 124)); 

    JRadioButton calcButton = new JRadioButton("Calculate Commission", true); 
    calcButton.setMnemonic(KeyEvent.VK_C); 
    calcButton.setActionCommand("Calculate"); 
    calcButton.setFont(new Font("Helvetica", Font.BOLD, 11)); 
    calcButton.setOpaque(true); 
    calcButton.setPreferredSize(new Dimension(200, 22)); 
    calcButton.setForeground(new Color(37, 75, 124)); 

    ButtonGroup bG = new ButtonGroup(); 
    bG.add(buyButton); 
    bG.add(calcButton); 
    buyButton.setSize(100,200); 
    calcButton.setSize(100,200); 

    //Create text based label 
    JLabel lblMessage = new JLabel("[CustomerNameHere] is currently logged on."); 
    lblMessage.setFont(new Font("Helvetica", Font.PLAIN, 11)); 
    lblMessage.setForeground(new Color(37, 75, 124)); 

    //Add the panel to the content page 
    JPanel panel = new JPanel(); 
    add(panel); 
    panel.setBackground(new Color(239, 239, 239)); 

    //Display the frame 
    setVisible(true); 

    //Set the layout 
    SpringLayout layout = new SpringLayout(); 
    panel.setLayout(layout); 

    //Add the label to the content pane 
    panel.add(lblImage); 
    panel.add(lblMessage); 
    panel.add(lblOptionMsg); 
    panel.add(okButton); 
    panel.add(cancelButton); 
    panel.add(buyButton); 
    panel.add(calcButton); 

    //Adjust constraints for the components 
    layout.putConstraint(SpringLayout.WEST, lblImage, 150, SpringLayout.WEST, panel); 
    layout.putConstraint(SpringLayout.NORTH, lblImage, 50, SpringLayout.NORTH, panel); 
    layout.putConstraint(SpringLayout.WEST, lblMessage, 75, SpringLayout.WEST, panel); //right 
    layout.putConstraint(SpringLayout.NORTH, lblMessage, 10, SpringLayout.NORTH, panel); //top 
    layout.putConstraint(SpringLayout.WEST, okButton, 90, SpringLayout.WEST, panel); 
    layout.putConstraint(SpringLayout.NORTH, okButton, 185, SpringLayout.NORTH, panel); 
    layout.putConstraint(SpringLayout.WEST, cancelButton, 195, SpringLayout.WEST, panel); 
    layout.putConstraint(SpringLayout.NORTH, cancelButton, 185, SpringLayout.NORTH, panel); 
    layout.putConstraint(SpringLayout.WEST, buyButton, 95, SpringLayout.WEST, panel); 
    layout.putConstraint(SpringLayout.NORTH, buyButton, 105, SpringLayout.NORTH, panel); 
    layout.putConstraint(SpringLayout.WEST, calcButton, 95, SpringLayout.WEST, panel); 
    layout.putConstraint(SpringLayout.NORTH, calcButton, 135, SpringLayout.NORTH, panel); 
    layout.putConstraint(SpringLayout.WEST, lblOptionMsg, 105, SpringLayout.WEST, panel); 
    layout.putConstraint(SpringLayout.NORTH, lblOptionMsg, 25, SpringLayout.NORTH, panel); 
} 

public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      //new MainWindow(0).setVisible(true); 
     } 
    }); 
} 
} 

回答

0

這裏有一個簡單的方法,如果你想通過第一和登錄的用戶到主窗口姓。

首先,修改您的查詢來獲取姓氏和名字,當你驗證,如果用戶在數據庫中存在:

String sql = "select FName, LName from tblCustomer where username = '" +username+"'and password = '"+password+"'" ; 
rs = st.executeQuery(sql); 

int count = 0; 
String lastName =''; 
String firstName = ''; 
while(rs.next()) 
{ 
    count ++; 
    //get last name and first name when connected 
    lastName = rs.getString("LName"); 
    firstName = rs.getString("FName"); 
} 

然後:

if(count == 1) 
{ 
    setVisible(false); 
    new MainWindow(firstName+" "+lastName);//<--pass it here to the mainWindow 
} 

Finaly,在主窗口:

public class MainWindow extends JFrame { 
    private String whosLoggedOn; 
    MainWindow(String whosLoggedOn) { 
     this.whosLoggedOn = whosLoggedOn; 

     //------ other stuff-------- 

     //Create text based label 
     JLabel lblMessage = new JLabel(this.whosLoggedOnis+ " is currently logged on."); 
     //------ other stuff-------- 

    } 
    } 

編輯1: 如果你想通過從其它表的其他信息,例如tblCustomertblAccount,你可以修改主窗口的構造是這樣的:

public class MainWindow extends JFrame { 
    MainWindow(ResultSet tblCustomer, ResultSet tblAccount) { 


     //use them as you want here 
     //for example to get last and first name : 
      String lastName = tblCustomer.getString("LName"); 
      String firstName = tblCustomer.getString("FName"); 

     //Create text based label 
     JLabel lblMessage = new JLabel(lastName +" "+ firstName" is currently logged on."); 
     //------ other stuff-------- 

    } 
    } 

現在,在您的登錄JFrame你只需要查詢表,你連接時需要將ResultSet傳遞給mainWindow

+0

謝謝@blackbishop(!!!)工作得很好。我還有其他的表格添加了信息,所以我可以使用這個過程,並以這種方式傳遞我的數據? – jenayaalisa 2015-04-03 00:10:12

+0

我跟着你的步驟來加載另一個表,但我正在碰到一個障礙。我有另一張桌子。示例:String sqlAccount =「SELECT custID,checkingAcctNum,savingsAcctNum,soarAcctNum FROM tblAccount ....我無法在MainWindow中傳遞firstName和lastName,並再次使用whosLoggedOn。我不知道如何將兩個表傳遞給主窗口,你能幫助我嗎?謝謝。 – jenayaalisa 2015-04-03 01:47:34

+0

我試着查詢窗口將ResultSet傳遞給其他窗口,但我得到了「Catch error」消息。難道說我沒有正確地執行查詢?當我回到初次展示給我的方式時,一切都奏效了: - / – jenayaalisa 2015-04-16 06:18:35

相關問題