java实现的简单的登录系统处理 (mysql数据库)

49 篇文章 0 订阅
14 篇文章 1 订阅
/**
 * 
 */
package taskDome;

import java.sql.DriverManager;
import java.sql.ResultSet;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * @author Lee
 *
 */
public class User {

    /**
     * 
     */
    private static String Name="";
    private static String Password="";
    private String Telephone="";
    private String MailNumber="";
    private String QQNumber="";
    private String AlipayNumber="";
    private String ApplyConfirm="false";


    public User() {
        // TODO Auto-generated constructor stub
    }
    public User(String name, String password, String telephone, String mailNumber, String qQNumber,
            String alipayNumber) {
        Name = name;
        Password = password;
        Telephone = telephone;
        MailNumber = mailNumber;
        QQNumber = qQNumber;
        AlipayNumber = alipayNumber;
    }
    public User(String name, String password, String telephone, String mailNumber,
            String alipayNumber) {
        Name = name;
        Password = password;
        Telephone = telephone;
        MailNumber = mailNumber;
        AlipayNumber = alipayNumber;
    }


    public User(String name, String password, String telephone, String mailNumber) {
        Name = name;
        Password = password;
        Telephone = telephone;
        MailNumber = mailNumber;
    }

    public User(String name, String password, String mailNumber) {
        Name = name;
        Password = password;
        MailNumber = mailNumber;
    }
    public User(String name, String password) {
        Name = name;
        Password = password;
    }
    /**
     * @return 
     * @return the name
     */
    public  String getName() {
        return Name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        Name = name;
    }
    /**
     * @return the password
     */
    public  String getPassword() {
        return Password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        Password = password;
    }
    /**
     * @return the telephone
     */
    public String getTelephone() {
        return Telephone;
    }
    /**
     * @param telephone the telephone to set
     */
    public void setTelephone(String telephone) {
        Telephone = telephone;
    }
    /**
     * @return the mailNumber
     */
    public String getMailNumber() {
        return MailNumber;
    }
    /**
     * @param mailNumber the mailNumber to set
     */
    public void setMailNumber(String mailNumber) {
        MailNumber = mailNumber;
    }
    /**
     * @return the qQNumber
     */
    public String getQQNumber() {
        return QQNumber;
    }
    /**
     * @param qQNumber the qQNumber to set
     */
    public void setQQNumber(String qQNumber) {
        QQNumber = qQNumber;
    }
    /**
     * @return the alipayNumber
     */
    public String getAlipayNumber() {
        return AlipayNumber;
    }
    /**
     * @param alipayNumber the alipayNumber to set
     */
    public void setAlipayNumber(String alipayNumber) {
        AlipayNumber = alipayNumber;
    }
    /**
     * @param args
     */
    /**
     * @return the applyConfirm
     */
    public String getApplyConfirm() {
        return ApplyConfirm;
    }
    /**
     * @param applyConfirm the applyConfirm to set
     */
    public void setApplyConfirm(String applyConfirm) {
        ApplyConfirm = applyConfirm;
    }


    public boolean check(){
        if(Name.equalsIgnoreCase("张三")||Name.equalsIgnoreCase("lily")){
            if(Password.equalsIgnoreCase("123")){
                return true;
            }
        }
        return false;
    }   
    public boolean PasswordIsCorrect(){ //密码修改判断
        if(Name.equalsIgnoreCase("张三")||Name.equalsIgnoreCase("lily")){
            if(Password.equalsIgnoreCase("123")){
                return true;
            }
        }
        return false;
    }   

    public boolean Registercheck(){
        if(Name.equalsIgnoreCase("张三")||Name.equalsIgnoreCase("lily")){
                return false;
        }
        else return true;

    }
    public boolean EmailConfirm(){
        if(MailNumber.equalsIgnoreCase("8342@qq.com")||MailNumber.equalsIgnoreCase("lily@qq.com")){ //邮箱没人用
        if(Password.equals("123")) return true;  //对应用户的密码正确
    }
       return false;
    }
    public boolean DealInfoCheck(){
        if(AlipayNumber.equalsIgnoreCase("8342@qq.com")||AlipayNumber.equalsIgnoreCase("lily@qq.com")){ //支付宝没人用
                if(Telephone.equals("12306")){   //电话可以用
                    if(Password.equals("123")){   //对应密码正确
                    return true;  //对应用户的密码正确
                    }
                }
        }
        return false;   
    }
    public boolean ConnectMysql()
    {
        String url = "jdbc:mysql://127.0.0.1:3306/";
        String user= "root";
        String dbpassword = "xxxxx";
        boolean result = false;
        try {
            //链接字符串
             String driver ="com.mysql.jdbc.Driver";
            //加载驱动
             Class.forName(driver);
             //建立数据库连接
             Connection   con = (Connection) DriverManager.getConnection(url,user, dbpassword);

            if(con==null){
                System.out.println("can't open JDBConnection");
            }
            //sql语句
            String sql = "select * from user.user where username=? and password=?";

            PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql); 
            pstmt.setString(1,this.Name); //第一个?字符的替换
            pstmt.setString(2,this.Password); //第二个?字符的替换
            System.out.println(sql);
            System.out.println(getName());
            System.out.println(getPassword());
            ResultSet rs=pstmt.executeQuery();//执行语句后,产生单个结果集的语句
            if(rs.next())
            {
                System.out.println(rs.getString("username"));
                System.out.println("PASS");
                result=true;

            }
            else
            {
                System.out.println("FALL");
                result=false;
            }
            pstmt.close();//3个关闭PreparedStatement,ResultSet,Connection
            rs.close();
            con.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;

    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        User a=new User("lee","8342");
        a.ConnectMysql();
    }

}

总结:
PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql); 与
ResultSet rs=pstmt.executeQuery();//执行语句后,产生单个结果集的语句 成对出现哦~。

System.out.println(rs.getString(“username”));
username为sql语句的参数

本地使用JDBC 比如路径写为 String url = “jdbc:mysql://127.0.0.1:3306/”; 而不是localhost

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值