无法显示我的数据库中的名称
having trouble display the name from my database
我想在另一个JFrame中用他的ID登录后打印用户名
我用了这个方法,但是没用
//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;
}
}
我想在这个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);
当我尝试登录时,出现此消息
“对空结果集的非法操作”
或者当我删除 //
“参数索引超出范围(1> 参数个数,即 0)。”
我认为错误在
pst.setString(1, getuserName());
//或
label.setText(rs.getString(1) + " welcome ");
从您的错误消息中可以清楚地看出,您的查询正在返回包含 0 行和 1 列的结果集。我假设您想将列 m_pass
与 JAVA 函数 getuserName()
的返回值进行比较(或者您可能想与用户密码进行比较)。
您可以尝试以下操作:
- 将您的查询代码更新为:
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
- 您应该始终在使用记录之前检查记录是否存在于结果集中。你可以通过
if (rs.next()) {
// record found
}
else {
// record not found
}
- 尝试使用 SQL Sever Management Studio 或任何等效工具 IDE 直接在您的数据库上执行相同的查询,并查看它是否 returns 任何行。
阅读有关 PreparedStatement and JDBC ResultSet 的更多信息。
希望对您有所帮助!
我想在另一个JFrame中用他的ID登录后打印用户名
我用了这个方法,但是没用
//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;
}
}
我想在这个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);
当我尝试登录时,出现此消息 “对空结果集的非法操作” 或者当我删除 // “参数索引超出范围(1> 参数个数,即 0)。” 我认为错误在
pst.setString(1, getuserName());
//或
label.setText(rs.getString(1) + " welcome ");
从您的错误消息中可以清楚地看出,您的查询正在返回包含 0 行和 1 列的结果集。我假设您想将列 m_pass
与 JAVA 函数 getuserName()
的返回值进行比较(或者您可能想与用户密码进行比较)。
您可以尝试以下操作:
- 将您的查询代码更新为:
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
- 您应该始终在使用记录之前检查记录是否存在于结果集中。你可以通过
if (rs.next()) {
// record found
}
else {
// record not found
}
- 尝试使用 SQL Sever Management Studio 或任何等效工具 IDE 直接在您的数据库上执行相同的查询,并查看它是否 returns 任何行。
阅读有关 PreparedStatement and JDBC ResultSet 的更多信息。 希望对您有所帮助!