Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java error i cant fix

  • 25-11-2005 3:11pm
    #1
    Closed Accounts Posts: 32


    Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few
    parameters. Expected 1.


    programs reads two variables searches table for first inputted variable, returns another variable and checks this against the second inputted variable. straight foward login program

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

    public class Login extends JFrame implements ActionListener
    {

    private JTextField nameField;
    private JTextField passField;
    private JLabel nameLabel;
    private JLabel passLabel;
    private JButton loginButton;

    String name, pass;

    public Login()
    {

    super ("Login");

    nameField = new JTextField();
    passField = new JTextField();

    nameLabel = new JLabel("UserName: ");
    passLabel = new JLabel("Password: ");

    loginButton = new JButton("Login");

    Container contentPane = getContentPane();
    setBounds(10,10, 230,400);

    contentPane.setLayout(null);

    nameField.setBounds(85,180,120,20);
    contentPane.add(nameField);

    passField.setBounds(85,210,120,20);
    contentPane.add(passField);

    nameLabel.setBounds(5,180,150,15);
    contentPane.add(nameLabel);

    passLabel.setBounds(5,210,150,15);
    contentPane.add(passLabel);

    loginButton.setBounds(85,330,120,20);
    contentPane.add(loginButton);
    loginButton.addActionListener(this);

    show();

    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == loginButton)
    {
    name = nameField.getText();
    pass = passField.getText();

    try
    {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    // set this to a MS Access DB you have on your machine
    String filename = "e:/iain/College/Y4/SWP/Program/website/database/loginDetails.mdb";
    String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
    database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end

    // now we can get the connection from the DriverManager
    Connection con = DriverManager.getConnection( database ,"","");

    Statement s = con.createStatement();

    s.execute(" SELECT [password] FROM logindetails WHERE [username] = name"); // select the data from the table
    ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query

    String passtmp = rs.getString(1);

    if(passtmp.equals(pass))
    {
    System.out.print("Verified");
    }

    s.close(); // close the Statement to let the database know we're done with it
    con.close(); // close the Connection to let the database know we're done with it

    }
    catch (Exception ee)
    {
    System.out.println("Error: " + ee);
    }
    }

    }



    public static void main (String args[])
    {
    Login login = new Login();
    login.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );;
    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 507 ✭✭✭bigbadcon


    Hey might not be relevant but are you sure your query is formatted the way it should be.Ive only used jdbc before and when writing an sql query with a variable in it you have to open and close the qoutes and then put the whole query in more quotes surrounding that.


    s.execute("" SELECT [password] FROM logindetails WHERE [username] = "+name+"");

    something like that .....

    could be wrong tho.

    just a guess


  • Closed Accounts Posts: 32 VanStrummer


    cheers, sent me in the right direction

    turns out this is correct:

    '%name%'


    i should have realised the SQL statement wouldn't recognise the variable name just on its own.

    thanks again


  • Registered Users, Registered Users 2 Posts: 507 ✭✭✭bigbadcon


    cool,

    im a recent graduate myself so i feel dead smart now :D

    take it easy


  • Closed Accounts Posts: 324 ✭✭madramor


    heres some working code
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Login extends JFrame implements ActionListener {
        
        private JTextField nameField;
        private JTextField passField;
        private JButton loginButton;
        
        public Login() {
            super("Login");
            Container contentPane = getContentPane();
            contentPane.add(getPanel());
            // allows you to use enter instead of the mouse
            getRootPane().setDefaultButton(loginButton);
            setBounds(10,10,230,400);
            setResizable(false);
            setLocation(300,50);
            // show() is depriciated
            setVisible(true);
        }
        
        private JPanel getPanel(){
            JPanel p = new JPanel();
            p.setLayout(null);
            // name label
            JLabel nameLabel = new JLabel("UserName: ");
            nameLabel.setBounds(5,180,150,15);
            p.add(nameLabel);
            // namefield
            nameField = new JTextField();
            nameField.setBounds(85,180,120,20);
            p.add(nameField);
            // pass label
            JLabel passLabel = new JLabel("Password: ");
            passLabel.setBounds(5,210,150,15);
            p.add(passLabel);
            // pass field
            passField = new JTextField();
            passField.setBounds(85,210,120,20);
            p.add(passField);
            // button
            loginButton = new JButton("Login");
            loginButton.setBounds(85,330,120,20);
            p.add(loginButton);
            loginButton.addActionListener(this);
            return p;
        }
        
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == loginButton) {
                String name = nameField.getText();
                if("".equals(name)){
                    JOptionPane.showMessageDialog(this,"Please enter a name.","Input Problem",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                String pass = passField.getText();
                if("".equals(pass)){
                    JOptionPane.showMessageDialog(this,"Please enter a password.","Input Problem",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                try {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    // use ODBC Datasource Administrator (odbcad32.exe) to set up a
                    // datasource called test that maps to your database file
                    String database = "jdbc:odbc:test";
                    // get the connection
                    Connection con = DriverManager.getConnection(database,"","");
                    // the sql text
                    String txt = "select password from logindetails where username = ?";
                    // use prepared statements
                    PreparedStatement pps = con.prepareStatement(txt);
                    // set the ? from the sql text
                    pps.setString(1,name);
                    // execute the query
                    ResultSet rs = pps.executeQuery();
                    // check to see if any results and
                    // move to the first set of results
                    if(rs.next()){
                        String passtmp = rs.getString("PASSWORD");
                        //  use pass because you already checked its not blank
                        if(pass.equals(passtmp)) {
                            JOptionPane.showMessageDialog(this,"Username and Password are correct","Success",JOptionPane.INFORMATION_MESSAGE);
                        }
                        else
                            JOptionPane.showMessageDialog(this,"Password incorrect","Error",JOptionPane.INFORMATION_MESSAGE);
                    }
                    else
                        JOptionPane.showMessageDialog(this,"Username is not in the database","Error",JOptionPane.INFORMATION_MESSAGE);
                    rs.close();
                    pps.close();
                    con.close();
                } 
                catch (Exception ee) {
                    JOptionPane.showMessageDialog(this,ee.toString(),"Error",JOptionPane.WARNING_MESSAGE);
                }
            }
        }
        
        public static void main(String args[]) {
            Login login = new Login();
            login.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );;
        }
        
    }
    


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Read the charter. You should explain how to fix the relavent part rather then just giving working code.


  • Advertisement
Advertisement