数据库的设计:数据库主要设计包括一个检查登陆的表,必须有点检查是账户和密码,其他的属性可 以随便添加一些,作为注册的时候用。
代码设计:分成4个层。1.实体类层 2.数据库操作层 2.登陆层 3.登陆验证层。
实体类层:
package com.itheima.entity;
import java.util.Date;
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", email=" + email + ", birthday=" + birthday + "]";
}
}
2.数据库操作层
package com.itheima.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtils {
private static String driverClass;
private static String url;
private static String user;
private static String password;
static{
ResourceBundle rb = ResourceBundle.getBundle("dbinfo");
//给上面4个变量赋值
driverClass = rb.getString("driverClass");
url = rb.getString("url");
user = rb.getString("user");
password = rb.getString("password");
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//得到连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
//关闭资源
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
3.登陆验证层
package com.itheima.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.itheima.entity.User;
import com.itheima.util.DBUtils;
public class DoLogin {
/**
* 根据用户名和密码查询用户对象信息
* @param name
* @param pwd
* @return u
*/
public User findUser(String name,String pwd){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
User u = null;
try {
conn = DBUtils.getConnection();//得到连接对象Connection
String sql ="SELECT * FROM admin WHERE name=? AND password=?";
stmt = conn.prepareStatement(sql);//得到执行sql语句的对象Statement
//给?赋值
stmt.setString(1, name);
stmt.setString(2, pwd);
rs = stmt.executeQuery();//执行sql语句
if(rs.next()){
u = new User();
u.setId(rs.getInt(1));
u.setName(rs.getString(2));
u.setPassword(rs.getString(3));
u.setEmail(rs.getString(4));
u.setBirthday(rs.getDate(5));
u.toString();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtils.closeAll(rs, stmt, conn);
}
return u;
}
}
4.登陆层
package com.itheima.client;
import java.util.Scanner;
import com.itheima.entity.User;
import com.itheima.service.DoLogin;
public class Login {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = input.nextLine();
System.out.println("请输入密码:");
String pwd = input.nextLine();
DoLogin dl = new DoLogin();
User user = dl.findUser(name, pwd);//调用查询用户的方法
if(user!=null){
System.out.println("欢迎你:"+user.getName());
}else{
System.out.println("用户名或密码错误!");
}
}
}
此外为了修改和连接数据库方便还应该设置一个dbinfo.propertise文件
内容如下:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名
user=root
password=密码
希望我的博客能对你有所帮助。如果感觉我的博客有问题的话,可以留言交流一下。