求JSP向mysql插入,删除,查询数据源代码

求JSP向mysql插入,删除,查询数据源代码,第1张

还有这样的需求呀?无奇不有……

写的dao的实现就行了呀 ,何必得要jsp呢?用jsp,只会越来越乱^

CustomerDaojava

package cnitcastdao;

import javautilList;

import cnitcastdomainCustomer;

public interface CustomerDao {

public void add(Customer customer);

public Customer find(int id);

public List getAllCustomer();

public void delete(int id);

public void update(Customer customer);

public int getAllRecord();

public List getCustomerByPage(int startindex,int pagesize);

}

CustomerDaoJdbcImpljava

package cnitcastdaoimpl;

import javasqlConnection;

import javasqlPreparedStatement;

import javasqlResultSet;

import javautilArrayList;

import javautilList;

import cnitcastdaoCustomerDao;

import cnitcastdomainCustomer;

import cnitcastutilJdbcUtils;

public class CustomerDaoJdbcImpl implements CustomerDao {

/

id int primary key auto_increment,

name varchar(20) not null,

sex varchar(4) not null,

birthday date,

cellphone varchar(20) not null,

Email varchar(40),

preference varchar(100),

type varchar(40),

Description varchar(255)

/

public void add(Customer customer) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "insert into customer(name,sex,birthday,cellphone,email,preference,type,description) values(,,,,,,,)";

st = connprepareStatement(sql);

stsetString(1, customergetName());

stsetString(2, customergetSex());

stsetDate(3, new javasqlDate(customergetBirthday()getTime()));

stsetString(4, customergetCellphone());

stsetString(5, customergetEmail());

stsetString(6, customergetPreference());

stsetString(7, customergetType());

stsetString(8, customergetDescription());

stexecuteUpdate();

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

public void delete(int id) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "delete from customer where id=";

st = connprepareStatement(sql);

stsetInt(1, id);

stexecuteUpdate();

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

public Customer find(int id) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer where id=";

st = connprepareStatement(sql);

stsetInt(1, id);

rs = stexecuteQuery();

if(rsnext()){

Customer c = new Customer();

csetId(rsgetInt("id"));

csetName(rsgetString("name"));

csetSex(rsgetString("sex"));

csetBirthday(rsgetDate("birthday"));

csetCellphone(rsgetString("cellphone"));

csetEmail(rsgetString("email"));

csetPreference(rsgetString("preference"));

csetType(rsgetString("type"));

csetDescription(rsgetString("description"));

return c;

}

return null;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

/

Id 编号 varchar(20)

name 客户姓名 varchar(20)

sex 性名 varchar(4)

birthday 生日 date

cellphone 手机 varchar(20)

Email 电子邮件 varchar(40)

preference 客户爱好 varchar(100)

type 客户类型 varchar(40)

Description 备注 varchar(255)

/

public List getAllCustomer() {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer order by id";

st = connprepareStatement(sql);

rs = stexecuteQuery();

List list = new ArrayList();

while(rsnext()){

Customer c = new Customer();

csetId(rsgetInt("id"));

csetName(rsgetString("name"));

csetSex(rsgetString("sex"));

csetBirthday(rsgetDate("birthday"));

csetCellphone(rsgetString("cellphone"));

csetEmail(rsgetString("email"));

csetPreference(rsgetString("preference"));

csetType(rsgetString("type"));

csetDescription(rsgetString("description"));

listadd(c);

}

return list;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

public void update(Customer customer) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "update customer set name=,sex=,birthday=,cellphone=,email=,preference=,type=,description= where id=";

st = connprepareStatement(sql);

stsetString(1, customergetName());

stsetString(2, customergetSex());

stsetDate(3, new javasqlDate(customergetBirthday()getTime()));

stsetString(4, customergetCellphone());

stsetString(5, customergetEmail());

stsetString(6, customergetPreference());

stsetString(7, customergetType());

stsetString(8, customergetDescription());

stsetInt(9, customergetId());

stexecuteUpdate();

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

public int getAllRecord() {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "select count() from customer";

st = connprepareStatement(sql);

rs = stexecuteQuery();

if(rsnext()){

return rsgetInt(1);

}

return 0;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

public List getCustomerByPage(int startindex, int pagesize) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtilsgetConnection();

String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer limit ,";

st = connprepareStatement(sql);

stsetInt(1, startindex);

stsetInt(2, pagesize);

rs = stexecuteQuery();

List list = new ArrayList();

while(rsnext()){

Customer c = new Customer();

csetId(rsgetInt("id"));

csetName(rsgetString("name"));

csetSex(rsgetString("sex"));

csetBirthday(rsgetDate("birthday"));

csetCellphone(rsgetString("cellphone"));

csetEmail(rsgetString("email"));

csetPreference(rsgetString("preference"));

csetType(rsgetString("type"));

csetDescription(rsgetString("description"));

listadd(c);

}

return list;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtilsrelease(rs, st, conn);

}

}

}

用executeUpdate(SQL语句),

有DELETE,EDIT,DROP什么的,具体见SQL命令大全:

>

你可以给按钮配置方法,方法去实现连接数据库,然后删除你需要删除的信息。

然后,给按钮配置方法的话,需要前台的按钮和后台的代码连接,可以使用Servlet或者Struts的Action等方式。你的这个问题不是一下子能说清楚的,因为涉及到的模块比较多,你问得比较宽泛。你要是全部都不熟悉的话,可能要花点时间。

以上就是关于求JSP向mysql插入,删除,查询数据源代码全部的内容,包括:求JSP向mysql插入,删除,查询数据源代码、JSP如何修改MYSQL的数据(内详)、java web开发jsp页面如何通过 删除 按钮 删除数据库信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9354498.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存