[英]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.