struts2Curd+Oracle
项目源码免费下载:http://download.youkuaiyun.com/detail/u012750578/6561655
项目结构:
所需jar包:或使用mysql,只需在c3p0连接池更改为mysql数据库即可
一、数据表设计:
--------------------------------------------------------
-- 文件已创建 - 星期三-十一月-13-2013
--------------------------------------------------------
--------------------------------------------------------
-- DDL for Table ACCOUNT
--------------------------------------------------------
CREATETABLE"SCOTT"."ACCOUNT"
( "PID" VARCHAR2(20 BYTE),
"PNAME" VARCHAR2(20 BYTE),
"PDEPT" VARCHAR2(20 BYTE)
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOLDEFAULT)
TABLESPACE"USERS" ;
REM INSERTINGinto SCOTT.ACCOUNT
SET DEFINE OFF;
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('1e8e','李五','技术部');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('7fe8','李四','李四');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('5c73','李四','李四');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('ec6d','小三','美女部');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('ea0e','李五李五','技术部');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('c7b8','李五','李五');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('78f9','李五','李五');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('735a','王六','技术部');
Insert into SCOTT.ACCOUNT (PID,PNAME,PDEPT)values ('41d8','小三','积淀');
二、C:Action
package com.test.action;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import javassist.expr.NewArray;
import com.opensymphony.xwork2.ActionSupport;
import com.test.dao.Account;
import com.test.dao.AccountDao;
public class AccountAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -2380007467627219743L;
private static AccountDao accountDao=new AccountDao() ;
private Account account;
private String id;
private List<Account> all;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Account> getAll() {
return all;
}
public void setAll(List<Account> all) {
this.all = all;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String queryAll(){
all=accountDao.accountList();
return "list";
}
public String queryById(){
try {
account=accountDao.findAccountById(id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "update";
}
public String save(){
accountDao.insert(account);
return queryAll();
}
public String delete(){
accountDao.deleteAccount(id);
return queryAll();
}
public String update(){
String pid=ServletActionContext.getRequest().getParameter("pid");
System.out.println("account pid="+pid);
account.setPid(pid);
accountDao.update(account);
return queryAll();
}
}
struts.xml配置:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="aa"extends="struts-default"namespace="/">
<action name="test" class="com.test.action.AccountAction" >
<result name="list">index.jsp</result>
<result name="update">update.jsp</result>
</action>
</package>
</struts>
web.xml配置:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">
<display-name>struts2Test</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.mycompany.myapp.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
三、M:
Account
package com.test.dao;
publicclass Account {
private Stringpid;
private Stringpname;
private Stringpdept;
public String getPid() {
returnpid;
}
publicvoid setPid(String pid) {
this.pid = pid;
}
public String getPname() {
returnpname;
}
publicvoid setPname(String pname) {
this.pname = pname;
}
public String getPdept() {
returnpdept;
}
publicvoid setPdept(String pdept) {
this.pdept = pdept;
}
}
AccountDao
package com.test.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.util.DbUtil;
public class AccountDao {
public List<Account> accountList(){
List<Account> all=new ArrayList<Account>();
QueryRunner runner = new QueryRunner(DbUtil.getDataSource());
String sql = "select * from Account";
try {
all= (List<Account>) runner.query(sql,new Object[]{},new BeanListHandler(Account.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return all;
}
public void update(Account account){
QueryRunner runner = new QueryRunner(DbUtil.getDataSource());
String sql = "update account set pname=?,pdept=? where pid=?";
try {
runner.update(sql,new Object[]{
account.getPname(),
account.getPdept(),
account.getPid()
});
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteAccount(String pid){
QueryRunner runner = new QueryRunner(DbUtil.getDataSource());
String sql = "delete from account where pid = ?";
try {
runner.update(sql,pid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(Account account){
QueryRunner runner = new QueryRunner(DbUtil.getDataSource());
String sql = "insert into account values(?,?,?)";
try {
runner.update(sql,new Object[]{
UUID.randomUUID().toString().substring(0,4),
account.getPname(),
account.getPdept()
});
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//根据ID号,查询某位客户信息
public Account findAccountById(String id) throws SQLException{
Account Account = null;
QueryRunner runner = new QueryRunner(DbUtil.getDataSource());
String sql = "select * from Account where pid = ?";
Account = (Account) runner.query(sql,id,new BeanHandler(Account.class));
return Account;
}
}
DbUtil
package com.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbUtil {
private static ComboPooledDataSource dataSource;
static{
dataSource = new ComboPooledDataSource();
}
//取得数据源
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
//取得连接
public static Connection getMySqlConnection() throws SQLException{
return dataSource.getConnection();
}
//关闭连接
public static void close(Connection conn) throws SQLException{
if(conn!=null){
conn.close();
}
}
public static void close(PreparedStatement pstmt) throws SQLException {
if(pstmt!=null){
pstmt.close();
}
}
public static void close(ResultSet rs) throws SQLException {
if(rs!=null){
rs.close();
}
}
public static void main(String[] args){
try{
if(DbUtil.getMySqlConnection()!=null)
System.out.println("获取连接成功!");
}catch(Exception e){
e.printStackTrace();
System.out.println("获取连接失败!");
}
}
}
c3p0配置
c3p0-config.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<c3p0-config>
<default-config>
<propertyname="driverClass">oracle.jdbc.driver.OracleDriver</property>
<propertyname="user">scott</property>
<propertyname="password">scott</property>
<propertyname="jdbcUrl">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
</default-config>
</c3p0-config>
测试
package com.testcase;
import java.sql.SQLException;
import java.util.List;
import org.junit.Test;
import com.test.dao.Account;
import com.test.dao.AccountDao;
public class AccountDaoTest {
public AccountDao accountDao;
public AccountDaoTest(){
accountDao=new AccountDao();
}
@Test
public void queryAll(){
List<Account> all=accountDao.accountList();
for(Account a:all)
System.out.println(a.getPid()+a.getPname());
}
@Test
public void querybyId(){
Account account=new Account();
try {
account=accountDao.findAccountById("1");
System.out.println(account.getPid()+account.getPname());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void update(){
Account account=new Account();
account.setPid("1");
account.setPdept("信息部");
account.setPname("小张");
accountDao.update(account);
}
@Test
public void insert(){
Account account=new Account();
account.setPdept("账务部");
account.setPname("小王");
accountDao.insert(account);
}
@Test
public void delete(){
String id="978f";
accountDao.deleteAccount(id);
}
}
四、V:
index.jsp:
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<tablewidth="600px"border="1">
<tr>
<tdcolspan="5"align="center">信息列表</td>
</tr>
<tr >
<td>ID</td>
<td>PNAME</td>
<td>PDEPT</td>
<tdcolspan="2">操作 <ahref="add.jsp">添加</a></td>
</tr>
<s:iterator value="all" var="li">
<tr>
<td><s:property value="#li.pid"/></td>
<td><s:property value="#li.pname"/></td>
<td><s:property value="#li.pdept"/></td>
<td><a href='<s:url action="test!queryById"><s:param name="id" value="#li.pid" /></s:url>'>修改</a></td>
<td><a href='<s:url action="test!delete"><s:param name="id" value="#li.pid" /></s:url>'>删除 </a></td>
<tr>
</s:iterator>
<table>
</body>
</html>
add.jsp
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@taglib prefix="s" uri="/struts-tags" %>
<s:form action="test!save">
<s:textfieldname="account.pname"label="姓名"/>
<s:textfieldname="account.pdept"label="部门"/>
<s:submitvalue="增加"/>
</s:form>
</body>
</html>
update.jsp
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="test!update" method="post">
<s:iterator value="account" var="n" >
编号为:<s:property value="#n.pid" /><br>
<s:textfield name="account.pname" value="%{#n.pname}"label="姓名:"></s:textfield><br>
<s:textfieldname="account.pdept" value="%{#n.pdept}"label="部门:"></s:textfield><br>
<s:submitname="pid" value="%{#n.pid}"></s:submit>
</s:iterator>
</s:form>
</body>
</html>
运行截图:
http://localhost:8080/struts2Test/test!queryAll
添加:
http://localhost:8080/struts2Test/add.jsp
添加成功:
更新:
更新成功:
删除: