如何在JTextfield中显示值?

时间:2016-04-13 20:21:07

标签: java database user-interface jtextfield

我尝试从数据库中提取用户的信息,并在用户打开其主JTextField上的信息时显示在3 GUI中。我有子GUI工作,但值没有显示。

这是子菜单的代码。

protected void Info() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 585, 387);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Name");
    lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel.setBounds(97, 11, 46, 14);
    contentPane.add(lblNewLabel);

    textField = new JTextField();
    textField.setBounds(20, 30, 199, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_3 = new JTextField();
    textField_3.setBounds(242, 30, 86, 20);
    contentPane.add(textField_3);
    textField_3.setColumns(10);

    textField_2 = new JTextField();
    textField_2.setBounds(338, 30, 86, 20);
    contentPane.add(textField_2);
    textField_2.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel("Grade Level");
    lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel_1.setBounds(242, 11, 76, 14);
    contentPane.add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("Reading Level");
    lblNewLabel_2.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel_2.setBounds(338, 11, 93, 14);
    contentPane.add(lblNewLabel_2);

    try{
        String query= ("select FirstName,GradeLevel,ReadingLevel from Student where FirstName=? AND GradeLevel=? AND ReadingLevel=?");

        PreparedStatement statement=connection.prepareStatement(query);
        Connection connection=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Butch\\Desktop\\Database\\Books.sqlite");
        statement.setString(1,textField.getText());
        statement.setString(2,textField_3.getText());
        statement.setString(3,textField_2.getText());

        ResultSet resultset=statement.executeQuery();
        if(resultset.next())
        {
            String FN=resultset.getString("FirstName");
            textField.setText(FN);
            String GL=resultset.getString("GradeLevel");
            textField_3.setText(GL);
            String RL=resultset.getString("ReadingLevel");
            textField_2.setText(RL);        
        }       
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

我是否缺少正确运行代码所需的部分代码?

0 个答案:

没有答案