jsp数据库增删改查项目,jsp增删改查代码怎么写

发布时间:2022-11-23

本文目录一览:

  1. 怎么用jsp做登录,增删查改
  2. jsp怎么连接数据库做增删改查
  3. 如何在JSP页面中实现对数据库的增删查改?
  4. jsp连接mysql数据库后增删改查怎么写
  5. jsp增删改查怎么写

怎么用jsp做登录,增删查改

  1. 先学会一种数据库的使用。推荐MySQL吧。可以上什么菜鸟教程啊什么的去参考。 2天速成
  2. 学会使用Java中的JDBC来连接数据库,并进行增删改查操作。注意,所有数据库语句都要使用PrepareStatement。 半天搞定
  3. 上各种菜鸟教程,w3school等,掌握html css javascript的使用。1天半速成
  4. 尝试使用eclipse新建dynamic web project,并成功创建一个jsp的Hello World页面。 半天搞定
  5. 学习并使用javabean来将jsp页面和java程序相结合起来,并能在页面上输出各种java程序的结果。将jsp和html知识融合,能做出有点像样的页面。1天
  6. 融汇贯通1-5,边查漏补缺边完成你boss的任务。不要忘记搜索引擎永远是最好的老师。1天

jsp怎么连接数据库做增删改查

数据库连接类:

