JDBC模拟用户登录

代码:

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

/*
* 使用prepareStatement
* */
public class Demo2Test {
    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 password=sc.nextLine();
        //登陆的方法
        login(name ,password);
    }
    private  static void login(String name,String password) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        //编写sql语句,未知内容使用?占位符;   用户名       密码
        String s = "select * from user3 where sname=? and sid=?";
        //获得PreparedStatement对象
        PreparedStatement ps = conn.prepareStatement(s);
        //设置实际的参数,setxxx(占位符的位置,真实的值)
        ps.setString(1,name);
        ps.setString(2,password);
        //执行sql语句
        ResultSet re =ps.executeQuery();
        if (re.next()){
            System.out.println("登陆成功");
        }else {
            System.out.println("登录失败");
        }
        //关闭资源
        JdbcUtils.close(re,ps,conn);
    }
}

工具类:

import java.sql.*;

/*
* 访问数据库工具类
* */
public class JdbcUtils {
    //把几个字符串定义成常量
    private static  final String USER="root";
    private static  final String PWD="root";
    private static  final String URl="jdbc:mysql://localhost:3306/day97";
    private static final String DRIVER="com.mysql.jdbc.Driver";
    static {
        try {
            //注册驱动
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //得到数据库连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URl,USER,PWD);
    }
    //关闭连接、执行的打开资源
    public static  void close(Connection connection, Statement statement){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //关闭所有打开资源
    public  static  void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (statement!=null){
            try {
               statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //关闭资源
    public static  void close(PreparedStatement preparedStatement, Connection connection) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/duguangming/p/10651538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值