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);
        }
    }
  • 相关阅读:
    程序员为什么难管理?
    Python多继承
    如何从程序员走向管理层?
    为什么Python能超越JAVA,有什么优势?
    HTTP协议简介与在python中的使用详解
    人力资源管理书籍推荐,这本书HR必看
    把市面上的书翻了个遍,找到这五本经典营销管理书籍推荐给大家
    服务器部署之 cap deploy:setup
    【转】D3DXLoadSkinMeshFromXof函数及.x在不同dx版本中
    【转】C/C++字节对齐算法
  • 原文地址:https://www.cnblogs.com/NCL--/p/7424365.html
Copyright © 2011-2022 走看看