一.登录逻辑思路
登录有验证码,用户名,密码(postman测试)
servlet LoginServlet,java
1) 乱码处理
2)得到前端的值
3) 封装到VO,VO就是用于封装前端传递到后台的值
4)调用服务逻辑层
5)得到服务逻辑层传回的数据(DTO)后端传递到前端的数据
6)将得到的数据转换为json对象传递到前端
service 服务逻辑层
UserService.java
UserServiceImpl.java 多态
1)判断验证码是否正确
2)对密码进行加密
3)将VO里面的数据复制到model里面
4)逻辑判断,传递到前端应该传什么数据
dao 数据持久层
UserDao.java
UserDaoImpl.java
1)封装sql语句
2)调用JDBC的查询方法
3)解析结果集ResulSet
JDBC 连接数据
1)加载驱动
2)得到数据库连接
3)编译sql语句
4)执行sql语句
5)返回得到的结果集
6)关闭连接
二.后台数据的增删改查
用户新增思路:
前端逻辑:
打开页面
输入用户信息
判断用户信息是否输入格式正确
提交信息到Java后台
后端逻辑:
servlet 六大步骤
service
1)将VO里面的数据复制到model
2)将密码加密
3)根据返回的结果判断是否返回什么ResultDt
dao
1)sql语句
2)调用JDBC
3)返回结果
4.JDBC
连接数据库的until帮助类
package demo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyDBUtil {
private static String className = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://192.168.0.131:9171/sisenmess-user-database";
public static final String user = "root";
public static final String password = "123";
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(className);
Connection con = DriverManager.getConnection(url,user,password);
return con;
}
public static Statement getStatement(Connection con) throws SQLException{
Statement st = con.createStatement();
return st;
}
public static PreparedStatement getPreparedStatement(Connection con,String sql) throws SQLException{
PreparedStatement ps = con.prepareStatement(sql);
return ps;
}
public static ResultSet getResultSet(Statement st,String sql) throws SQLException{
ResultSet rs = st.executeQuery(sql);
/*int rs1 = st.executeUpdate(sql);*/
return rs;
}
public static void closeAllMethod(Connection con,Statement st,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
项目中的增删改查示例
package demo1;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;
public class BuguManger {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
BuguManger bugu= new BuguManger();
//bugu.addBugu();
//bugu.search();
//bugu.update();
bugu.deleteBugu();
}
public void search() throws ClassNotFoundException, SQLException, IOException{
//查询数据
String sql = "";
Connection con1 = MyDBUtil.getConnection();
PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
sql = "select * from userlist";
ResultSet rs1 = ps.executeQuery(sql);
while(rs1.next()){
String uid=rs1.getString(1);
String uid2=rs1.getString(2);
System.out.println(uid);
}
}
public void addBugu() throws ClassNotFoundException, SQLException, IOException{
//增加一条数据
String sql = "";
Connection con1 = MyDBUtil.getConnection();
PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(input);
// System.out.print("请输入用户名:");
// uname = br1.readLine();
// System.out.print("请输入密码:");
// pword = br1.readLine();
// ps.setString(1, uname);
// ps.setString(2, pword);
System.out.print("请输入要添加的姓名:");
String uname = br.readLine();
//
// System.out.print("请输入要添加的密码:");
String pword = br.readLine();
//
// System.out.print("请输入要添加的邮箱:");
String email = br.readLine();
sql = "insert into userlist (UserId,UserNo,UserTName) values('"+uname+"','"+pword+"','"+email+"')";
int rs1 = ps.executeUpdate(sql);
}
//修改数据
public void update() throws ClassNotFoundException, SQLException, NumberFormatException, IOException{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(input);
String sql = "";
Connection con1 = MyDBUtil.getConnection();
PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
System.out.println("请输入你所要修改的编号:");
int id = Integer.parseInt(br.readLine());
System.out.println("请输入修改后的名字:");
String UserNo = br.readLine();
System.out.println("请输入修改后的密码:");
String UserTName = br.readLine();
sql = "update userlist set UserNo='"+UserNo+"',UserTName='"+UserTName+"'where UserId = '"+id+"'";
ps.executeUpdate(sql);
System.out.println("修改成功!");
}
//删除数据
public void deleteBugu() throws ClassNotFoundException, SQLException, NumberFormatException, IOException{
String sql = "";
Connection con1 = MyDBUtil.getConnection();
PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(input);
System.out.println("请输入你所要删除的编号:");
int id = Integer.parseInt(br.readLine());
sql = "delete from userlist where userId='"+id+"'";
ps.executeUpdate(sql);
}
}