0

I am trying to create a login form with Java.

I have created the form, but seem to have trouble either connecting to the database, or retrieving the usernames and passwords from the database.

Here is the Login.java code:

    package login;

import javax.swing.*;
import java.awt.event.*;
import java.sql.*;


public class Login {


    Connection con;
    Statement st;
    ResultSet rs;

    JFrame f = new JFrame("User Login");
    JLabel l = new JLabel("Username");
    JLabel l1 = new JLabel("Password");
    JTextField t = new JTextField(10);
    JTextField t1 = new JTextField(10);
    JButton b = new JButton("Login");


    public Login()
    {
        connect();
        frame();

    }

    public void connect()
    {
        try
        {

            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            Class.forName(driver);

            String db = "jdbc:odbc:credentials";
            con = DriverManager.getConnection(db);
            st = con.createStatement();

        }
        catch(Exception ex)
        {


        }
    }

    public void frame()
    {
        f.setSize(600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        JPanel p = new JPanel();
        p.add(l);
        p.add(t);
        p.add(l1);
        p.add(t1);
        p.add(b);

        f.add(p);

        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                String username = t.getText().trim();
                String password = t1.getText().trim();

                String sql = "select user, pass from users where user = '" +username+"'and pass = '"+password+"'" ;
                rs = st.executeQuery(sql);

                int count = 0;
                while(rs.next())
                {
                    count ++;
                }

                if(count == 1)
                {

                }
                else if(count > 1)
                {
                    JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied!");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "User Not Found!");
                }




                }

                catch(Exception ex)
                {
                   JOptionPane.showMessageDialog(null, "Catch Error");         
                }
            }


        });


    }   


    public static void main(String[] args) {

        new Login();

    }
}

When the program is run, it bypasses the retrieval of the user and pass, and outputs "catch error", which I set up.

This is probably something stupid, like a missed character, but any help would be greatly appreciated.

Many thanks.

Harvey
  • 1
  • 1
  • 1

1 Answers1

1

Insert a space here after the single quote

String sql = "select user, pass from users where user = '"
         + username + "'and pass = '" + password + "'";
                       ^

Also, display the exact message from the exception by adding:

ex.printStackTrace();

in the exception block.

Aside: Use PreparedStatement rather than Statement to protect against SQL Injection attacks

Edit:

Stack trace displayed:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application at 

Solution was to installl & use 32-bit version of Java rather than the 64 bit JDK/JRE one.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Added the space, no change though. :/ – Harvey May 29 '13 at 16:24
  • Thanks for the tip about injection attacks. – Harvey May 29 '13 at 16:26
  • I added printstack, but to no effect. – Harvey May 29 '13 at 16:27
  • What appears when an exception occurs? – Reimeus May 29 '13 at 16:28
  • java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956) at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113) at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072) at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323) at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174) at java.sql.DriverManager.getConnection(DriverManager.java:579) – Harvey May 29 '13 at 16:29
  • I have a feeling that the program isn't connecting to the database properly. – Harvey May 29 '13 at 16:31
  • Certainly not. You need to match your client driver with the server. See this [answer](http://stackoverflow.com/questions/8895823/the-specified-dsn-contains-an-architecture-mismatch-between-the-driver-and-appli?answertab=votes#tab-top) – Reimeus May 29 '13 at 16:34
  • I seem to have hit a wall with that also. My system wont let me install 64bit drivers, as my office installation is 32bit. – Harvey May 29 '13 at 16:41
  • Ok, I figured it out. Because I was running 32-bit office, I couldn't install the 64-bit ODBC drivers. And as I was using 64 bit Java, and 64 bit netbeans, this was messing everything up. After I installed 32-bit java, and switched over to the 32 platform on netbeans, it worked like a charm. Thank you to everyone who helped. – Harvey May 29 '13 at 17:16