0

I am trying to capture & store user details(name, password etc) & store them in sql database so as to be used by users to login in future.

How do I save the details as a row in the database, yet they are entered as individual strings? Also, how do I capture &store details like gender(Male,Female) selected from a Combobox? Thanks.

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);
            }

     }
   }
1
  • Please remember not to store the passwords in clear. A good hash function (not MD5) and a salt are essential. Commented Jun 8, 2012 at 11:05

1 Answer 1

2

adding 'userdetail' as a tuple in the database

Something like:

Connection con = DriverManager.getConnection(Url);
PreparedStatement stmt = con.prepareStatement("insert into user_details (fname, lname, username, pwd) values (?,?,?,?));
stmt.setString(1, fnamevalue);
stmt.setString(2, lnamevalue);
stmt.setString(3, username);
stmt.setString(4, new String (pwdvalue));
stmt.executeUpdate();

Your code has another problem:

if (pwdvalue == pwdrptvalue)

you can not compare objects like that. You need to convert those char[] to Strings and use equals() to compare them. Search for "java string equals" to find out why. This is very basic Java knowledge (and is covered in each and every tutorial I have seen)

This assumes you have a table named user_details with the columns fname, lname, username and pwd. If your table definition is different you need to adjust the insert statement.

Also, how do I capture &store details like gender(Male,Female) selected from a Combobox?

I would suggest you open a second question for that. You should not mix completely different topics (JDBC/SQL vs. plain Swing) in a single question.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a_horse_with_no_name. It worked wonderfully. Actually, some user details are captured using a Combobox (e.g. user selects their gender from the two options). I was hoping to find out how I can also add these to the same row along with the others in the user_details table.
@ambroz: use getSelectedItem() on the combobox and then use that value in the insert statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.