zoukankan      html  css  js  c++  java
  • jdbc数据库中的增删改

    HttpSession session=request.getSession(); 
    session.setAttribute("currentUser",u.username);//获取用户名

    servlet中不能直接获取session

    package com.hanqi.dal;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import com.hanqi.model.AppUser;
    import com.hanqi.model.Dept;
    import com.hanqi.model.Emp;
    import com.hanqi.util.JdbcConnectionUtil;
    
    public class MethodDal {
    
        private Connection con;
        private PreparedStatement ps;
        private ResultSet rs;
    
        // 添加一条数据
        public int insertData() {
            init();
            int i = -1;
            String sql = "insert into course" + "values('11','cc','33')";
            try {
                ps = con.prepareStatement(sql);
                i = ps.executeUpdate();
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
            close();
            return i;
        }
    
        // 添加制定的参数
        public int insertData(AppUser user) {
            init();
            int i = -1;
            String sql = "insert into course" + "values(?,?,?)";
            //long l = new Date().getTime();
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, user.getUsername());
                ps.setString(2, user.getPassword());
                ps.setString(3, user.getRealname());
                //ps.setDate(4, new java.sql.Date(l));
                i = ps.executeUpdate();
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
            close();
            return i;
        }
    
        // 批量添加数据
        public int[] insertBatchData() {
            init();
            int[] arr = null;
            try {
                String sql = "insert into appuser values(?,?,?)";
                ps = con.prepareStatement(sql);
                for (int i = 0; i < 5; i++) {
                    ps.setString(1, "11" + i);
                    ps.setString(2, "cc" + i);
                    ps.setString(3, "22" + i);
                    ps.addBatch();
                }
                arr = ps.executeBatch();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            close();
            return arr;
        }
    
        // 删除一条记录
        public int deleteData(int cno) {
            init();
            int i = -1;
            String sql = "delete course c where c.cno=?";
            try {
                ps = con.prepareStatement(sql);
                ps.setInt(1, cno);
                i = ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            close();
            return i;
        }
    
        // 更新表中的数据
        public int updateData(int id, String realname) {
            init();
            int i = -1;
            String sql = "update course c set c.cname=? where c.cno=?";
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, cname);
                ps.setInt(2, cno);
                i = ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            close();
            return i;
        }
    
        public List<AppUser> selectAppUser() {
            String sql = "select a.id,a.username hh,a.password,a.realname,a.createtime from appuser a where a.id in (76,73)";
            init();
            List<AppUser> list = new ArrayList<AppUser>();
            try {
                ps = con.prepareStatement(sql);
                rs = ps.executeQuery();
                while (rs.next()) {
                    AppUser au = new AppUser();
                    
                    au.setId(rs.getInt(1));
                    au.setUsername(rs.getString(2));
                    au.setPassword(rs.getString(3));
                    au.setRealname(rs.getString(4));
                    au.setCreatetime(rs.getDate(5));
                    list.add(au);
                }
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
            close();
            return list;
        }
        
        public void init() {
            con = JdbcConnectionUtil.getConnection();
        }
    
        public void close() {
            JdbcConnectionUtil.destroy(con, ps, rs);
        }
    }
  • 相关阅读:
    WEB服务器防盗链_HttpAccessKeyModule_Referer(Nginx&&PHP)
    子查询2
    子查询
    接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码
    Django项目之【学员管理系统】
    Django 请求生命周期【图示】
    Django 之一些request封装的常用功能
    Django 认证系统 cookie & session & auth模块
    Django 模型系统(model)&ORM--进阶
    Django 模型系统(model)&ORM--基础
  • 原文地址:https://www.cnblogs.com/NCL--/p/7424365.html
Copyright © 2011-2022 走看看