遇到了从我的数据库中显示名称的问题。

huangapple go评论85阅读模式
英文:

having trouble display the name from my database

问题

//this class to login
 public mClass() {
        // ... (previous code)

        JButton login = new JButton("登录");
        login.setBounds(245, 355, 300, 40);
        // ... (previous ActionListener code)

        panel.add(login);
        setVisible(true);
    }
    // ... (previous code)

    public String getUserName() {
        return user.getText(); // Changed to return the content of the user JTextField
    }
}

// ... (other code)

// i extend this class to the last Class
try {
    // ... (previous code)

    String sql = "SELECT m_name FROM mstable WHERE m_pass = ?";
    PreparedStatement pst = con.prepareStatement(sql);
    pst.setString(1, getUserName()); // Set the password as a parameter
    ResultSet rs = pst.executeQuery();
    
    if (rs.next()) {
        JLabel label = new JLabel();
        label.setText(rs.getString(1) + " 欢迎您");
        label.setBounds(402, -295, size.width, size.height);
        panel.add(label);
    }
} catch (SQLException ex) {
    JOptionPane.showMessageDialog(null, ex);
}
f.setVisible(true);

Please note that the translated code is provided above. If you encounter any issues or errors, you may need to check the original code for any possible mistakes.

英文:

I want to print the user name after logging in with his ID in another JFrame

I followed this method but it does not work

//this class to login
 public mClass() {
// Frame build :
        setSize(800, 600);
        setTitle(" page1  ");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width / 2 - getWidth() / 2,
                size.height / 3 - getHeight() / 3);
        //Panel & BackGround :
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);
        panel.setBackground(Color.decode("#DFDDCE"));

        // text box
        JTextField user = new JTextField();
        user.setBounds(245, 300, 300, 40);
        user.setFont(newFonts);
        user.setForeground(Color.decode("#5195E1"));
        panel.add(user);
        // text for box :
        JLabel tx = new JLabel(" Enter your ID to login.");
        tx.setBounds(245, 30, size.width, size.height);
        panel.add(tx);
        //submit button :
        JButton login = new JButton(" login ");
        login.setBounds(245, 355, 300, 40);
        login.setBorder(new LineBorder(Color.decode("#5195E1")));
           login.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            // Class.forName("com.mysql.jdbc.Driver");
                            String host = "jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC";
                            String uName = "root"; String uPass = "dataroot";
                            Connection con = DriverManager.getConnection(host, uName, uPass);
                            String password=user.getText();
                            String sql = "SELECT * FROM mstable where m_pass=? ";
                            PreparedStatement pst = con.prepareStatement(sql);
                            pst.setString(1,password);
                            ResultSet rs = pst.executeQuery();
                            if(rs.next()) {
                                new welcomeMs();
                                dispose();
                            }
                            else { JOptionPane.showMessageDialog(null,"wrong id , Please try again ");
                                 } }
                        catch ( SQLException ex) { System.out.println(ex); }
                       } });
           panel.add(login);
        //back button :
        setVisible(true);

    }
    public String getuserName() {
        return this.user;
    }
}


i want to show the name in this class

// i extend this class to the last Class
try {
            // Class.forName("com.mysql.jdbc.Driver");
            String host = "jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC";
            String uName = "root";
            String uPass = "dataroot";
            Connection con = DriverManager.getConnection(host, uName, uPass);
            String sql = "SELECT m_name FROM mstable where m_pass = 'getuserName()' ";
            PreparedStatement pst = con.prepareStatement(sql);
            //pst.setString(1, getuserName());
            ResultSet rs = pst.executeQuery();
            rs.next();
                JLabel label = new JLabel();
                label.setText(rs.getString(1) + " welcome ");
                label.setBounds(402, -295, size.width, size.height);
                panel.add(label);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,ex);
        }
f.setVisible(true);

When I try to log in, this message appears
" lllegal operation on empty result set "
or when i remove the //
" Parameter index is out of range (1> number of parameters, which is 0)."
i think error at

 pst.setString(1, getuserName());

//or

  label.setText(rs.getString(1) + " welcome ");

答案1

得分: 0

从您的错误消息中可以清楚地看出,您的查询返回了一个包含0行和1列的结果集。我假设您想要将列 m_pass 与 JAVA 函数 getuserName() 的返回值进行比较(或者您可能想要与用户密码进行比较)。

您可以尝试以下操作:

  1. 将您的查询代码更新为以下内容:
String sql = "SELECT m_name FROM mstable WHERE m_pass = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, getuserName()); // 或者您想要比较的任何字符串
  1. 在使用结果集之前,始终应检查记录是否存在。您可以通过以下方式进行检查:
if (rs.next()) {
   // 找到记录
} else {
  // 未找到记录
}
  1. 尝试在数据库中直接使用 SQL Server Management Studio 或任何等效的 IDE 执行相同的查询,看看是否返回任何行。

了解更多关于 PreparedStatementJDBC ResultSet 的信息。
希望能有所帮助!

英文:

From your error message it is clear that your query is returning resultset that contains 0 rows and 1 column. I assume that you want to compare column m_pass to returned value of JAVA function getuserName() (Or you might want to compare with user password).

You can try following:

  1. Update your query code to this:
String sql = "SELECT m_name FROM mstable where m_pass = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, getuserName()); //Or any string that you want to compare
  1. You should always check if record exists in result set before consuming it. you can do this by
if (rs.next()) {
   // record found 
}
else {
  // record not found
}
  1. Try executing same query on your database directly using SQL Sever Management Studio or any equivalent IDE and see if it returns any rows.

Read more about PreparedStatement and JDBC ResultSet.
Hope it helps!

huangapple
  • 本文由 发表于 2020年4月11日 10:25:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61151359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定