半神半圣亦半仙,全儒全道是全闲,脑中真书藏万贯,掌握文武半边天。
Sep 2
    我是手动安装的Tomcat服务,网上的帖子提到把 JRE 里的msvcr71.dll文件拷贝到C:\windows\system32下面,可以解决这个问题,可惜我在JDK1.5和1.6中都没有找到那个msvcr71.dll,估计那个帖子只是针对JDK1.4的。

原因:将Tomcat安装服务后又动了JDK。

解决:开始以为是环境变量的问题,将JAVA_HOME和CATALINA_HOME配置到Path变量后,可以通过startup命令启动Tomcat,但以服务形式启动仍然报错,后来想起来前几天把JDK的路径改了一下(用的电脑是别人装的JDK,感觉路径别扭,就换了目录),查找注册表后发现在
HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat5\Parameters\Java下的Jvm项中为 D:\Blood\Java1\jdk1.5.0_11\jre\bin\server\jvm.dll,即移动前的JDK路径,将其修改为 D:\Java\jdk1.5.0_11\jre\bin\server\jvm.dll
后,问题解决。
造成这个错误的原因可能有多种,以上是我的解决方案,在CSDN上这个问题还被标记为[真正的高手难题],无满意答案而结帖,开心ing,嘿嘿。

另:今天又找到一个解决方法,应该比较通用。就是删除服务再重新安装。方法如下
用sc.exe这个Windows命令,“开始”——“运行”——“cmd”,然后输入  sc delete "服务名"  (如果服务名中间有空格,就需要前后加引号),比如我的是: sc delete Tomcat5。
这样,Tomcat服务在注册表中的信息就被清除了,最好再用优化大师之类的程序清理一下注册表。
下面开始重装Tomcat服务,cd到%CATALINA_HOME%\bin下,输入service install Tomcat5,再到服务管理界面,启动Tomcat服务,应该是正常的了。
Aug 17

用Timestamp来记录日期时间还是很方便的,但有时候显示的时候是不需要小数位后面的毫秒的,这样就需要在转换为String时重新定义格式。

      Timestamp转化为String:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
String str = df.format(now);
      String转化为Timestamp:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);
Date、String、Timestamp之间的转换!

Date 和String之间的转换main函数:
public static void main(String[] args) {
   // TODO Auto-generated method stub
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");        
Date date = null;   
String str = null;                 
            
// String转Date   
str = "2009-01-06";         
try {   
date = format.parse(str); // Wed sep 26 00:00:00 CST 2007   
} catch (ParseException e) {   
e.printStackTrace();   
}   
          
date = java.sql.Date.valueOf(str); // 只保留日期部分,返回的是java.sql.Date 2007-9-26
System.out.println(date);
// Date转String   
date = new Date();   // Wed sep 26 18 17:14:01 CST 2007      
str = format.format(date); // 2007-9-26   
             System.out.println(str);
format = DateFormat.getDateInstance(DateFormat.SHORT);   
str = format.format(date); // 07-9-26
System.out.println(str);
           
format = DateFormat.getDateInstance(DateFormat.MEDIUM);   
str = format.format(date); // 2007-9-26  
                 System.out.println(str);
      format = DateFormat.getDateInstance(DateFormat.FULL);   
        str = format.format(date); // 2007年9月26日 星期三
   System.out.println(str);
}

Timestamp和String之间转换的函数:
public static void main(String[] args) {
   // TODO Auto-generated method stub
   //Timestamp转化为String:
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
    Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
    String str = df.format(now);
    System.out.println(str);
   
         ///String转化为Timestamp:
          SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    String time = df1.format(date);
    Timestamp ts = Timestamp.valueOf(time);
    System.out.println(ts);

}

Aug 15
《jBPM 4视频教程》:基于jBPM 4.3,发布日期2010-3-5。
NO.分级标题
01《配置开发环境》:下载jBPM 4和eclipse,安装GPD流程设计器,设计简单流程。  下载   提意见
02《管理流程定义》:将流程定义发布到流程引擎中,实现查看和删除流程定义的功能。  下载   提意见
03《管理流程实例》:发起新流程,使暂停的流程继续运行,实现终止和删除流程实例。  下载   提意见
04《设计请假流程》:以请假审批流程为例,以web的方式发布新流程定义。  下载   提意见
05《发起新流程》:在流程定义中设置任务分配,提供接收和完成任务的功能。  下载   提意见
06《流程驳回》:实现流程驳回和重新提交申请的功能。  下载   提意见
07《组织机构》:介绍jBPM 4中默认提供的组织机构。  下载   提意见
08《监听事件》:为流程定义设置事件,监听流程执行时触发的事件。  下载   提意见
09《跟踪流程图》:显示流程图,跟踪当前路程实例的状态。  下载   提意见
Aug 15
    使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决:
    在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则 Hibernate抛session already closed Exception;    Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题.
    它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。
    Open Session In View在request把session绑定到当前thread期间一直保持 hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以 lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter 方法或Interceptor的postHandle方法自动关闭session。
 
OpenSessionInViewInterceptor配置
<beans>
<bean name="openSessionInViewInterceptor"
class
="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="urlMapping"
class
="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor"/>
</list>
</property>
<property name="mappings">
 ..
</property>
</bean>
 

 
</beans>
 
