sclsch

          java備忘

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            10 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks

          2008年10月5日 #

          package com.secondHand.db;

          import java.math.BigDecimal;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.ResultSet;
          import java.sql.ResultSetMetaData;
          import java.sql.SQLException;
          import java.sql.Statement;
          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.List;

          public class DBUtil {
              
          private String url = "jdbc:mysql://localhost:3306/db_secondhand?user=root&password=root";

              
          private String dbUserName = "root";

              
          private String dbUserPassword = "root";

              
          private String driver = "com.mysql.jdbc.Driver";

              
          private Connection conn = null;

              
          private Statement stmt = null;

              
          private ResultSet rs = null;

              
          public DBUtil() {
                  
          try {
                      Class.forName(driver);
                  } 
          catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }
              }

              
          private Connection getConnection() {
                  
          try {
                      conn 
          = DriverManager.getConnection(url, dbUserName, dbUserPassword);
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                  }
                  
          return conn;
              }

              
          private void close(ResultSet rs, Statement stmt, Connection conn) {
                  
          if (rs != null) {
                      
          try {
                          rs.close();
                      } 
          catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
                  
          if (stmt != null) {
                      
          try {
                          stmt.close();
                      } 
          catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
                  
          if (conn != null) {
                      
          try {
                          conn.close();
                      } 
          catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
              }

              
          public List query(String sql) {
                  List list 
          = new ArrayList();

                  conn 
          = this.getConnection();
                  
          try {
                      stmt 
          = conn.createStatement();
                      rs 
          = stmt.executeQuery(sql);
                      
          // 獲取數(shù)據(jù)庫(kù)表結(jié)構(gòu)
                      ResultSetMetaData rsm = rs.getMetaData();
                      
          // 取得數(shù)據(jù)庫(kù)的列數(shù)
                      int col = rsm.getColumnCount();
                      
          // 生成col長(zhǎng)度的Object數(shù)組
                      Object[] obj = new Object[col];
                      
          // 遍歷結(jié)果集,將結(jié)果存入Object數(shù)組
                      while (rs.next()) {
                          
          for (int i = 0; i < col; i++) {
                              obj[i] 
          = rs.getObject(i + 1);
                          }
                          list.add(obj);
                      }
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                  } 
          finally {
                      
          this.close(rs, stmt, conn);
                  }
                  
          return list;
              }

              
          public void update(String sql) {
                  
          try {
                      conn 
          = this.getConnection();
                      stmt 
          = conn.createStatement();
                      stmt.executeUpdate(sql);
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                  }
          finally{
                      
          this.close(rs, stmt, conn);            
                  }
              }

              
          public static void main(String args[]) {
                  DBUtil nj 
          = new DBUtil();
                  String sql 
          = "select * from users";
                  List list 
          = nj.query(sql);
                  
          // 返回list的迭代器
                  Iterator it = list.iterator();
                  
          // 遍歷迭代器,取出結(jié)果
                  while (it.hasNext()) {
                      Object[] o 
          = (Object[]) it.next();
                      
          int id = ((BigDecimal) o[0]).intValue();
                      System.out.println(id);
                  }

              }
          }
          posted @ 2009-04-03 10:51 sclsch 閱讀(191) | 評(píng)論 (0)編輯 收藏

               摘要: 增加區(qū)域截取屏幕的功能,歡迎試用,簡(jiǎn)單實(shí)用。  閱讀全文
          posted @ 2009-01-27 21:45 sclsch 閱讀(1447) | 評(píng)論 (2)編輯 收藏

          個(gè)人開(kāi)發(fā)的小巧的屏幕截圖工具,可以全屏截圖,其他功能還待完善。
          下載地址/Files/sclsch/LittleScreenCapture.rar
          posted @ 2009-01-26 18:15 sclsch 閱讀(1216) | 評(píng)論 (0)編輯 收藏

              下拉框是網(wǎng)頁(yè)的重要元素,動(dòng)態(tài)取數(shù)據(jù)并不難,通常的思路是在action中取數(shù)據(jù),然后把數(shù)據(jù)放到request中,最后在頁(yè)面上用標(biāo)簽遍歷數(shù)據(jù),但寫(xiě)多了,是不是很煩,我想做一個(gè)通用的下拉框標(biāo)簽,只要指明了業(yè)務(wù)接口,并且該接口實(shí)現(xiàn)了特定方法,就可以了。
              首先定義一個(gè)接口,用來(lái)取下拉框的數(shù)據(jù)。
             
             1package com.ssh.tag;
             
          2.
             
          3import java.util.List;
             
          4.
             
          5/** 
             6.  * 
          @author 孫程亮 E-mail:sclsch@188.com 
             7.  * 
          @version 創(chuàng)建時(shí)間:Oct 27, 2008 6:59:05 PM
             8.  * 取得下拉框數(shù)據(jù)接口 
             9.  
          */
            
          10public interface SelectorInterface {
            
          11.   public List getVableValueList();
            
          12. }
             
              如果哪個(gè)業(yè)務(wù)層service需要增加下拉框的功能,就需要實(shí)現(xiàn)它。
          例如:
            
             1package com.ssh.entity.board.service;
             
          2.
             
          3import java.util.ArrayList;
             
          4import java.util.List;
             
          5.
             
          6import com.ssh.common.vo.ValueLabelBean;
             
          7import com.ssh.entity.board.dao.IBoardDao;
             
          8import com.ssh.entity.board.model.Board;
             
          9import com.ssh.tag.SelectorInterface;
            
          10import com.sun.java_cup.internal.internal_error;
            
          11.
            
          12/**
            13.  * 
          @author 孫程亮 E-mail:sclsch@188.com
            14.  * 
          @version 創(chuàng)建時(shí)間:Sep 4, 2008 6:36:22 PM
            15.  
          */
            
          16public class BoardServiceImpl implements IBoardService,SelectorInterface{
            
          17.     private IBoardDao boardDao;
            
          18.
            
          19.     public void addBoard(Board b) {
            
          20.        boardDao.addBorad(b);
            
          21.     }
            
          22.
            
          23.     public IBoardDao getBoardDao() {
            
          24.         return boardDao;
            
          25.     }
            
          26.
            
          27.     public void setBoardDao(IBoardDao boardDao) {
            
          28.         this.boardDao = boardDao;
            
          29.     }
            
          30.
            
          31.     public List getAllBoards() {
            
          32.         return this.boardDao.getAllBoards();
            
          33.     }
            
          34.     /**
            35.      * 用來(lái)實(shí)現(xiàn)下拉框的方法,
            36.      * 把下拉數(shù)據(jù)存放在ValuLabelBean中,再存放在list中返回
            37.      * 給自定義標(biāo)簽。
            38.      * 
          @return 下拉數(shù)據(jù)集合
            39.      
          */
            
          40.     public List getVableValueList() {
            
          41.         List list = this.boardDao.getAllBoards();
            
          42.         List valueLableList = new ArrayList();
            
          43.         for(int i=0;i<list.size();i++){
            
          44.           Board board = (Board)list.get(i);
            
          45.           ValueLabelBean vlb = new ValueLabelBean();
            
          46.           vlb.setValue(board.getId().toString());
            
          47.           vlb.setLabel(board.getName());
            
          48.           valueLableList.add(vlb);
            
          49.         }
            
          50.         return valueLableList;
            
          51.     }
            
          52. }
              注意數(shù)據(jù)必須放在ValueLabelBean中,label表示下拉框顯示的數(shù)據(jù),value表示下拉框的value值,下面是ValueLabelBean
          這個(gè)bean:
             1package com.ssh.common.vo;
             
          2.
             
          3import java.io.Serializable;
             
          4.
             
          5/**
             6.  * 
          @author 孫程亮 E-mail:sclsch@188.com
             7.  * 
          @version 創(chuàng)建時(shí)間:Oct 27, 2008 7:00:36 PM
             8.  
          */
             
          9public class ValueLabelBean implements Serializable {
            
          10.     private String value;
            
          11.     private String label;
            
          12.
            
          13.     public String getValue() {
            
          14.         return value;
            
          15.     }
            
          16.
            
          17.     public void setValue(String value) {
            
          18.         this.value = value;
            
          19.     }
            
          20.
            
          21.     public String getLabel() {
            
          22.         return label;
            
          23.     }
            
          24.
            
          25.     public void setLabel(String label) {
            
          26.         this.label = label;
            
          27.     }
            
          28. }

             下面就是寫(xiě)tag了,暫時(shí)設(shè)置了三個(gè)屬性 tagId,serviceBean和title,
          tagId:select 的 id 屬性值。
          serviceBean:對(duì)應(yīng)于spring容器中service的id。
          title:select的默認(rèn)選中項(xiàng)。
             1package com.ssh.tag;
             
          2.
             
          3import java.io.IOException;
             
          4import java.lang.reflect.Method;
             
          5import java.util.List;
             
          6.
             
          7import javax.servlet.jsp.JspException;
             
          8import javax.servlet.jsp.tagext.TagSupport;
             
          9.
            
          10import org.springframework.context.support.AbstractApplicationContext;
            
          11import org.springframework.util.StringUtils;
            
          12import org.springframework.web.context.WebApplicationContext;
            
          13import org.springframework.web.context.support.WebApplicationContextUtils;
            
          14import org.springframework.web.util.JavaScriptUtils;
            
          15import com.ssh.common.util.*;
            
          16import com.ssh.entity.board.service.IBoardService;
            
          17import com.sun.org.apache.xml.internal.utils.ObjectPool;
            
          18import com.ssh.common.vo.*;
            
          19import com.ssh.tag.*;
            
          20/**
            21.  * 
            22.  * 
          @author 孫程亮 E-mail:sclsch@188.com
            23.  * 
          @version 創(chuàng)建時(shí)間:Oct 25, 2008 10:22:18 AM
            24.  
          */
            
          25public class SelectorTag extends TagSupport {
            
          26.     
            
          27.     private String tagId;      //select's id
            28.     private String serviceBean;//service
            29.     private String title;      //select's title
            30.     
            
          31.     public int doEndTag() throws JspException {
            
          32.       WebApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
            
          33.       SelectorInterface selectorInterface = (SelectorInterface)applicationContext.getBean(serviceBean);
            
          34.       List list1 = selectorInterface.getVableValueList();
            
          35.       //List list = ServiceLocator.getSelectorService(serviceBean).getVableValueList();
            36.       StringBuffer sBuffer = new StringBuffer();
            
          37.       sBuffer.append("<select id='"+this.tagId);
            
          38.
            
          39.       sBuffer.append("'>");
            
          40.       if(!StringUtil.isBlank(title)){
            
          41.           sBuffer.append("<option value='-1' selected>"+title+"</option>");
            
          42.       }
            
          43.       for(int i=0;i<list1.size();i++){
            
          44.         ValueLabelBean vlb =  (ValueLabelBean)list1.get(i);
            
          45.         sBuffer.append("<option value='"+vlb.getValue()+"'>"+vlb.getLabel()+"</option>");
            
          46.       }
            
          47.       sBuffer.append("</select>");
            
          48.       try {
            
          49.         pageContext.getOut().println(sBuffer.toString());
            
          50.     } catch (IOException e) {
            
          51.         // TODO Auto-generated catch block
            52.         e.printStackTrace();
            
          53.     }
            
          54.       return EVAL_PAGE;
            
          55.     }
            
          56.     public void setTagId(String tagId) {
            
          57.         this.tagId = tagId;
            
          58.     }
            
          59.     public void setServiceBean(String serviceBean) {
            
          60.         this.serviceBean = serviceBean;
            
          61.     }
            
          62.     public void setTitle(String title) {
            
          63.         this.title = title;
            
          64.     }
            
          65. }

          在標(biāo)簽中可以用WebApplicationContextUtils來(lái)得到context,曾一度起了彎路,想到用一個(gè)工具類(lèi)加載容器,倒也能實(shí)現(xiàn),也想到用反射,但是行不通的。 看來(lái)變通一下,可能會(huì)少走很多彎路。
             下面是tld文件:
          <?xml version="1.0" encoding="UTF-8" ?>
           <!DOCTYPE taglib PUBLIC
               "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
               "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

           <taglib>

               <tlib-version>1.0</tlib-version>
               <jsp-version>1.0</jsp-version>
               <short-name>sclschTag</short-name>
               <description>sclschTag</description>

               <tag>
                   <name>selectorTag</name>
                   <tag-class>com.ssh.tag.SelectorTag</tag-class>
                   <body-content>JSP</body-content>
                   <description>
                   </description>
                   <attribute>
                       <name>tagId</name>
                       <required>true</required>
                       <rtexprvalue>true</rtexprvalue>
                   </attribute>

                   <attribute>
                       <name>serviceBean</name>
                       <required>true</required>
                       <rtexprvalue>true</rtexprvalue>
                   </attribute>
                   <attribute>
                       <name>title</name>
                       <required>false</required>
                       <rtexprvalue>true</rtexprvalue>
                   </attribute>
               </tag>

           </taglib>
          最后就剩頁(yè)面了:
            <%@ page language="java" contentType="text/html; charset=UTF-8"
               pageEncoding
          ="UTF-8"%>
           
          <%@ taglib uri="/WEB-INF/tld/selectorTag.tld" prefix="sclsch"%>
           
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           
          <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
           
          <head>
           
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           
          <title>mytag(sclsch@188.com)</title>
           
          </head>
           
          <body>
           
          <sclsch:selectorTag tagId='myid' title="--請(qǐng)選擇--" serviceBean="boardService" />
           
          </body>
           
          </html>

              好了,盡管這個(gè)tag很簡(jiǎn)陋,但為以后省了不少工,只要在業(yè)務(wù)層實(shí)現(xiàn)一個(gè)SelectorInterface接口,在頁(yè)面上擺個(gè)標(biāo)簽就可以了。我剛學(xué)標(biāo)簽的編寫(xiě),有什么不足請(qǐng)指正,如果有更好的設(shè)計(jì)一定告訴我額。


          posted @ 2008-10-28 20:57 sclsch 閱讀(748) | 評(píng)論 (0)編輯 收藏

          我是css初學(xué)者,自己做了一個(gè)工字型的布局,但整個(gè)頁(yè)面不居中。請(qǐng)指點(diǎn),代碼如下:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
          <HTML>
           
          <HEAD>
            
          <style type="text/css">

                * 
          {
              margin
          : 0px;
              padding
          : 0px;
              
          }
               body 
          {
              font-family
          : Arial, Helvetica, sans-serif;
              font-size
          : 12px;
              margin
          : 0px auto;
              height
          : auto;
              width
          : 760px;
              border
          :1px #66CCFF solid;
             
              
          }
              .header 
          {
              height
          : 100px;
              width
          : 760px;
              background-image
          : url(header.jpg);
              background-repeat
          : no-repeat;
              margin
          :0px 0px 3px 0px;
              border
          :1px #66CCFF solid;
               
              
          }

              .contentleft 
          {
              height
          : 250px;
              width
          : 150px;
              font-size
          : 14px;
              list-style-type
          : none;
              float
          :left;
              border
          :1px #66CCFF solid;

              
          }
              .contentleft li 
          {
              float
          :left;
              
          }
              .contentleft li a
          {
              color
          :#000000;
              text-decoration
          :none;
              padding-top
          :4px;
              display
          :block;
              width
          :97px;
              height
          :22px;
              text-align
          :center;
              background-color
          : #009966;
              margin-left
          :2px;
              
          }
              .contentleft li a:hover
          {
              border
          :1px #66CCFF solid;
              color
          :#FFFFFF;
              
          }
              .content 
          {
              height
          :auto;
              width
          : 760px;
              line-height
          : 1.5em;
              padding
          : 10px;
              border
          :1px #66CCFF solid;
              
          }
              .content p 
          {
              text-indent
          : 2em;
              
          }
              .content h3 
          {
              font-size
          : 16px;
              margin
          : 10px;
              
          }

              .footer 
          {
              height
          : 50px;
              width
          : 760px;
              line-height
          : 2em;
              text-align
          : center;
              background-color
          : #009966;
              padding
          : 10px;
              border
          :1px #66CCFF solid;
              
          }
              .contentright
          {
                  height
          : 250px;
                  width
          : 580px;
                  font-size
          : 14px;
                  list-style-type
          : none;
                  border
          :1px #66CCFF solid;
                  float
          :right;
              
          }
              .logo
          {
                background-image
          : url(scltemp.jpg);
                height
          : 100px;
                width
          : 75px;

              
          }

            
          </style>
             
            
          <TITLE> New Document </TITLE>
            
          <META NAME="Generator" CONTENT="EditPlus">
            
          <META NAME="Author" CONTENT="">
            
          <META NAME="Keywords" CONTENT="">
            
          <META NAME="Description" CONTENT="">
           
          </HEAD>

           
          <BODY>
          <div class="header">
            
          <div class="logo"></div>
          </div>

          <div class="content">
              
          <div class="contentleft">
              
          <li><href="#">首 頁(yè)</a></li>
              
          <li><href="#">文 章</a></li>
              
          <li><href="#">相冊(cè)</a></li>
              
          <li><href="#">Blog</a></li>
              
          <li><href="#">論 壇</a></li>
              
          <li><href="#">幫助</a></li>
              
          </div>
              
          <div class="contentright">
              
          <h3>前言</h3>
              
          <p>第一段內(nèi)容</p>
              
          <h3>理解CSS盒子模式</h3>
              
          <p>第二段內(nèi)容</p>
              
          </div>
          </div>

          <div class="footer">
          <p>關(guān)于華升 | 廣告服務(wù) | 華升招聘 | 客服中心 | Q Q留言 | 網(wǎng)站管理 | 會(huì)員登錄 | 購(gòu)物車(chē)</p><p>Copyright &copy;2006 - 2008 Tang
          Guohui. All Rights Reserved
          </p>
          </div>
           
          </BODY>
          </HTML>
          另外,還有很多不完美的地方,請(qǐng)指點(diǎn)一下!

          posted @ 2008-10-05 21:00 sclsch 閱讀(757) | 評(píng)論 (3)編輯 收藏

          主站蜘蛛池模板: 海宁市| 麟游县| 濉溪县| 昌黎县| 内乡县| 八宿县| 丹阳市| 通城县| 临夏县| 曲阳县| 枣强县| 长海县| 临泽县| 巩留县| 新营市| 吉林市| 大名县| 灵武市| 陕西省| 米泉市| 洪江市| 阳谷县| 九江市| 蒲城县| 保靖县| 柳河县| 炉霍县| 长岭县| 大方县| 定襄县| 武宣县| 宁蒗| 荆州市| 通道| 浦东新区| 涟源市| 巧家县| 弥渡县| 渭源县| 旅游| 若羌县|