用户登录
用户注册

分享至

如何使用jsp+servlet实现增删改查代码功能

  • 作者: 左医生
  • 来源: 51数据库
  • 2021-09-22

如何使用jsp+servlet实现增删改查代码功能

package ceet.ac.cn.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 ceet.ac.cn.model.*****;

public class *****Dao {

public List*****> getAll*****(){ //查询所有信息

List*****> list = new ArrayList*****>(); //创建集合

Connection conn = DbHelper.getConnection();

String sql = "select * from *****"; //SQL查询语句

try {

PreparedStatement pst = conn.prepareStatement(sql);

ResultSet rst = pst.executeQuery();

while (rst.next()) {

***** ***** = new *****();

*****.setId(rst.getInt("id")); //得到ID

*****.setUsername(rst.getString("username"));

*****.setUserpwd(rst.getString("userpwd"));

list.add(*****);

}

rst.close(); //关闭

pst.close(); //关闭

} catch (SQLException e) {

e.printStackTrace(); //抛出异常

}

return list; //返回一个集合

}

public boolean add*****(***** *****){ //添加信息

String sql = "INSERT INTO `*****`(`id`,`username`,`userpwd`) VALUES (?,?,?)"; //添加的SQL语句

Connection conn = DbHelper.getConnection();

try {

PreparedStatement pst = conn.prepareStatement(sql);

pst.setInt(1, *****.getId());

pst.setString(2, *****.getUsername());

pst.setString(3, *****.getUserpwd());

int count = pst.executeUpdate();

pst.close();

return count>0?true:false; //是否添加的判断

} catch (SQLException e) {

e.printStackTrace();

}

return false;

}

public boolean update*****(***** *****){ //修改

String sql = "UPDATE `*****` SET `username`=?,`userpwd`=? WHERE `id` = ?"; //修改的SQL语句,根据ID修改

Connection conn = DbHelper.getConnection();

try {

PreparedStatement pst = conn.prepareStatement(sql);

pst.setString(1, *****.getUsername());

pst.setString(2, *****.getUserpwd());

pst.setInt(3, *****.getId()); //根据的ID

int count = pst.executeUpdate();

pst.close(); //关闭

return count>0?true:false; //是否修改的判断

} catch (SQLException e) {

e.printStackTrace();

}

return false;

}

public boolean delete*****(int id){ //删除

String sql = "delete from ***** where id = ?"; //删除的SQL语句,根据ID删除

Connection conn = DbHelper.getConnection();

try {

PreparedStatement pst = conn.prepareStatement(sql);

pst.setInt(1, id);

int count = pst.executeUpdate();

pst.close();

return count>0?true:false; //是否删除的判断

} catch (SQLException e) {

e.printStackTrace();

}

return false;

}

public ***** select*****ById(int id){ //根据ID进行查询

Connection conn = DbHelper.getConnection();

String sql = "select * from ***** where id = "+id;

***** ***** = null;

try {

PreparedStatement pst = conn.prepareStatement(sql);

ResultSet rst = pst.executeQuery();

while (rst.next()) {

***** = new *****();

*****.setId(rst.getInt("id"));

*****.setUsername(rst.getString("username"));

*****.setUserpwd(rst.getString("userpwd"));

}

rst.close();

pst.close();

} catch (SQLException e) {

e.printStackTrace();

}

return *****; //返回

}

}

package ceet.ac.cn.dao;

import java.sql.Connection;

import java.sql.DriverManager;

/**

* 连接数据库

* @author 画船听雨眠

*

*/

public class DbHelper {

private static String url = "jdbc:mysql://localhost:3306/*****"; //数据库地址

private static String userName = "root"; //数据库用户名

private static String passWord = "359129127"; //数据库密码

private static Connection conn = null;

private DbHelper(){

}

public static Connection getConnection(){

if(null == conn){

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(url, userName, passWord);

} catch (Exception e) {

e.printStackTrace();

}

}

return conn;

}

public static void main(String[] args) { //测试数据库是否连通

System.err.println(getConnection());

}

}

package ceet.ac.cn.model;

import java.io.Serializable;

public class ***** implements Serializable{ //数据封装类



软件
前端设计
程序设计
Java相关