简体   繁体   English

如何让我的程序在 JtextField 中显示数据库中的用户名?

[英]How can I make my program display the user's name from the database in JtextField?

I'm making a program where you login and it takes you into a into a different frame after you log in.. That part of the program works, but I'm having trouble getting it to return the user's name and other data from the database.我正在制作一个程序,您可以在其中登录,并在您登录后将您带入不同的框架。程序的那部分工作正常,但我无法让它从用户名和其他数据返回数据库。 It connects to the database, but it won't return the information inside JTextField.它连接到数据库,但不会返回 JTextField 中的信息。 If I can find out how to do firstName, I can figure out the rest.如果我能找出如何做 firstName,我就能弄清楚其余的。 I'm using Eclipse as my IDE and SQLite Manager as the database.我使用 Eclipse 作为我的 IDE,使用 SQLite Manager 作为数据库。 There are 2 tables Login (username,password) Student(SID,firstName,GradeLevel, and more) Also username is there ID start with an S (like S01 and so forth).有 2 个表 Login (username,password) Student(SID,firstName,GradeLevel, and more) 还有 username 是以 S 开头的 ID(比如 S01 等等)。 Here's the code.这是代码。

public class student extends JFrame {
private JTextField textField;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                student frame = new student();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
private void ShowInfo() {

try{


    String query="SELECT firstName From Student,Login Where firstname=?";
    PreparedStatement ps=conn.prepareStatement(query);
    ps.setString(1, "firstName");
    ResultSet rs= ps.executeQuery();
    while (rs.next()) {
    textField.setText(rs.getString("firstName"));
        System.out.print(""+textField);
    }ps.close();
    conn.close();
    } catch ( Exception ex)
    {

        JOptionPane.showMessageDialog(null,ex);
}


}

} }

private void ShowInfo() {

try{
    String query="SELECT firstName From Student,Login Where firstname=?";
    PreparedStatement ps=conn.prepareStatement(query);
    ps.setString(1, "firstName");
    ResultSet rs= ps.executeQuery();
    if(rs.next()) {
    textField.setText(rs.getString(1));
    System.out.print(""+textField); //you are getting data
    }ps.close();
    conn.close();
    } catch ( Exception ex)
    {
        JOptionPane.showMessageDialog(null,ex); //you have a error
}
}

there were sevral thing which were wrong in the code.代码中有几处错误。 first one is there should be something wrong with sql statment Student,Login Where firstname .第一个是 sql statment Student,Login Where firstname应该有问题。 you should change this line textField.setText(rs.getString(1));你应该改变这一行textField.setText(rs.getString(1)); and you have a while loop to extract the data but and you are planning on having a textfield to store data.并且您有一个while loop来提取数据,但是您正计划使用textfield来存储数据。 that is pointless if you are expecting more than one output from your resultset you need something more than a jtextfield maybe a jtable.如果您期望结果集中有多个输出,那么这毫无意义,您需要的不仅仅是 jtextfield 或者 jtable。 bt i have changed your while loop to a if loop to get one single output from the resultset.and the other thing i notice in your application is you are not calling the showInfo method. bt 我已将您的 while 循环更改为if loop以从结果集中获取一个输出。我在您的应用程序中注意到的另一件事是您没有调用showInfo方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用Jtextfield在对象数组中命名所有对象 - how i can name all my objectes in array of objectes with Jtextfield 如何使JTextField的宽度固定? - How Can I Make a JTextField's Width Static? 如何在用户输入时获取JTextField内容的长度? - How can I get the length of a JTextField's contents as the user types? 如何让我的程序让用户在运行其余代码之前在JtextField中输入文本? (Java 8) - How to make my program let the user enter text in JtextField before running the rest of the code? (Java 8) 如何从JTextField中读取System.in - How can i make System.in read from a JTextField 如何将 JTextField 中的数据保存到 mysql 数据库中? - How can I save data from JTextField into the mysql database? 我怎样才能从我的Sqlite数据库中随机向用户显示GROUP内容 - How can i Randomly display GROUP of content to user from my Sqlite database 我如何使用jTextField在数据库中搜索 - how i can search in database using jTextField 我如何透明我的JTextField和JLabel - how can i transparent my JTextField and JLabel 如何仅获取用户在 numGames() 中输入的值并避免程序打印“你叫什么名字?” 再次? - How can I get only the value entered by the user in numGames() and avoid the program from printing “What's your name?” again?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM