idea 业务层无法跳转到实现层_Java系列:五层架构实现登录注册(IntelliJ idea版)...

代码实例

实体类层

package com.test.entity;

public class Employee {

private int userid;

private String username;

private String password;

public Employee() {

}

public Employee(int userid,String username, String password) {

this.userid = userid;

this.username = username;

this.password = password;

}

public int getUserid() {

return userid;

}

public void setUserid(int userid) {

this.userid = userid;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

数据持久层

package com.test.dao;

import com.test.entity.Employee;

import com.test.util.ConnectionFactory;

public class EmployeeDao {

public int insert(Employee employee){

int flag = 3;

int userid = employee.getUserid();

String username = employee.getUsername();

String password = employee.getPassword();

try {

String sql_exist = String.format(

"select 1 from kj_user where userid = %s and username='%s' and password='%s'",

userid,username,password

);

String sql_regist = String.format(

"select 1 from kj_user where userid = %s",

userid

);

String res = ConnectionFactory.queryNoPage(sql_exist);

String regist = ConnectionFactory.queryNoPage(sql_regist);

if(res.indexOf("1") == -1){

System.out.println("登录失败");

if (regist.indexOf("1") == -1){

System.out.println("用户未注册,请注册!");

String sql = String.format(

"insert into kj_user(userid,username,password) select %s,'%s','%s' from dual",

userid,username,password

);

ConnectionFactory.executeSQL(sql);

flag = 1;

}else{

System.out.println("请换不同ID注册!");

flag = 2;

}

}else {

System.out.println("登录成功");

flag = 0;

}

} catch (Exception e){

e.printStackTrace();

}

return flag;

}

}

连接数据库

package com.test.util;

import java.sql.*;

public class ConnectionFactory {

private static ConnectionFactory instance = null;

public static ConnectionFactory getInstance() {

if (instance == null) instance = new ConnectionFactory();

return instance;

}

private static String USERNAMR = "mine";

private static String PASSWORD = "mine";

private static String DRVIER = "oracle.jdbc.OracleDriver";

private static String URL = "jdbc:oracle:thin:@192.168.100.103:1521:orcl";

private static Connection conn = null;

public static Connection getConnection(){

try {

Class.forName(DRVIER);

conn = DriverManager.getConnection(URL, USERNAMR, PASSWORD);

System.out.println("成功连接数据库");

} catch (ClassNotFoundException e) {

throw new RuntimeException("class not find !", e);

} catch (SQLException e) {

throw new RuntimeException("get connection error!", e);

}

return conn;

}

public static void executeSQL(String sql) {

Statement stat = null;

try {

conn = getConnection();

stat = conn.createStatement();

} catch (Exception e) {

e.printStackTrace();

return;

}

try {

stat.execute(sql);

} catch (Exception e) {

e.printStackTrace();

return;

}

try {

if (stat != null) stat.close();

if (conn != null) conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static String queryNoPage(String sql) {

Connection conn = null;

Statement stat = null;

StringBuffer buff = new StringBuffer();

try {

conn = getConnection();

stat = conn.createStatement();

}

catch (Exception e) {

e.printStackTrace();

return "[]";

}

try {

ResultSet rows = stat.executeQuery(sql);

//得到数据集的列数

ResultSetMetaData rsmd = rows.getMetaData();

int colCount = rsmd.getColumnCount();

boolean first = true;

buff.append("[");

while (rows.next()) {

String rowStr = "";

for (int i = 1; i <= colCount; i ++ ) {

if (i>1) rowStr += ",";

String tempValue = rows.getString(i);

if (tempValue!=null){

tempValue = tempValue.replace("\'","\\\'");

tempValue = tempValue.replace("\n","\\n");

tempValue = tempValue.replace("\t","\\t");

}

rowStr += String.format("%s", tempValue);

}

rowStr = String.format("\"%s\"", rowStr);

if (first) first = false;

else buff.append(",");

buff.append(rowStr);

}

buff.append("]");

rows.close();

}

catch (Exception e) {

e.printStackTrace();

return "[]";

}

try {

if (stat != null) stat.close();

if (conn != null) conn.close();

}

catch (Exception e) {

e.printStackTrace();

return "[]";

}

return buff.toString();

}

}

业务逻辑层

package com.test.service;

import com.test.dao.EmployeeDao;

import com.test.entity.Employee;

public class EmployeeService {

public int regist(Employee employee){

EmployeeDao dao= new EmployeeDao();

int result = dao.insert(employee);

return result;

}

}

控制层

package com.test.servlet;

import com.test.entity.Employee;

import com.test.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

public class EmployeeServlet extends javax.servlet.http.HttpServlet{

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {

response.setContentType("text/html");

request.setCharacterEncoding("utf-8");

Employee u = new Employee();

int userid = Integer.parseInt(request.getParameter("userid"));

String username = request.getParameter("username");

String password = request.getParameter("password");

u.setUsername(username);

u.setPassword(password);

System.out.println(username);

Employee employee=new Employee(userid,username,password);

EmployeeService service=new EmployeeService();

int result = service.regist(employee);

if (result==0){

response.sendRedirect(request.getContextPath()+"/login_success.jsp");

}else if (result==1){

response.sendRedirect(request.getContextPath()+"/regist_success.jsp");

}else {

response.sendRedirect(request.getContextPath()+"/login_failure.jsp");

}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {

doPost(request,response);

}

}

用户界面层

用户登录

ID:

用户名:

密码:

web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

This is the description of my J2EE component

This is the display name of my J2EE component

EmployeeServlet

com.test.servlet.EmployeeServlet

EmployeeServlet

/EmployeeServlet

login.jsp

效果图

打开App,阅读手记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值