jdbc连接mysql,实现简单的登陆验证
package com.examples.jdbc.o6_实现登录界面;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Map<String, String> userInfo = userLogin();
boolean loginResult = loginCheck(userInfo);
System.out.println(loginResult ? "登录成功" : "登录失败");
}
private static boolean loginCheck(Map<String, String> userInfo) {
boolean loginResult = false;
String name = userInfo.get("userName");
String passwd = userInfo.get("passWord");
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
String driver = resourceBundle.getString("driver");
String url = resourceBundle.getString("url");
String userName = resourceBundle.getString("userName");
String passWord = resourceBundle.getString("passWord");
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, userName, passWord);
statement = connection.createStatement();
String sql = "select * from tb_user where uname = '"+name+"' and upasswd = '"+passwd+"'";
resultSet = statement.executeQuery(sql);
if(resultSet.next()){
loginResult = true;
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
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();
}
}
}
return loginResult;
}
private static Map<String, String> userLogin() {
Scanner scanner = new Scanner(System.in);
System.out.println("用户名: ");
String userName = scanner.nextLine();
System.out.println("密码: ");
String passWord = scanner.nextLine();
Map<String, String> userInfo = new HashMap<>();
userInfo.put("userName", userName);
userInfo.put("passWord", passWord);
return userInfo;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY