码迷,mamicode.com
首页 > 数据库 > 详细

jdbc数据库中的增删改

时间:2017-08-24 19:48:57      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:log   des   session   imp   dba   exception   time   ddb   ring   

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);
    }
}

 

jdbc数据库中的增删改

标签:log   des   session   imp   dba   exception   time   ddb   ring   

原文地址:http://www.cnblogs.com/NCL--/p/7424365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!