package com.jsp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.jsp.util.ConnectionUtils;
import com.jsp.vo.Client;
public class clientDaoImp implements clientDao {
@Override
public List<Client> findClientsByName(String clientname) {
Connection conn = ConnectionUtils.getConnection();
String sql = "select * from client where clientname like '%"+clientname+"%'";
List<Client> list = new ArrayList<Client>();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Client client = new Client();
client.setClientid(rs.getString("clientid"));
client.setClientname(rs.getString("clientname"));
client.setReducemode(rs.getString("reducemode"));
client.setChoice(rs.getString("choice"));
client.setAddress(rs.getString("address"));
client.setLinkman(rs.getString("linkman"));
client.setLinkphone(rs.getString("linkphone"));
client.setPaymentmode(rs.getString("paymentmode"));
client.setPri(rs.getString("pri"));
list.add(client);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@Override
public boolean addClient(Client client) {
Connection conn = ConnectionUtils.getConnection();
String sql = "insert into client(clientid,clientname,reducemode,choice,address,linkman,linkphone,paymentmode,pri) values(?,?,?,?,?,?,?,?,?)";
try{
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, client.getClientid());
pstmt.setString(2, client.getClientname());
pstmt.setString(3, client.getReducemode());
pstmt.setString(4, client.getChoice());
pstmt.setString(5, client.getAddress());
pstmt.setString(6, client.getLinkman());
pstmt.setString(7, client.getLinkphone());
pstmt.setString(8, client.getPaymentmode());
System.out.println(client.getPaymentmode());
pstmt.setString(9, client.getPri());
int i = pstmt.executeUpdate();
if (i == 1) {
conn.commit();
return true;
}
}catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
@Override
public boolean delClient(String[] clientid) {
Connection conn = ConnectionUtils.getConnection();
String str ="";
for(int i=0;i<clientid.length;i++){
if(i==clientid.length-1){
str=str+"'"+clientid[i]+"'";
}else{
str=str+"'"+clientid[i]+"',";
}
}
String sql = "delete from client where clientid in("+str+")";
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
int i = stmt.executeUpdate(sql);
System.out.println("所影响的记录数为"+i);
if (i >= 1) {
conn.commit();
return true;
}
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
@Override
public List<Client> listClients() {
Connection conn = ConnectionUtils.getConnection();
String sql = "select * from client";
List<Client> list = new ArrayList<Client>();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Client client = new Client();
//clientid,clientname,reducemode,choice,address,linkman,linkphone,paymentmode,pri
client.setClientid(rs.getString("clientid"));
client.setClientname(rs.getString("clientname"));
client.setReducemode(rs.getString("reducemode"));
client.setChoice(rs.getString("choice"));
client.setAddress(rs.getString("address"));
client.setLinkman(rs.getString("linkman"));
client.setLinkphone(rs.getString("linkphone"));
client.setPaymentmode(rs.getString("paymentmode"));
client.setPri(rs.getString("pri"));
list.add(client);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@Override
public boolean modClient(Client client) {
Connection conn = ConnectionUtils.getConnection();
String sql = "update client set clientname='"+client.getClientname()+"',reducemode='"+client.getReducemode()+"',choice='"
+client.getChoice()+"',address='"+client.getAddress()+"',linkman='"+client.getLinkman()+"',linkphone='"
+client.getLinkphone()+"',paymentmode='"+client.getPaymentmode()+"',pri='"+client.getPri()+"' where clientid='"
+client.getClientid()+"'";
System.out.println(sql);
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
int i = stmt.executeUpdate(sql);
System.out.println("所影响的记录数为"+i);
if (i == 1) {
conn.commit();
return true;
}
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public Client findClientById(String clientid) {
Connection conn = ConnectionUtils.getConnection();
String sql = "select * from client where clientid="+clientid ;
Client client = null;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
client = new Client();
//clientid,clientname,reducemode,choice,address,linkman,linkphone,paymentmode,pri
client.setClientid(rs.getString("clientid"));
client.setClientname(rs.getString("clientname"));
client.setReducemode(rs.getString("reducemode"));
client.setChoice(rs.getString("choice"));
client.setAddress(rs.getString("address"));
client.setLinkman(rs.getString("linkman"));
client.setLinkphone(rs.getString("linkphone"));
client.setPaymentmode(rs.getString("paymentmode"));
client.setPri(rs.getString("pri"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return client;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
jsp+servlet+eclipse+mysql(包含源码和数据库),含增删改查功能,导入数据库,就可以运行。 各层的类如下: com.jsp.dao ---clientDao.java ---clientDaoImp.java ---DaoFactory.java com.jsp.service ---addClientServlet.java ---delClientServlet.java ---findClientsServlet.java ---listClientsServlet.java ---modClientServlet.java com.jsp.util ---ConnectionUtils.java ---EncodingFilter.java ---db.properties com.jsp.vo ---Client.java
资源推荐
资源详情
资源评论
收起资源包目录
jsp+servlet+eclipse+mysql.rar (46个子文件)
jsp.sql 2KB
JSPhomework
.project 1KB
.settings
.jsdtscope 454B
org.eclipse.wst.common.component 428B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.common.project.facet.core.xml 320B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
com
jsp
dao
clientDao.java 396B
clientDaoImp.java 7KB
DaoFactory.java 772B
vo
Client.java 2KB
service
modClientServlet.java 2KB
listClientsServlet.java 1KB
delClientServlet.java 978B
addClientServlet.java 2KB
findClientByIdServlet.java 801B
util
TestConnection.java 909B
EncodingFilter.java 3KB
db.properties 143B
ConnectionUtils.java 1KB
build
classes
com
jsp
dao
clientDao.class 528B
DaoFactory.class 1KB
clientDaoImp.class 8KB
vo
Client.class 2KB
service
modClientServlet.class 2KB
listClientsServlet.class 2KB
delClientServlet.class 1KB
findClientByIdServlet.class 1KB
addClientServlet.class 2KB
util
db.properties 143B
ConnectionUtils.class 3KB
TestConnection.class 1KB
EncodingFilter.class 2KB
WebContent
css
style.css 150B
WEB-INF
web.xml 3KB
lib
mysql-connector-java-5.1.16-bin.jar 768KB
standard-1.0.6.jar 497KB
jstl-1.0.2.jar 20KB
index.jsp 390B
addClient.jsp 3KB
META-INF
MANIFEST.MF 39B
listClient.jsp 2KB
images
edit.gif 617B
modClient.jsp 5KB
.classpath 845B
共 46 条
- 1
ko16078
- 粉丝: 3
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页