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

          主站蜘蛛池模板: 五台县| 惠水县| 湖北省| 阿图什市| 顺平县| 水富县| 乡宁县| 陆良县| 苏尼特右旗| 黄龙县| 镇安县| 海原县| 保亭| 开鲁县| 娱乐| 新疆| 台南县| 涟水县| 尚志市| 大兴区| 镇远县| 辽阳市| 苗栗市| 德格县| 衢州市| 八宿县| 天长市| 长治县| 茌平县| 商洛市| 奈曼旗| 文登市| 六枝特区| 新巴尔虎右旗| 宝山区| 晋江市| 池州市| 静安区| 喀喇沁旗| 襄城县| 佛山市|