sclsch

java备忘

BlogJava 首页 新随笔 联系 聚合 管理
  10 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks

2008年9月28日 #

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);
            
// 获取数据库表结构
            ResultSetMetaData rsm = rs.getMetaData();
            
// 取得数据库的列数
            int col = rsm.getColumnCount();
            
// 生成col长度的Object数组
            Object[] obj = new Object[col];
            
// 遍历结果集,将结果存入Object数组
            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();
        
// 遍历迭代器,取出结果
        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 阅读(166) | 评论 (0)编辑 收藏

     摘要: 增加区域截取屏幕的功能,欢迎试用,简单实用。  阅读全文
posted @ 2009-01-27 21:45 sclsch 阅读(1427) | 评论 (2)编辑 收藏

个人开发的小巧的屏幕截图工具,可以全屏截图,其他功能还待完善。
下载地址/Files/sclsch/LittleScreenCapture.rar
posted @ 2009-01-26 18:15 sclsch 阅读(1195) | 评论 (0)编辑 收藏

    下拉框是网页的重要元素,动态取数据并不难,通常的思路是在action中取数据,然后把数据放到request中,最后在页面上用标签遍历数据,但写多了,是不是很烦,我想做一个通用的下拉框标签,只要指明了业务接口,并且该接口实现了特定方法,就可以了。
    首先定义一个接口,用来取下拉框的数据。
   
   1package com.ssh.tag;
   
2.
   
3import java.util.List;
   
4.
   
5/** 
   6.  * 
@author 孙程亮 E-mail:sclsch@188.com 
   7.  * 
@version 创建时间:Oct 27, 2008 6:59:05 PM
   8.  * 取得下拉框数据接口 
   9.  
*/
  
10public interface SelectorInterface {
  
11.   public List getVableValueList();
  
12. }
   
    如果哪个业务层service需要增加下拉框的功能,就需要实现它。
例如:
  
   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 创建时间: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.      * 用来实现下拉框的方法,
  36.      * 把下拉数据存放在ValuLabelBean中,再存放在list中返回
  37.      * 给自定义标签。
  38.      * 
@return 下拉数据集合
  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. }
    注意数据必须放在ValueLabelBean中,label表示下拉框显示的数据,value表示下拉框的value值,下面是ValueLabelBean
这个bean:
   1package com.ssh.common.vo;
   
2.
   
3import java.io.Serializable;
   
4.
   
