superwei

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          簡單的struts應用開發(2)

          第二步: 配置數據庫

          1.首先創建數據聯接bean, 并打包到 ConnectionPool, 它將從我們配置的聯接池中取得可用聯接.

          // Java Document
          package ConnectionPool;
          import javax.naming.*;
          import javax.sql.*;
          import java.sql.*;
          import java.io.*;
          import java.util.*;

          public class getDbConnection {

          Connection conn;
          Statement stmt;
          ResultSet rs=null;

          String foo = "Not Connected";
          int bar = -1;

          public getDbConnection() {
          try{
          Context ctx = new InitialContext();
          if(ctx == null )
          throw new Exception("Boom - No Context");

          DataSource ds =
          (DataSource)ctx.lookup(
          "java:comp/env/jdbc/TestDB");

          if (ds != null) {
          conn = ds.getConnection();

          }
          }
          catch(Exception e) {
          e.printStackTrace();
          }
          }

          public Connection getCon()
          {
          return conn;
          }


          public void d_close() throws SQLException
          {
          if (rs != null) {
          try { rs.close(); } catch (SQLException e) { ; }
          rs = null;
          }
          if (stmt != null) {
          try { stmt.close(); } catch (SQLException e) { ; }
          stmt = null;
          }
          if (conn != null) {
          try { conn.close(); } catch (SQLException e) { ; }
          conn = null;
          }

          }

          protected void finalize() throws Throwable
          {
          try { d_close(); } catch (SQLException e) { ; }
          }

          }

          2.創建數據庫.

          數據庫名: mystruts

          部門表: mydep

          _______________________________________
          dep_id dep_name
          _______________________________________
          1 dep1
          2 dep2
          3 dep3
          4 dep4
          _______________________________________

          用戶表: myuser
          ______________________________________________
          user_id user_name dep tel
          ______________________________________________
          1 jack 1 0769-2454042
          2 max 2 0769-2454043
          ______________________________________________

          (注: 以上字段為了測試方便. 全部使用字符型)


          第三步: 創建ActionForm (userActionForm.java)

          package mystruts; //請打包到mystruts下.

          import org.apache.struts.action.*;
          import javax.servlet.http.*;

          public class userActionForm extends ActionForm {
          private String action="add";
          private String dep;
          private String tel;
          private String user_id;
          private String user_name;
          public String getAction() {
          return action;
          }
          public void setAction(String action) {
          this.action = action;
          }
          public String getDep() {
          return dep;
          }
          public void setDep(String dep) {
          this.dep = dep;
          }
          public String getTel() {
          return tel;
          }
          public void setTel(String tel) {
          this.tel = tel;
          }
          public String getUser_id() {
          return user_id;
          }
          public void setUser_id(String user_id) {
          this.user_id = user_id;
          }
          public String getUser_name() {
          return user_name;
          }
          public void setUser_name(String user_name) {
          this.user_name = user_name;
          }
          public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

          return null;
          }
          public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
          }
          }

          posted on 2005-10-31 10:30 小辭猬 閱讀(188) 評論(0)  編輯  收藏 所屬分類: JAVA

          主站蜘蛛池模板: 淮滨县| 夏津县| 胶南市| 镶黄旗| 双牌县| 铜山县| 军事| 平乐县| 维西| 宁夏| 和林格尔县| 昆明市| 怀柔区| 乌拉特中旗| 云和县| 平塘县| 乐业县| 昭觉县| 宜城市| 项城市| 大庆市| 博野县| 宜君县| 昆明市| 太湖县| 图片| 罗田县| 溧水县| 墨脱县| 普陀区| 肇庆市| 通榆县| 永州市| 宽城| 虎林市| 湖南省| 信阳市| 会理县| 锡林郭勒盟| 惠州市| 古浪县|