OpenSessionInViewFilter配置
<web-app>
 

 
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
 

 
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

 
</web-app>
 
很多人在使用OpenSessionInView过程中提及一个错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations
are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into
FlushMode.AUTO or remove 'readOnly' marker from transaction definition
看看OpenSessionInViewFilter里的几个方法
 
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,FilterChain filterChain)
throws ServletException, IOException {
 SessionFactory sessionFactory = lookupSessionFactory();
 logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
 Session session = getSession(sessionFactory);
 TransactionSynchronizationManager.bindResource(
  sessionFactory, new SessionHolder(session));
 try {
  filterChain.doFilter(request, response);
 }
 finally {
 TransactionSynchronizationManager.unbindResource(sessionFactory);
 logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
 closeSession(session, sessionFactory);
 }
}
 
 
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
 Session session = SessionFactoryUtils.getSession(sessionFactory, true);
 session.setFlushMode(FlushMode.NEVER);
 return session;
}

protected void closeSession(Session session, SessionFactory sessionFactory)
throws CleanupFailureDataAccessException {
 SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);
}
 
     可以看到OpenSessionInViewFilter在getSession的时候,会把获取回来的session的 flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该 sessionFactory的绑定,最后closeSessionIfNecessary根据该 session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的 transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。
 
public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory)
throws CleanupFailureDataAccessException {
 
if (session == null ||
TransactionSynchronizationManager.hasResource(sessionFactory)) {
return;
}
logger.debug("Closing Hibernate session");
try {
session.close();
}
catch (JDBCException ex) {
// SQLException underneath
throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException());
}catch (HibernateException ex) {
throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex);
}
}
 
    也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有 insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则 doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。
采用spring的事务声明,使方法受transaction控制
 
 
<bean id="baseTransaction"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract
="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
 
<bean id="userService" parent="baseTransaction">
 
<property name="target">
<bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
</property>
</bean>
 
对 于上例,则以save,add,update,remove开头的方法拥有可写的事务,如果当前有某个方法,如命名为importExcel(),则因没 有transaction而没有写权限,这时若方法内有insert,update,delete操作的话,则需要手动设置flush model 为Flush.AUTO,如
session.setFlushMode(FlushMode.AUTO);
session.save(user);
session.flush();
      尽 管Open Session In View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的 doFilterInternal方法代码,这个方法 实际上是被父类的doFilter调用的,因此,我们可以大约了解的 OpenSessionInViewFilter调用流程: request(请求)->open session并开始 transaction->controller->View(Jsp)->结束transaction 并 close session.
     一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这期间connection就一直被占用而不释 放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,另一方面可能是网速慢,服务器与用户间传输时 间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。
Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用。
Aug 14

得到ResultSet记录数的方法

ResultSet没有方法直接得到记录数,只有另想方法,下面我介绍一下我取记录数方法:

如果只要得到记录数,可以直接用sql语句的select count()得出来,但结果我既想得到记录数,同时也需要用到记录集的数据呢?那就要用到下面这种方法了。

Java代码

stat = conn.createStatement();
         改为
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

rs==stmt.executeQuery(sql);

  1. ResultSet rs;   
  2. rs.last(); //移到最后一行   
  3. int rowCount = rs.getRow(); //得到当前行号,也就是记录数   
  4. rs.beforeFirst(); //还要用到记录集,就把指针再移到初始化的位置  

在既要得到记录数,又需要用到记录集的时候,这种方法应该是最节省资源了。

stat = conn.createStatement();
         改为 stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 就可以了

分析: 异常出现于移动结果集的指针时,原因是在生成statement对象的时候提供的参数不同
无参数的那个方法使用的是默认参数,statement执行后得到的结果集类型为 ResultSet.TYPE_FORWARD_ONLY.这种类型的结果集只能通过rs.next();方法逐条读取,使用其他方法就会报异常. 如果想执行一些复杂的移动结果集指针的操作就要使用其他参数了
顺便简单介绍一下各个参数:

   1. ResultSet.TYPE_FORWARD_ONLY   (略)
   2. ResultSet.TYPE_SCROLL_INSENSITIVE 双向滚动,但不及时更新,就是如果数据库里的数据修改过,并不在ResultSet中反应出来。
   3. ResultSet.TYPE_SCROLL_SENSITIVE 双向滚动,并及时跟踪数据库里的更新,以便更改ResultSet中的数据。
   4. ResultSet.CONCUR_READ_ONLY 只读取ResultSet
   5. ResultSet.CONCUR_UPDATABLE 用ResultSet更新数据库

转载地址:http://blog.csdn.net/predictbird/archive/2008/01/12/2038888.aspx

返回ResultSet的字段个数?

使用rs.getMetaData()方法,既可以得到一系列的数据(表项的数量,类型等),这个方法的返回类型是ResultSetMetaData,在这个类里,你再调用getColumnCount()方法,既可以得到一个int的数据,这就是你要的字段的数量了
如下:

Java代码
  1. ResultSetMetaData    rsmd1=rs.getMetaData();     
  2.   int    cCount1=rsmd1.getColumnCount();     
分页: 1/60 第一页 1 2 3 4 5 6 7 8 9 10 下页 最后页 [ 显示模式: 摘要 | 列表 ]