5/**
   6.  * 
@author 孙程亮 E-mail:sclsch@188.com
   7.  * 
@version 创建时间: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. }

   下面就是写tag了,暂时设置了三个属性 tagId,serviceBean和title,
tagId:select 的 id 属性值。
serviceBean:对应于spring容器中service的id。
title:select的默认选中项。
   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 创建时间: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. }

在标签中可以用WebApplicationContextUtils来得到context,曾一度起了弯路,想到用一个工具类加载容器,倒也能实现,也想到用反射,但是行不通的。 看来变通一下,可能会少走很多弯路。
   下面是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>
最后就剩页面了:
  <%@ 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="--请选择--" serviceBean="boardService" />
 
</body>
 
</html>

    好了,尽管这个tag很简陋,但为以后省了不少工,只要在业务层实现一个SelectorInterface接口,在页面上摆个标签就可以了。我刚学标签的编写,有什么不足请指正,如果有更好的设计一定告诉我额。


posted @ 2008-10-28 20:57 sclsch 阅读(731) | 评论 (0)编辑 收藏

我是css初学者,自己做了一个工字型的布局,但整个页面不居中。请指点,代码如下:
<!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="#">首 页</a></li>
    
<li><href="#">文 章</a></li>
    
<li><href="#">相册</a></li>
    
<li><href="#">Blog</a></li>
    
<li><href="#">论 坛</a></li>
    
<li><href="#">帮助</a></li>
    
</div>
    
<div class="contentright">
    
<h3>前言</h3>
    
<p>第一段内容</p>
    
<h3>理解CSS盒子模式</h3>
    
<p>第二段内容</p>
    
</div>
</div>

<div class="footer">
<p>关于华升 | 广告服务 | 华升招聘 | 客服中心 | Q Q留言 | 网站管理 | 会员登录 | 购物车</p><p>Copyright &copy;2006 - 2008 Tang
Guohui. All Rights Reserved
</p>
</div>
 
</BODY>
</HTML>
另外,还有很多不完美的地方,请指点一下!

posted @ 2008-10-05 21:00 sclsch 阅读(733) | 评论 (3)编辑 收藏

    最近要做一个树型下拉框,参考网上的资料,也没有找到合适的,于是自己实现了一个,原理是先顺序加载根节点,在加载每个根节点后,再遍历每个数组,看有没有以这个根节点为父 节点的数组,如果有,就加载到下拉框,然后再递归看有没有以这个节点为父结点的数组,如果有,再加载到下拉框,以此类推...
代码如下:(如果有更好的方法,请留言,有待改进)
   
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2   <html xmlns="http://www.w3.org/1999/xhtml">
 3    <head>
 4     <title> javascript树型菜单 author:sclsch@188.com</title>
 5     <meta name="generator" content="editplus" />
 6     <meta name="author" content="" />
 7     <meta name="keywords" content="" />
 8     <meta name="description" content="" />
 9     <script type="text/javascript">
10       var data =new Array();
11      
12       data[0]= {id:'0',pid:'1',text:'河北'};
13       data[1]= {id:'1',pid:'-1',text:'中国'};
14       data[2]= {id:'2',pid:'6',text:'莫斯科'};
15       data[3]= {id:'3',pid:'0',text:'邯郸'};
16       data[4]= {id:'4',pid:'0',text:'石家庄'};
17       data[5]= {id:'5',pid:'3',text:'邯郸县'};
18       data[6]= {id:'6',pid:'-1',text:'俄罗斯'};
19       data[7]= {id:'7',pid:'5',text:'孙程亮 sclsch@188.com'};
20 
21      function TreeSelector(item,data,rootId){
22         this._data = data;
23         this._item = item;
24         this._rootId = rootId;
25 
26      }
27      TreeSelector.prototype.createTree = function(){
28          var  len =this._data.length;
29          forvar i= 0;i<len;i++){
30            if ( this._data[i].pid == this._rootId){
31               this._item.options.add(new Option(".."+this._data[i].text,this._data[i].id));
32                   for(var j=0;j<len;j++){
33                     this.createSubOption(len,this._data[i],this._data[j]);
34                   
35                   }  
36            }
37         }
38      }
39       
40      TreeSelector.prototype.createSubOption = function(len,current,next){
41             var blank = "..";
42             if ( next.pid == current.id){
43                  intLevel =0;
44                  var intlvl =this.getLevel(this._data,this._rootId,current);
45                  for(a=0;a<intlvl;a++)
46                     blank += "..";
47                  blank += "├-";
48                  this._item.options.add(new Option(blank + next.text,next.id));
49                   
50                  for(var j=0;j<len;j++){
51                    this.createSubOption(len,next,this._data[j]);
52                  
53                  }   
54                  
55             }
56      }
57 
58      TreeSelector.prototype.getLevel = function(datasources,topId,currentitem){
59          
60          var pid =currentitem.pid;
61          if( pid !=topId)
62          {
63            for(var i =0 ;i<datasources.length;i++)
64            {
65               if( datasources[i].id == pid)
66               {
67                  intLevel ++;
68                  this.getLevel(datasources,topId,datasources[i]);
69               }
70            }  
71          }
72          return intLevel;
73     }
74 
75     </script>
76    </head>
77 
78    <body>
79     <select id="myselect">
80   </select>
81   <script language=javascript type="text/javascript">
82     var ts = new TreeSelector(document.getElementById("myselect"),data,-1);
83     ts.createTree();
84   </script>
85    </body>
86   </html>

posted @ 2008-09-28 11:20 sclsch 阅读(2317) | 评论 (0)编辑 收藏