package cn.hpu.bbs.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
    // 定义MySQL的数据库驱动程序
    public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
    //定义mysql的数据库连接地址:
    public static final String DBDURL = "jdbc:mysql://localhost/bbs2014" ;
    //mysql数据库的连接用户名
    public static final String DBUSER = "root" ;
    //mysql数据库的连接密码
    public static final String DBPASS = "1234" ;
    public static Connection createConn(){
        Connection conn = null;
        try {
            Class.forName(DBDRIVER);
            conn = DriverManager.getConnection(DBDURL, DBUSER, DBPASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static PreparedStatement prepare(Connection conn, String sql){
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ps;
    }
    public static void close(Connection conn){
        if (conn == null) return;
        try {
            conn.close();
            conn = null;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(Statement stmt){
        if (stmt == null) return;
        try {
            stmt.close();
            stmt = null;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(ResultSet rs){
        if (rs == null) return;
        try {
            rs.close();
            rs = null;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Category的一个JavaBean:

package cn.hpu.bbs.model;
public class Category {
    private int id;
    private String name;
    private String description;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

对数据库和Category的操作类://说白了就是增删查修

package cn.hpu.bbs.service;
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 cn.hpu.bbs.model.Category;
import cn.hpu.bbs.util.DB;
public class CategoryService {
    public void add(Category c){
        Connection conn = DB.createConn();
        String sql = "insert into category (name,description) values (?,?)";
        PreparedStatement ps = DB.prepare(conn, sql);
        try {
            ps.setString(1, c.getName());
            ps.setString(2, c.getDescription());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DB.close(ps);
        DB.close(conn);
    }
    public List<Category> list(){
        Connection conn = DB.createConn();
        String sql = "select * from category";
        PreparedStatement ps = DB.prepare(conn, sql);
        List<Category> categories = new ArrayList<Category>();
        try {
            ResultSet rs = ps.executeQuery();
            Category c = null;
            while(rs.next()){
                c = new Category();
                c.setId(rs.getInt("id"));
                c.setName(rs.getString("name"));
                c.setDescription(rs.getString("description"));
                categories.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DB.close(ps);
        DB.close(conn);
        return categories;
    }
    public void delete(Category c){
        deleteById(c.getId());
    }
    public void deleteById(int id){
        Connection conn = DB.createConn();
        String sql = "delete from category where id=?";
        PreparedStatement ps = DB.prepare(conn, sql);
        try {
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DB.close(ps);
        DB.close(conn);
    }
    public void update(Category c){
        Connection conn = DB.createConn();
        String sql = "update category set name = ? , description = ? where id = ?";
        PreparedStatement ps = DB.prepare(conn, sql);
        try {
            ps.setString(1, c.getName());
            ps.setString(2, c.getDescription());
            ps.setInt(3, c.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DB.close(ps);
        DB.close(conn);
    }
    public Category loadById(int id){
        Connection conn = DB.createConn();
        String sql = "select * from category where id=?";
        PreparedStatement ps = DB.prepare(conn, sql);
        Category c = null;
        try {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                c = new Category();
                c.setId(rs.getInt("id"));
                c.setName(rs.getString("name"));
                c.setDescription(rs.getString("description"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DB.close(ps);
        DB.close(conn);
        return c;
    }
}

如何在JSP页面中实现对数据库的增删查改?

首先我觉得你的问题不太明确,做增删改查的话一般不用ajax,除非其中要用到单独的验证字段的时候采用,比如在注册时验证用户名,这里用到ajax查询用户名是否存在,返回给页面直接用流打回页面就行(比如:此用户名可用之类的)而其他的查询比如显示所有或者查询的结果为对象的话那看你用什么框架(controller),struts直接封装到值栈中,在页面用标签显示就行,不知道能不能帮到你

jsp连接mysql数据库后增删改查怎么写

建议使用MVC模式做,JSP页面提交相应的操作后,提交给Servlet,Servlet中调用Model中定义的增删改查方法,方法调用后返回结果,然后通过Servlet返回给JSP页面。对于前台的增删改查跟数据库中中新建查询的操作是一样的,只是JSP页面增删改查是调用数据库查询语句封装的函数方法而已!

jsp增删改查怎么写

package test.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import test.entity.Emp;
import test.util.BaseDao;
public class EmpDao {
    // 查找数据库Emp表所有人信息
    public List<Emp> findAll() throws Exception {
        Connection conn = BaseDao.getCon();
        Statement stat = conn.createStatement();
        String sql = "select * from tblemp,tbldept where tbldept.deptid=tblemp.deptid";
        ResultSet rs = stat.executeQuery(sql);
        List<Emp> list = new ArrayList<Emp>();
        while (rs.next()) {
            Emp e = new Emp();
            e.setEmpid(rs.getInt("empid"));
            e.setEname(rs.getString("ename"));
            e.setDname(rs.getString("dname"));
            list.add(e);
        }
        BaseDao.close(conn);
        return list;
    }
    // 往Emp表添加新员工
    public void save(Emp e) throws Exception {
        Connection conn = BaseDao.getCon();
        String sql = "insert into tblemp(ename,egendar,deptid) values(?,?,?)";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setString(1, e.getEname());
        prep.setDouble(2, e.getEgendar());
        prep.setInt(3, e.getDeptid());
        prep.executeUpdate();
        BaseDao.close(conn);
    }
    // 根据id删除该员工
    public void delete(int id) throws Exception {
        Connection conn = BaseDao.getCon();
        String sql = "delete from tblemp where empid=?";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setLong(1, id);
        prep.executeUpdate();
        BaseDao.close(conn);
    }
    // 根据id查找该员工信息
    public Emp findById(int id) throws Exception {
        Connection conn = BaseDao.getCon();
        Emp e = null;
        String sql = "select * from tblemp,tbldept where empid=? and tbldept.deptid=tblemp.deptid";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setLong(1, id);
        ResultSet rs = prep.executeQuery();
        if (rs.next()) {
            e = new Emp();
            e.setEmpid(id);
            e.setEname(rs.getString("ename"));
            e.setEgendar(rs.getInt("egendar"));
            e.setDname(rs.getString("dname"));
            e.setDeptid(rs.getInt("deptid"));
        }
        return e;
    }
    // 根据id修改该员工
    public void update(Emp e) throws Exception {
        Connection conn = BaseDao.getCon();
        String sql = "update tblemp set ename=?,egendar=?,deptid=? where empid=?";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setString(1, e.getEname());
        prep.setInt(2, e.getEgendar());
        prep.setInt(3, e.getDeptid());
        prep.setLong(4, e.getEmpid());
        prep.executeUpdate();
        BaseDao.close(conn);
    }
}

我把我写过的给你参考,你只需要修改成自己的字段就能用了。 需要用到哪个方法,就调用它。

EmpDao dao = new EmpDao();
// 增加就dao.save(数据);
// 删除就dao.delete(id);
// 查找就dao.findAll();
// 修改就dao.update(内容);

希望帮到你