• 欢迎光临~

JDBC登录案例改进

开发技术 开发技术 2022-01-23 111次浏览
package cn.itcast.jdbc;

import cn.itcast.jdbc.util.JDBCUtils;

import java.sql.*;
import java.util.Scanner;
import java.util.function.Predicate;

public class JDBCDemo6 {
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();
boolean flag = login(name, password);
if (flag){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}

}
//通过键盘录入用户名和密码
//判断用户名是否登录成功
public static boolean login(String name,String password) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
if (name == null || password == null) {
return false;
}
try {
conn = JDBCUtils.getConnection();
String sql = "select *from user where name = ?and password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,password);
rs = pstmt.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
}
程序员灯塔
转载请注明原文链接:JDBC登录案例改进
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com