闫平平
写代码也要酷酷的!

导航

 
package com.aaa.demo1;

import com.aaa.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class DemoLogin {
    public static void main(String[] args) throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = sc.nextLine();
        System.out.println("请输入密码:");
        String pwd = sc.nextLine();
        login(name,pwd);
    }
    //登陆方法
    public static void login(String name,String password) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        //1编辑SQL语句
        String sql="select * from users where uname=? and uage=?";
        PreparedStatement pre = conn.prepareStatement(sql);
        pre.setString(1,name);
        pre.setString(2,password);
        ResultSet rs = pre.executeQuery();
        if(rs.next()){
            System.out.println("登陆成功!");
        }else{
            System.out.println("登陆失败");
        }
        //关闭资源
        JdbcUtils.close(rs,pre,conn);
    }

}

  

posted on 2019-04-02 23:08  写代码也要酷酷的  阅读(308)  评论(0编辑  收藏  举报