我从CSDN下载的JSP+MYSQL的源码怎么用

我从CSDN下载的JSP+MYSQL的源码怎么用,第1张

把你的项目导入MyEclipse中,然后把sql文件导入mysql中,生成本地的数据库,接着找到项目中配置数据库连接的java类,把数据库的用户名和密码改成你自己的数据库的用户名和密码(这步很重要),最后把项目部署到tomcat中,启动tomcat服务器,就可以运行了!祝你好运!

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

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

CustomerDao.java

package cn.itcast.dao

import java.util.List

import cn.itcast.domain.Customer

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)

}

CustomerDaoJdbcImpl.java

package cn.itcast.dao.impl

import java.sql.Connection

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.util.ArrayList

import java.util.List

import cn.itcast.dao.CustomerDao

import cn.itcast.domain.Customer

import cn.itcast.util.JdbcUtils

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 = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

st.setString(1, customer.getName())

st.setString(2, customer.getSex())

st.setDate(3, new java.sql.Date(customer.getBirthday().getTime()))

st.setString(4, customer.getCellphone())

st.setString(5, customer.getEmail())

st.setString(6, customer.getPreference())

st.setString(7, customer.getType())

st.setString(8, customer.getDescription())

st.executeUpdate()

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(rs, st, conn)

}

}

public void delete(int id) {

Connection conn = null

PreparedStatement st = null

ResultSet rs = null

try{

conn = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

st.setInt(1, id)

st.executeUpdate()

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(rs, st, conn)

}

}

public Customer find(int id) {

Connection conn = null

PreparedStatement st = null

ResultSet rs = null

try{

conn = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

st.setInt(1, id)

rs = st.executeQuery()

if(rs.next()){

Customer c = new Customer()

c.setId(rs.getInt("id"))

c.setName(rs.getString("name"))

c.setSex(rs.getString("sex"))

c.setBirthday(rs.getDate("birthday"))

c.setCellphone(rs.getString("cellphone"))

c.setEmail(rs.getString("email"))

c.setPreference(rs.getString("preference"))

c.setType(rs.getString("type"))

c.setDescription(rs.getString("description"))

return c

}

return null

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(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 = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

rs = st.executeQuery()

List list = new ArrayList()

while(rs.next()){

Customer c = new Customer()

c.setId(rs.getInt("id"))

c.setName(rs.getString("name"))

c.setSex(rs.getString("sex"))

c.setBirthday(rs.getDate("birthday"))

c.setCellphone(rs.getString("cellphone"))

c.setEmail(rs.getString("email"))

c.setPreference(rs.getString("preference"))

c.setType(rs.getString("type"))

c.setDescription(rs.getString("description"))

list.add(c)

}

return list

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(rs, st, conn)

}

}

public void update(Customer customer) {

Connection conn = null

PreparedStatement st = null

ResultSet rs = null

try{

conn = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

st.setString(1, customer.getName())

st.setString(2, customer.getSex())

st.setDate(3, new java.sql.Date(customer.getBirthday().getTime()))

st.setString(4, customer.getCellphone())

st.setString(5, customer.getEmail())

st.setString(6, customer.getPreference())

st.setString(7, customer.getType())

st.setString(8, customer.getDescription())

st.setInt(9, customer.getId())

st.executeUpdate()

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(rs, st, conn)

}

}

public int getAllRecord() {

Connection conn = null

PreparedStatement st = null

ResultSet rs = null

try{

conn = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

rs = st.executeQuery()

if(rs.next()){

return rs.getInt(1)

}

return 0

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(rs, st, conn)

}

}

public List getCustomerByPage(int startindex, int pagesize) {

Connection conn = null

PreparedStatement st = null

ResultSet rs = null

try{

conn = JdbcUtils.getConnection()

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

st = conn.prepareStatement(sql)

st.setInt(1, startindex)

st.setInt(2, pagesize)

rs = st.executeQuery()

List list = new ArrayList()

while(rs.next()){

Customer c = new Customer()

c.setId(rs.getInt("id"))

c.setName(rs.getString("name"))

c.setSex(rs.getString("sex"))

c.setBirthday(rs.getDate("birthday"))

c.setCellphone(rs.getString("cellphone"))

c.setEmail(rs.getString("email"))

c.setPreference(rs.getString("preference"))

c.setType(rs.getString("type"))

c.setDescription(rs.getString("description"))

list.add(c)

}

return list

}catch(Exception e){

throw new RuntimeException(e)

}finally{

JdbcUtils.release(rs, st, conn)

}

}

}


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

原文地址: http://outofmemory.cn/zaji/6099509.html

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

发表评论

登录后才能评论

评论列表(0条)

保存