[英]Having trouble connecting to my database server: ArrayIndexOutOfBoundsException
[英]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);
當我嘗試登錄時,此消息出現“空結果集上的 lllegal 操作”或當我刪除 //“參數索引超出范圍(1> 參數數量,即 0)時。” 我認為錯誤在
pst.setString(1, getuserName());
//或者
label.setText(rs.getString(1) + " welcome ");
從您的錯誤消息中可以清楚地看出,您的查詢返回的結果集包含 0 行和 1 列。 我假設您想將m_pass
列與 JAVA function 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
}
閱讀有關PreparedStatement和JDBC ResultSet的更多信息。 希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.