第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將數(shù)據(jù)庫(kù)連接到 GUI

將數(shù)據(jù)庫(kù)連接到 GUI

守候你守候我 2024-01-05 14:46:01
我在讓我的代碼與 GUI 一起正常工作時(shí)遇到了一些困難。我創(chuàng)建了一個(gè)連接到數(shù)據(jù)庫(kù)的簡(jiǎn)單登錄程序,該程序在 GUI 中允許用戶輸入用戶名和密碼,然后提示數(shù)據(jù)庫(kù)以查看用戶名和密碼是否存在。我創(chuàng)建了一個(gè)連接到數(shù)據(jù)庫(kù)的類和一個(gè)執(zhí)行 SQL 語(yǔ)句的 DAO 類。我還有兩個(gè)類,其中一個(gè)是接口,另一個(gè)包含稍后未實(shí)現(xiàn)的方法?,F(xiàn)在,我遇到的問(wèn)題是將 GUI 連接到我的數(shù)據(jù)庫(kù),這應(yīng)該通過(guò) DAO 類來(lái)完成。我的 GUI 有兩個(gè)文本字段,應(yīng)該用來(lái)比較用戶名和密碼,但我不知道該怎么做。另外,我的 DAO 類并未完全實(shí)現(xiàn)它,因?yàn)樗捎?int 作為參數(shù)。如果你能幫助我,我將不勝感激。UserDAO 類:public class UserDAO implements IDao<User> {    DemoDB DemoDBSingleton = null;    public UserDAO() {        DemoDBSingleton = DemoDB.getInstance();    }    @Override    public User get(int id) throws NoSuchElementException {        User user = null;        try {            ResultSet resultSet = DemoDBSingleton                    .excecuteQuery("SELECT user_name, password FROM users WHERE id=" + id);            if (!resultSet.next())                throw new NoSuchElementException("The user " + id + " doesn't exist in database");            else                DemoDBSingleton.close();        } catch (SQLException e) {            e.printStackTrace();        }        return user;    }}
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

也許如果你這樣做


public class UserDAO implements IDao<User> {


    DemoDB DemoDBSingleton = null;


    public UserDAO() {

        DemoDBSingleton = DemoDB.getInstance();

    }


    @Override


    public boolean login(String userName, String password) throws NoSuchElementException {

        boolean login = false;

        try {

            ResultSet resultSet = DemoDBSingleton

                    .excecuteQuery("SELECT user_name, password FROM users WHERE user_name='" + userName + "' AND password = '" + password + "'");

            if (resultSet.next())

                login = true;

        } catch (SQLException e) {

            e.printStackTrace();

        }

        DemoDBSingleton.close();

        return login;

    }

}

并更正您的文本字段


JTextField txtUserName = new JTextField(10);

JPasswordField txtPassword = new JPasswordField(10);

和點(diǎn)擊事件


private class ClickListener implements ActionListener {


    @Override

    public void actionPerformed(ActionEvent e) {

        UserDAO userDao = new UserDAO();


        if (userDao.login(txtUserName.getText(), txtPassword.getText())) {

            // here you redirect to your main window

        }

        else

            JOptionPane.showMessageDialog(null, "Incorrect user or password!!!");

    }


}


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)