在java和ms访问中验证用户名和密码

时间:2016-04-10 13:28:54

标签: java database ms-access database-connection jdbc-odbc

import java.sql.*;
import java.util.*;

class Check {
    public static void main(String[] args)               
    {
        try   
        {
            String username;
            String password;
            System.out.println("Enter username and password");
            Scanner s=new Scanner(System.in);
            username=s.next();
            password=s.next();
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
            Connection con=DriverManager.getConnection("jdbc:odbc:logindetails");
            PreparedStatement stm=con.prepareStatement("select*from login where username=?and password=?"); 
            stm.setString(1,username); 
            stm.setString(2,password);
            int result=stm.executeUpdate();
            if (result==1)
            {
                System.out.println("login success"); 
            }      
            else
            {
                System.out.println("login failed");
            }
            con.close();
        } 
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        } 
    }
}

1 个答案:

答案 0 :(得分:0)

你可能会错过照顾密码是一个保留字。因此:

PreparedStatement stm = con.prepareStatement("select * from login where username=? and [password]=?");