emrys
emrys

Reputation: 1

Adding values entered in java program(in a form) to sql database:

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

     }
   }

Upvotes: 0

Views: 2953

Answers (1)

user330315
user330315

Reputation:

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.

Upvotes: 2

Related Questions