• jspMVC


    package com.xx.control;

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class StudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;// 版本序列---可不要

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println();
    }
    }

    =================

    package com.xx.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import com.xx.model.User;
    import com.xx.util.JdbcUtil;

    public class UserDao {
    // 封装数据库对象
    private static Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    // 增
    public boolean insert(User entity) {
    // 声明返回值变量
    boolean flag = false;
    // 获取连接对象
    conn = JdbcUtil.getConn();
    // 定义sql语句
    String sql = "insert into users(id,name,pass,sex,age) values(?,?,?,?,?)";
    try {
    // 根据sql语句创建预处理对象
    pstmt = conn.prepareStatement(sql);
    // 为占位符赋值
    int index = 1;
    pstmt.setObject(index++, entity.getId());
    pstmt.setObject(index++, entity.getName());
    pstmt.setObject(index++, entity.getPass());
    pstmt.setObject(index++, entity.getSex());
    pstmt.setObject(index++, entity.getAge());
    // 执行更新
    int i = pstmt.executeUpdate();
    if (i > 0) {
    flag = true;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    // 释放资源
    JdbcUtil.release(rs, pstmt);
    return flag;
    }

    // 删
    public boolean deleteById(Integer id) {
    // 声明返回值变量
    boolean flag = false;
    // 获取连接对象
    conn = JdbcUtil.getConn();
    // 定义sql语句
    String sql = "delete from users where id=?";
    try {
    // 根据sql语句创建预处理对象
    pstmt = conn.prepareStatement(sql);
    // 为占位符赋值
    int index = 1;
    pstmt.setObject(index++, id);
    // 执行更新
    int i = pstmt.executeUpdate();
    if (i > 0) {
    flag = true;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    // 释放资源
    JdbcUtil.release(rs, pstmt);
    return flag;
    }

    // 改
    public boolean updateById(User entity) {
    // 声明返回值变量
    boolean flag = false;
    // 获取连接对象
    conn = JdbcUtil.getConn();
    // 定义sql语句
    String sql = "update users set name=?,pass=?,sex=?,age=? where id=?";
    try {
    // 根据sql语句创建预处理对象
    pstmt = conn.prepareStatement(sql);
    // 为占位符赋值
    int index = 1;
    pstmt.setObject(index++, entity.getName());
    pstmt.setObject(index++, entity.getPass());
    pstmt.setObject(index++, entity.getSex());
    pstmt.setObject(index++, entity.getAge());
    pstmt.setObject(index++, entity.getId());
    // 执行更新
    int i = pstmt.executeUpdate();
    if (i > 0) {
    flag = true;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }

    // 释放资源
    JdbcUtil.release(rs, pstmt);
    return flag;
    }

    // 查所有
    public List<User> findAll() {
    List<User> users = new ArrayList<User>();

    // 获取连接对象
    conn = JdbcUtil.getConn();
    // 定义sql语句
    String sql = "select * from users";
    try {
    // 根据sql语句创建预处理对象
    pstmt = conn.prepareStatement(sql);
    // 执行更新
    rs = pstmt.executeQuery();
    while (rs.next()) {
    // 声明返回值变量
    User entity = new User();
    entity.setId(rs.getInt("id"));
    entity.setName(rs.getString("name"));
    entity.setPass(rs.getString("pass"));
    entity.setSex(rs.getString("sex"));
    entity.setAge(rs.getInt("age"));
    users.add(entity);
    }

    } catch (SQLException e) {
    e.printStackTrace();
    }

    // 释放资源
    JdbcUtil.release(rs, pstmt);
    return users;
    }

    // 查单个
    public User findById(Integer id) {
    // 声明返回值变量
    User entity = new User();
    // 获取连接对象
    conn = JdbcUtil.getConn();
    // 定义sql语句
    String sql = "select * from users where id=?";
    try {
    // 根据sql语句创建预处理对象
    pstmt = conn.prepareStatement(sql);
    // 为占位符赋值
    int index = 1;
    pstmt.setObject(index++, id);
    // 执行更新
    rs = pstmt.executeQuery();
    if (rs.next()) {
    entity.setId(rs.getInt("id"));
    entity.setName(rs.getString("name"));
    entity.setPass(rs.getString("pass"));
    entity.setSex(rs.getString("sex"));
    entity.setAge(rs.getInt("age"));
    }

    } catch (SQLException e) {
    e.printStackTrace();
    }

    // 释放资源
    JdbcUtil.release(rs, pstmt);
    return entity;
    }
    }

    ====================

    package com.xx.model;

    public class User {
    private Integer id;
    private String name;
    private String pass;
    private String sex;
    private Integer age;

    ===============

    package com.xx.Test;

    import org.junit.Test;

    import com.xx.dao.UserDao;
    import com.xx.model.User;

    public class UserTest {
    UserDao uDao = new UserDao();

    @Test
    public void insert() {
    User entity = new User();
    entity.setName("测试1");
    entity.setPass("123");
    entity.setSex("女");
    entity.setAge(21);
    boolean flag = uDao.insert(entity);
    if (flag) {
    System.out.println("插入成功");
    } else {
    System.out.println("插入失败");
    }
    }

    @Test
    public void findById() {
    User entity = uDao.findById(1);
    System.out.println(entity.toString());
    }

    @Test
    public void update() {
    User entity = uDao.findById(4);
    entity.setName("测试2");
    entity.setAge(20);
    boolean flag = uDao.updateById(entity);
    if (flag) {
    System.out.println("更新成功");
    } else {
    System.out.println("更新失败");
    }
    }

    @Test
    public void deleteById() {
    boolean flag = uDao.deleteById(27);
    if (flag) {
    System.out.println("删除成功");
    } else {
    System.out.println("删除失败");
    }
    }
    }

    =============

    package com.xx.util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class JdbcUtil {
    public static Connection getConn() {
    // 传统jdbc取得数据库连接
    Connection con = null;
    String driver = "org.gjt.mm.mysql.Driver";
    String db_server = "jdbc:mysql://127.0.0.1/jspdemo"; // jspdemo表示database
    String user = "root";
    String pwd = "123";// 设置你的数据库密码
    try {
    // 加载数据库驱动
    Class.forName(driver);
    // 创建连接
    con = DriverManager.getConnection(db_server, user, pwd);
    } catch (Exception e) {
    e.printStackTrace();
    }
    // 返回一个数据库连接
    return con;
    }

    // 释放资源方法
    public static void release(ResultSet rs, PreparedStatement pstmt) {
    // 关闭顺序由里到外(使用的时候首先打开的事pstmt→rs,关闭的时候反过来)

    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (pstmt != null) {
    try {
    pstmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    }

    ============

    create database jspdemo;
    use jspdemo;
    CREATE TABLE user (
    id int unsigned not null auto_increment primary key,
    name varchar(20) DEFAULT NULL,
    password varchar(20) DEFAULT NULL,
    sex varchar(10) DEFAULT NULL,
    age int DEFAULT NULL
    );
    charset gbk;

  • 相关阅读:
    JedisConnectionException: java.net.ConnectException: Connection refused
    启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法
    JAVA 判断一个字符串是不是一个合法的日期格式
    升级openssl
    Linux操作路由
    Linux的用户行为审计
    升级gdb
    Linux的运行级别
    sudo的用法
    Linux缓存清理
  • 原文地址:https://www.cnblogs.com/superjt/p/3069529.html
Copyright © 2020-2023  润新知