2

I am doing a project on Hotel Management whose GUI is being designed using Swings and SQl Server Management Studio,2008 to store the data.But the problem I am facing is,i am getting an exception as "Driver does not support this function"...I am not able to sort out this problem...kindly enlighten me where I am going wrong..Thanks in advance..:)

I have created 2 forms:SignUp form and Login form...Here is my SignUp form where I am stuck...

btnSubmit = new JButton("SUBMIT");
    btnSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
         try{
             if(textField.getText().equals("") || textField_2.getText().equals("") ||     
             textField_5.getText().equals("") || textField_6.getText().equals("") || 
             textField_7.getText().equals("") || passwordField.getPassword().equals("") 
             || passwordField_1.getPassword().equals("")){
                    JOptionPane.showMessageDialog(null,"Fields cannot be left 
              empty!!!"); 
                }
             else{
                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 Connection con=DriverManager.getConnection("jdbc:odbc:SignUp_DSN");
                 String firstname=textField.getText();
                 String lastname=textField_1.getText();
                 String email_id=textField_2.getText();
                 String country=textField_5.getText();
                 String state=textField_6.getText();
                 String ph_no=textField_7.getText();
                 char[] password=passwordField.getPassword();
                 char[] retype_password=passwordField_1.getPassword();

            if(!password.equals(retype_password)){
                     JOptionPane.showMessageDialog(null,"Passwords are not 
                     matching.Enter again!!!"); } 


                if(password.length<8 || retype_password.length<8){
                     JOptionPane.showMessageDialog(null,"Password should be more than 8 
                     characters!!!");
                }
            String sql="insert into  Sign_Up(`Firstname`,`Lastname`,`Email_id`,`Password`,`Retype_Password`,`Country`,`State`,`Phone_no`) values(?,?,?,?,?,?,?,?)";
                  PreparedStatement ps=con.prepareStatement(sql);
                     ps.setString(1, firstname);
                     ps.setString(2, lastname);
                     ps.setString(3, email_id);
                     ps.setString(6, country);
                     ps.setString(7, state);
                     ps.setString(8,ph_no);
                     ps.setString(4, new String(password));
                     ps.setString(5, new String(retype_password) ); 
                     ResultSet rs=ps.executeQuery(sql);
                      while(rs.next()){ }
                con.close(); 
                    ps.close();
                    //rs.close();
                 }
    }catch(Exception ex){

                    String str=ex.toString();
                    JOptionPane.showMessageDialog(null,str);
            }
        }
});

And also the condition for Password matching is not working...I get a Dialogue message saying passwords doesn't match always;whether the password match or not!!!

user3801050
  • 21
  • 1
  • 5
  • 3
    Unrelated but: don't use the JDBC/ODBC bridge. It has always been buggy and slow and it is no longer available in Java 8. Use a real JDBC driver instead. –  Jul 18 '14 at 06:36

4 Answers4

4

I think I see the problem,

PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, firstname);
ps.setString(2, lastname);
ps.setString(3, email_id);
ps.setString(6, country);
ps.setString(7, state);
ps.setString(8,ph_no);
ps.setString(4, new String(password));
ps.setString(5, new String(retype_password) ); 
ResultSet rs=ps.executeQuery(sql); // <-- here.

You set-up your PreparedStatement query and bind the parameters, but then you call the unbound query again when you pass String sql to executeQuery()!

ResultSet rs=ps.executeQuery();

Also, you should add a finally block to close rs and ps.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I l try..and get back in a min..thnx. – user3801050 Jul 18 '14 at 06:39
  • @user3801050 Move the variable `conn`, `rs` and `ps` variable declarations outside the try block (declare as null and assign in the try, or use the new [try with resources](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)). – Elliott Frisch Jul 18 '14 at 06:43
  • Exception is gone...but the data is not getting stored in the database when I input in the GUI form..Can anybdy suggest me where I am going wrng!!! :( – user3801050 Jul 18 '14 at 07:12
  • I did exactly as u said...still not working..:( nothing is getting stored in database..!!! – user3801050 Jul 18 '14 at 07:25
  • Post another question with the query, your exception sounds resolved. Are you getting a new exception? This was a select, where is the insert? – Elliott Frisch Jul 18 '14 at 07:31
2

Your column name 'state' is a keyword. Rename the column to something else.

Jp Vinjamoori
  • 1,173
  • 5
  • 16
0

There are two questions in your one question:

  1. Why isn't the password check working? It's because you can't compare two char-Arrays using equals. Arrays don't override equals(), so it is basically a ==-check. Use Arrays.equals(password, retype_password)
  2. As Elliott said: you are already setting your sql-query in the prepareStatement - don't pass it again in executeQuery()

There are some other issues I want to point out:

  • There's no need to store password and retype_password - they are equal anyway.
  • NEVER STORE PASSWORDS IN CLEARTEXT. Always use a suitable hash-function with salt like PBKDF2
piet.t
  • 11,718
  • 21
  • 43
  • 52
0

The error is parameters not compatible.

The executeQuery function in PreparedStatement is executeQuery() without parameters

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459