文章目录
class UserLogin
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class UserLogin {
public static void main(String[] args) {
//初始化一个界面
Map<String,String> userLoginInfo=initUI();
//验证用户名和密码
boolean loginSuccess=login(userLoginInfo);
//最后输出结果
System.out.println(loginSuccess?"登入成功":"登入失败");
}
/**
* 用户登入
* @param userLoginInfo 用户登入信息
* @return false表示登入失败 ture表示登入成功
*/
private static boolean login(Map<String, String> userLoginInfo) {
ResultSet resultSet=null;
Statement statement=null;
Connection connection=null;
boolean loginSuccess=false;
String userName=userLoginInfo.get("userName");
String userPassword=userLoginInfo.get("userPassword");
//第一步:注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//第二步:获取数据库连接
// ?useUnicode=true&characterEncoding=utf8
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mobilemallsystem", "root", "123456");
//第三步:获取数据库操作对象
statement = connection.createStatement();
//第四步:执行SQL语句
String sql="select * from usermessage where uname='"+userName+"'and upassword='"+userPassword+"'";
resultSet= statement.executeQuery(sql);
//第五步:处理查询结果集
if(resultSet.next()){
loginSuccess=true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//第六步:释放资源
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement !=null){
try {
statement .close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return loginSuccess;
}
/**
* 初始化用户界面
* @return 返回用户输入的用户名和密码等信息
*/
private static Map<String, String> initUI() {
Map<String,String> userInfo = new HashMap<>();
Scanner scanner= new Scanner(System.in);
System.out.print("请输入用户名:");
String userName=scanner.next();
System.out.print("请输入用密码:");
String userPassword=scanner.next();
userInfo.put("userName",userName);
userInfo.put("userPassword",userPassword);
return userInfo;
}
}
异常体提示
无
显示结果
错误原因
字符集转码错误
纠正方法
?useUnicode=true&characterEncoding=utf8
class UserLogin
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class UserLogin {
public static void main(String[] args) {
//初始化一个界面
Map<String,String> userLoginInfo=initUI();
//验证用户名和密码
boolean loginSuccess=login(userLoginInfo);
//最后输出结果
System.out.println(loginSuccess?"登入成功":"登入失败");
}
/**
* 用户登入
* @param userLoginInfo 用户登入信息
* @return false表示登入失败 ture表示登入成功
*/
private static boolean login(Map<String, String> userLoginInfo) {
ResultSet resultSet=null;
Statement statement=null;
Connection connection=null;
boolean loginSuccess=false;
String userName=userLoginInfo.get("userName");
String userPassword=userLoginInfo.get("userPassword");
//第一步:注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//第二步:获取数据库连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mobilemallsystem?useUnicode=true&characterEncoding=utf8", "root", "123456");
//第三步:获取数据库操作对象
statement = connection.createStatement();
//第四步:执行SQL语句
String sql="select * from usermessage where uname='"+userName+"'and upassword='"+userPassword+"'";
resultSet= statement.executeQuery(sql);
//第五步:处理查询结果集
if(resultSet.next()){
loginSuccess=true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//第六步:释放资源
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement !=null){
try {
statement .close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return loginSuccess;
}
/**
* 初始化用户界面
* @return 返回用户输入的用户名和密码等信息
*/
private static Map<String, String> initUI() {
Map<String,String> userInfo = new HashMap<>();
Scanner scanner= new Scanner(System.in);
System.out.print("请输入用户名:");
String userName=scanner.next();
System.out.print("请输入用密码:");
String userPassword=scanner.next();
userInfo.put("userName",userName);
userInfo.put("userPassword",userPassword);
return userInfo;
}
}