我正在尝试捕获和存储用户详细信息(名称、密码等)并将它们存储在 sql 数据库中,以便用户将来登录时使用。
如何将详细信息保存为数据库中的一行,但将它们作为单独的字符串输入?另外,我如何捕获和存储从组合框中选择的性别(男、女)等详细信息?谢谢。
private class achandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
//capture user details
String fnamevalue = fnametext.getText();
String lnamevalue = lnametext.getText();
String usernamevalue = usernametext.getText();
char[] pwdvalue = pwdtext.getPassword();
char[] pwdrptvalue = pwdrpttext.getPassword();
if(pwdvalue == pwdrptvalue) {
//add user details to an array
String[] userdetail = {fnamevalue, lnamevalue, usernamevalue, pwdvalue.toString()};
try {
Class.forName("com.mysql.jdbc.Driver");
String Url = "jdbc:mysql://localhost/nsibirwa?" +
"user=root&password=1234";
Connection con = DriverManager.getConnection(Url);
Statement stmt = con.createStatement();
//I am stuck here!!!! i.e. adding 'userdetail' as a tuple in the database
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
else {
JOptionPane.showMessageDialog(null, "password not matching!",
"Password error", JOptionPane.ERROR_MESSAGE);
}
}
}