2017-06-16 7 views
0

當我使用下面的示例代碼向數據庫輸入數據時,控制檯上會顯示System.out.println("Insertion complete")行。當我進入數據庫時​​,我發現存儲的數據是空白的。將數據插入數據庫時​​遇到問題

我認爲這個問題可能是我的sqlquery,但我沒有看到任何問題。

這裏是我的代碼示例:

public class SignUpFrame extends JFrame { 

    public SignUpFrame() { 

     super("SIGN UP"); 
     setLayout(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(400, 400); 
     setVisible(true); 
     setResizable(false); 
     setLocation(500, 250); 
     getContentPane().setBackground(Color.WHITE);  

     ImageIcon FrameIcon = new ImageIcon("images/signup.png"); 
     JLabel frameicon = new JLabel(FrameIcon); 
     frameicon.setBounds(150, 0, 100, 100); 
     add(frameicon); 


     JLabel firstNamelbl,LastNameLbl,EmailAddressLbl,PasswordLbl,ConfirmPasswordLbl; 

     JTextField firstNametxt,LastNametxt,EmailAddresstxt; 
     JPasswordField passwordtxt,ConfirmPasswordtxt; 
     JButton OKbtn; 
     JButton CANCELbtn; 

     firstNamelbl = new JLabel("FIRST NAME:"); 
     firstNametxt = new JTextField(); 
     firstNametxt.setBounds(150, 110, 200, 20); 
     firstNamelbl.setBounds(50, 110, 100, 20); 

     LastNameLbl = new JLabel("USERNAME:"); 
     LastNametxt = new JTextField(); 
     LastNametxt.setBounds(150, 140, 200, 20); 
     LastNameLbl.setBounds(50,140, 100, 20); 

     EmailAddressLbl = new JLabel("EMAIL ADDRESS:"); 
     EmailAddresstxt = new JTextField(); 
     EmailAddresstxt.setBounds(150, 170, 200, 20); 
     EmailAddressLbl.setBounds(50, 170, 150, 20); 

     PasswordLbl = new JLabel("PASSWORD:"); 
     passwordtxt = new JPasswordField(); 
     passwordtxt.setBounds(150, 200, 200, 20); 
     PasswordLbl.setBounds(50, 200, 150, 20); 

     ConfirmPasswordLbl = new JLabel("CONFIRM PASSWORD:"); 
     ConfirmPasswordtxt = new JPasswordField(); 
     ConfirmPasswordtxt.setBounds(200, 230, 150, 20); 
     ConfirmPasswordLbl.setBounds(50, 230, 150, 20); 

     add(firstNamelbl); 
     add(firstNametxt); 

     add(LastNameLbl); 
     add(LastNametxt); 

     add(EmailAddressLbl); 
     add(EmailAddresstxt); 

     add(PasswordLbl); 
     add(passwordtxt); 

     add(ConfirmPasswordLbl); 
     add(ConfirmPasswordtxt); 

     OKbtn = new JButton("OK"); 
     OKbtn.setBounds(100, 270, 50, 20); 
     add(OKbtn); 
     char [] pass = passwordtxt.getPassword(); 
     String passWord = new String(pass); 


     String FIRSTNAME,USERNAME,EMAIL,PASSWORD; 
     FIRSTNAME = firstNametxt.getText().toString(); 
     USERNAME =LastNametxt.getText().toString(); 
     EMAIL = EmailAddresstxt.getText().toString(); 
     PASSWORD = passWord; 




     OKbtn.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 


       String url = "jdbc:mysql://localhost:3306/System"; 
       String user = "root"; 
       String password = ""; 

       Connection con; 
       try { 
        con = DriverManager.getConnection(url, user, password); 
        Statement statement = con.createStatement(); 
        String sqlquery = "insert into signUp_tb "+ 
             "(FirstName,UserName,Email,Password) values ('"+FIRSTNAME+"','"+USERNAME+"','"+EMAIL+"','"+PASSWORD+"')"; 

        statement.executeUpdate(sqlquery); 
        System.out.println("Insertion complete"); 
       } catch (SQLException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 

      } 
     }); 




     CANCELbtn = new JButton("CANCEL"); 
     CANCELbtn.setBounds(170, 270, 100, 20); 
     add(CANCELbtn); 

     CANCELbtn.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       setVisible(false); 
       dispose(); 

      } 
     }); 

    } 


} 
+0

這是什麼'的Statement.executeUpdate(sqlquery的);'返回? –

+0

你可能無法獲得連接實例。添加Class.forName(「the-driver-name」)來加載驅動程序。 –

+1

你應該考慮把你的SQL邏輯關閉到EDT。如果您的連接/查詢掛起,您不希望這會凍結GUI。 –

回答

3

需要初始化你FIRSTNAMEUSERNAME等在ActionListener的OK按鈕。當沒有輸入數據時,您已初始化那些Strings,這就是爲什麼一切都是空白的。當用戶按下按鈕時,您想要獲得JTextFields。之前沒有。

在SQLite上測試。

OKbtn.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     char [] pass = passwordTxt.getPassword(); 
     String PASSWORD = new String(pass); 

     String FIRSTNAME,USERNAME,EMAIL; 
     FIRSTNAME = firstNameTxt.getText(); 
     USERNAME = lastNameTxt.getText(); 
     EMAIL = emailAddressTxt.getText(); 


     String url = "jdbc:mysql://localhost:3306/System"; 
     String user = "root"; 
     String password = ""; 

     Connection con; 
     try { 
      con = DriverManager.getConnection(url, user, password); 
      Statement statement = con.createStatement(); 
      String sqlquery = "insert into signUp_tb "+ 
           "(FirstName,UserName,Email,Password) values ('"+FIRSTNAME+"','"+USERNAME+"','"+EMAIL+"','"+PASSWORD+"')"; 

      statement.executeUpdate(sqlquery); 
      System.out.println("Insertion complete"); 
     } catch (SQLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

    } 
}); 

setVisible(true)應該是你做的最後一件事。應該看起來像:

SwingUtilities.invokeLater(() -> { 
    this.setVisible(true); 
}); 

所以你會從EDT啓動它。

編輯:關於setLayout(null) - 嘗試那些A Visual Guide to Layout Managers

雖然可能沒有佈局管理器做的,你應該使用 佈局管理器,如果在所有可能的。版面管理器可以更輕鬆地將 調整爲外觀依賴的組件外觀,不同的字體大小,容器的不斷變化的大小以及不同的 區域設置。佈局管理器也可以容易地被其他的容器以及其他程序重複使用。

Doing Without a Layout Manager (Absolute positioning)

+0

非常感謝它爲我工作 –

+0

@BradleyJuma沒問題。實際上,你不需要調用'.getText()。toString()'。 'getText()'已經可以滿足你的需求了,而且你的代碼會更容易閱讀。 – bigby

相關問題