首页 技术 正文
技术 2022年11月15日
0 收藏 911 点赞 4,132 浏览 6883 个字
  • Controller包:表现层(视图)层。用来显示数据和接收用户数据
  • Service包:业务逻辑层,用来处理页面。先写接口,后写实现类
  • Dao包:持久层(数据访问层)。用来操作数据库

其中Dao包处于最底层,对于用户处于隐藏状态,对于开发者处于固定状态,其中包括Con_CloseSql类(数据库连接与关闭)、UserDao类(进行增删改查)与UserBeans(数据库数据)

Con_CloseSql类:

package dao;import java.sql.*;public class Con_CloseSql {    final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost:3306/表名?serverTimezone=GMT&characterEncoding=utf-8&useSSL=false"; final String USER = "root";
final String PASS = "密码";
Statement stmt = null; public Connection getConnect()
{
Connection conn = null; try{
Class.forName(JDBC_DRIVER);
String url = DB_URL;
conn = DriverManager.getConnection(url, USER, PASS);
return conn; }catch(ClassNotFoundException nfe) {
nfe.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
} public static void closeConnection(Connection connection) {
if(connection != null) {
try{
connection.close();
}catch(SQLException sqle) {
sqle.printStackTrace();
}
}
}}

UserDao类:

package dao;import bean.UserBean;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;public class UserDao {

  //保存用户数据
public void saveUser(UserBean userBean) throws SQLException {
Connection connect = new Con_CloseSql().getConnect();
Statement statement = connect.createStatement(); String sql = "insert into 表名 values (?,?,?,?,?,?,?,?,?)"; try {
PreparedStatement pr = connect.prepareStatement(sql); pr.setString(1, userBean.getHutype());
pr.setString(2, userBean.getHousetype());
pr.setString(3, userBean.getArea());
pr.setString(4, userBean.getNum());
pr.setString(5, userBean.getName());
pr.setString(6, userBean.getId());
pr.setString(7, userBean.getSex());
pr.setString(8, userBean.getNation());
pr.setString(9, userBean.getEducation()); pr.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
Con_CloseSql.closeConnection(connect);
} }

  //通过name删除该行
public void deleteUser(UserBean userBean) {
Connection connect = new Con_CloseSql().getConnect(); String sql = "delete from 表名 where name=? "; try {
PreparedStatement pr = connect.prepareStatement(sql);
pr.setString(1, userBean.getName()); pr.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} }


  //查询数据库中消息并从前端输出表格
public List<UserBean> query() {
Connection connect = new Con_CloseSql().getConnect();
String sql = "select * from 表名";
List<UserBean> userBeans = new ArrayList<>();
try {
try (PreparedStatement ps = connect.prepareStatement(sql)) { ResultSet resultSet = ps.executeQuery(sql); UserBean userBean = null;
while (resultSet.next()) {
String hutype = resultSet.getString("...");
String housetype = resultSet.getString("...");
String area = resultSet.getString("...");
//表头关键字
userBean = new UserBean(..., ..., ...,...);//表中元素
userBeans.add(userBean);
} } } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Con_CloseSql.closeConnection(connect);
}
return userBeans;
}

  //通过名字来展示信息
public UserBean queryByName(String name) {
Connection connect = new Con_CloseSql().getConnect();
String sql = "select * from census where 户主姓名 = '"+name+"' ";
UserBean userBean = null;
//Object[] params = {name}; try {
try (PreparedStatement ps = connect.prepareStatement(sql)) { ResultSet resultSet = ps.executeQuery(sql); if (resultSet.next()) {
String hutype = resultSet.getString("户别");
String housetype = resultSet.getString("住房类型");
String area = resultSet.getString("本户现住房面积");
String num = resultSet.getString("本户现住房间数");
String name1 = resultSet.getString("户主姓名");
String id = resultSet.getString("身份证号码");
String sex = resultSet.getString("性别");
String nation = resultSet.getString("民族");
String education = resultSet.getString("受教育程度");
userBean = new UserBean(hutype, housetype, area, num, name1, id, sex, nation, education); } } } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Con_CloseSql.closeConnection(connect);
}
return userBean;
}  //通过名字更新其他信息
public void upDate(UserBean user){
Connection connect = new Con_CloseSql().getConnect();
String sql="update census set 身份证号码=?,性别=?,民族=?,受教育程度=? where 户主姓名=?";
try {
PreparedStatement ps = connect.prepareStatement(sql);
ps.setString(1, user.getId());
ps.setString(2, user.getSex());
ps.setString(3,user.getNation());
ps.setString(4,user.getEducation());
ps.setString(5,user.getName()); int i= ps.executeUpdate(); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Con_CloseSql.closeConnection(connect);
} }


  //判断是否为该用户
public boolean isUser(String name) {
Connection connect = new Con_CloseSql().getConnect(); try { String sql = "select * from census where 户主姓名 = '" + name + "'"; // 执行查询语句,并把结果集返回给ResultSet
PreparedStatement pr = connect.prepareStatement(sql);
ResultSet rs = pr.executeQuery();
if (rs.next())
return true;
else
return false; } catch (SQLException e) {
// TODO Auto-generated catch block
return false;
}
} public ArrayList<String> findDate() {
Connection conn = new Con_CloseSql().getConnect();
String sql = "select * from census";
PreparedStatement pr; try {
pr = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pr.executeQuery();
int col = rs.getMetaData().getColumnCount();
ArrayList<String> m = new ArrayList<String>();
int n = 0;
while (rs.next()) {
for (int i = 1; i <= col; i++) {
m.add(rs.getString(i) + "<br>");
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() < 8)) {
m.add("<br>");
}
}
System.out.println("");
}
return m;
} catch (SQLException e) {
e.printStackTrace();
} return null;
}}

Service包主要包括各种servlet

XXXServlet类:

package service;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class XXXServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;utf-8"); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

pom.xml常用配置为:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

web.xml为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list></web-app>

jsp基础配置为:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body></body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839