需要改进的增删查

来源:互联网 发布:ubuntu 五笔拼音 编辑:程序博客网 时间:2024/04/28 11:35

1。环境

  JDK1.6 +Tomcat5.5+eclipse-jee-galileo-win32.zip+struts-2.1.6-all.zip+tomcatPluginV3beta.zip

 (

①配置JRE: 启动Eclipse,单击【Window】→【Preferences】,在左边的面板中选择【Java】→【Installed JREs】,选择自己安装的JDK的jre目录,  JDK版本必须1.5以上。

②配置Tomcat: 单击【Window】→【Preferences】,在左边的面板中选择【Tomcat】,version选择5.x,设置TomcatHome、Tomcat base为Tomcat5.5的安装目录、JVM1.6

③配置Server: 单击【Window】→【Preferences】,在左边的面板中选择【Server】→【Runtime Environment】→Add→选择Tomcat v5.5,【Next>】

 

 

 

2.做成的文件

①web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>MyTest</display-name>
 
 <filter>
     <filter-name>s2filter</filter-name>
     <filter-class>org.seasar.framework.container.filter.S2ContainerFilter</filter-class>
 </filter>
 
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <welcome-file-list>
  <welcome-file>pages/login.jsp</welcome-file>
 </welcome-file-list>
</web-app>

②struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
    <package name="struts2" extends="struts-default">
     <action name="Login" class="Action.LoginAction">
      <result name="success">/pages/login.jsp</result>
     </action>
    
     <action name="Delete" class="Action.LoginAction" method="delete">
      <result name="success">/pages/login.jsp</result>
     </action>
    </package>
   
    <bean type="Form.ShainForm" name="shainForm" class="Form.ShainForm"/>
</struts>

 

③login.jsp(用到了jstl标签c:foreach,只需要把从http://www.apache.org/dist/jakarta/taglibs/standard/

上面下载的c.tld文件copy到WEB-INF目录下)

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Login</title>
<script   language="javascript">  
function btnDel_OnClick() {
 targetForm = document.forms[0];
 targetForm.action = "Delete";
 targetForm.submit();

</script>
</head>
<body>
<s:form action="Login">
<c:if test="${records != null}">
 <TR style="HEIGHT: 25px"><!-- HEAD -->
  <TD>社員コード</TD>
  <TD>社員名称</TD>
  <TD>社員名称カナ</TD>
 </TR>
 <c:forEach items="${records}" var="record"> 
 <TR>
  <TD width="80">
   <input name="DetailShainCd" value="${record.shainCd}">
  </TD>
  <TD width="80">
   <input name="DetailShainNm" value="${record.shainNm}">
  </TD>
  <TD width="80">
   <input name="DetailShainNmk" value="${record.shainNmk}">
  </TD>
 </TR>
 </c:forEach>
 <TR></TR>
 <TR></TR>
 <TR></TR>
 <TR></TR>
</c:if>     
<s:textfield name="shainCd" label="社員コード"/>
<s:textfield name="shainNm" label="社員名称"/>
<s:textfield name="shainNmk" label="社員名称カナ"/>
<s:submit value="追加"/>
<s:submit name="btnDel" value="削除" onclick="btnDel_OnClick();">
</s:submit>
</s:form>
</body>
</html>

 

④LoginAction.java

package Action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;

import Dao.GetShainListDao;
import Dto.ShainDto;
import Form.ShainForm;

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{
 
 private static final long serialVersionUID = 1L;
// private GetShainListDao dao;
// public void setDao(GetShainListDao dao) {
//  this.dao = dao;
// }
// private ShainForm shainForm;
// public void setShainForm(ShainForm shainForm) {
//  this.shainForm = shainForm;
// }
// public ShainForm getShainForm() {
//  return shainForm;
// }
 private String shainCd;
 private String shainNm;
 private String shainNmk;
 private ArrayList<ShainDto> records;

    public ArrayList<ShainDto> getRecords() {
  return records;
 }

 public void setRecords(ArrayList<ShainDto> records) {
  this.records = records;
 }
 
 public String getShainCd() {
  return shainCd;
 }

 public void setShainCd(String shainCd) {
  this.shainCd = shainCd;
 }

 public String getShainNm() {
  return shainNm;
 }

 public void setShainNm(String shainNm) {
  this.shainNm = shainNm;
 }

 public String getShainNmk() {
  return shainNmk;
 }

 public void setShainNmk(String shainNmk) {
  this.shainNmk = shainNmk;
 }
 public String execute() throws Exception {
  
//  S2Container container = S2ContainerFactory.create ("app.dicon"); 
//        container.init();  
  
        String DB_URL = "jdbc:oracle:thin:@123.123.123.123:1521:DBname"; 
        String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
        String DB_USERNAME = "username";
        String DB_PASSWORD = "password";
        Connection conn = null;
        try{
            
            Class.forName(DB_DRIVER);
            conn = DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);
         if(shainCd!=null || !shainCd.isEmpty()){           
             PreparedStatement insertPreparedStatement = null;  
             String   locInsertSQL   =   "INSERT INTO table "   +  
             "(col1,col2,col3)   "   +  
             "VALUES   (?,?,?)";
             insertPreparedStatement=   conn.prepareStatement(locInsertSQL);  
             insertPreparedStatement.setString(1,shainCd);  
             insertPreparedStatement.setString(2,shainNm);  
             insertPreparedStatement.setString(3,shainNmk);  
             insertPreparedStatement.executeUpdate();  
    insertPreparedStatement.close();
         }

            PreparedStatement prepStmt = null;
            ResultSet rs=null;
            String selectStatement =null;
            selectStatement ="select col1,col2,col3 from table";
            prepStmt=conn.prepareStatement(selectStatement);
            rs=prepStmt.executeQuery();
           
            ArrayList<ShainDto> tmp = new ArrayList<ShainDto>();
            while(rs.next()){
             ShainDto dto = new ShainDto();
             dto.setShainCd(rs.getString(1));
             dto.setShainNm(rs.getString(2));
             dto.setShainNmk(rs.getString(3));
             tmp.add(dto);
            }
            if(tmp.size() != 0){
                records = tmp; 
            }else{
             records = null;
            }

            rs.close();
            prepStmt.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
         conn.close();
        }
  
     return "success";
    }
 

 public String delete() throws Exception {

        String DB_URL = "jdbc:oracle:thin:@123.123.123.123:1521:DBname"; 
        String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
        String DB_USERNAME = "username";
        String DB_PASSWORD = "password"; 
    Connection conn = null;
    try{
        
        Class.forName(DB_DRIVER);
        conn = DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);        
        PreparedStatement deletePreparedStatement = null;  
        String   sql   =   "delete from table where col1='"+ shainCd+ "'";
        deletePreparedStatement=   conn.prepareStatement(sql);  
        deletePreparedStatement.executeUpdate();  
        deletePreparedStatement.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
     conn.close();
    }
  return "success"; 
 }
}