如何使用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{ //数据封装类