Java、Struts、Hibernate、Spring学习笔记
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
    使用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();     
Aug 13

以一个Servlet为例子说明如何从Tomcat 6的JNDI Resources中获取Data Source对象。
--------------------------------------------------------------------------
环境:
JDK 1.6
Tomcat 6
Eclipse Galileo 3.5.1
组件:
sqljdbc4.jar(可从microsoft.com下载,用于连接SQL Server)
参考:
http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html
--------------------------------------------------------------------------

1、准备library.
Copy sqljdbc4.jar到Tomcat 6的lib目录:$CATALINA_HOME/lib, 在Eclipse创建一个Java Project,引用$CATALINA_HOME/lib/servlet-api.jar和sqljdbc4.jar。

2、编写Servlet
编写一个名为JNDITest的Servlet类,代码如下:package com.Test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class JNDITest extends HttpServlet
{
    public void doGet(HttpServletRequest request
              ,HttpServletResponse response) throws IOException
    {
        PrintWriter pw=response.getWriter();
        try
        {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)
            envCtx.lookup("jdbc/DevDB");
            Connection conn = ds.getConnection();
            pw.write("Cool,Successful!</br>"+conn.toString()+"</br>");
            conn.close();
        }
        catch(SQLException exSql)
        {
            pw.write("SQL Exception:"+exSql.getMessage()+"</br>");
        }
        catch(NamingException exNam)
        {
            pw.write("Naming Exception:"+exNam.getMessage()+"</br>");
        }
        finally
        {
            pw.flush();
            pw.close();
            pw=null;
        }
    }
}.

  代码中创建了一个简单的Servlet,在Servlet的get方法中,通过InitialContext从JNDI的名为java:comp/env(Java Environment Naming Context)中查找名为jdbc/DevDB的Resource,然后把查找到的Resource转换为Data Source,然后从该Data Source中得到用于连接数据库的Connection对象,如果获取成功,则在Web页面中输出Connection的toString(),否则输出错误信息。
  若想从JNDI中查找到相应名称的Resource,则要事先在$CATALINA_HOME/conf/server.xml或META-INF/context.xml中进行配置。
  编译以上代码得到.class文件。

3、发布Servlet
  在$CATALINA_HOME/webapps(默认WEB ROOT)路径下创建名为jndi的文件夹,然后在jndi下创建WEB-INF文件夹,把编译后的.class文件按package的路径Copy到WEB-INF下的classes目录中,根据package配置,完整路径为$CATALINA_HOME/webapps/jndi/WEB-INF/classes/com/Test/JNDITest.class;在WEB-INF下创建名为web.xml的xml文件,内容如下:<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
      <servlet-name>JNDITest</servlet-name>
      <servlet-class>com.Test.JNDITest</servlet-class>
    </servlet>
    <servlet-mapping>
     <servlet-name>JNDITest</servlet-name>
     <url-pattern>/</url-pattern>
    </servlet-mapping>
   
    <resource-ref>
     <res-ref-name>jdbc/DevDB</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
    </resource-ref>
</web-app>  web.xml配置文件中映射了Servlet,并通过<resource-ref>节声明了我们要引用的Resource,实践证明在Tomcat6中可以不用在web.xml中声明<resource-ref>节。
  web.xml配置完成后,可以在浏览器中通过http://localhost:8080/jndi访问JNDITest Servlet,只不过还没有声明相应的资源,会看到相应的错误信息。

4、配置JNDI Resource
  Tomcat中有两个地方可以配置JNDI资源,一个是Web Application下的META-INF/context.xml文件,一个是$CATALINA_HOME/conf/server.xml文件。

  方法一:在context.xml文件中配置:
  在$CATALINA_HOME/jndi/META-INF目录下创建context.xml文件,内容如下:<?xml version="1.0" encoding="UTF-8"?>
<Context>
     <Resource name="jdbc/DevDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="testuser"
            password="testpass"
            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
            url="jdbc:sqlserver://193.168.1.3:41433;databaseName=DevDB"
            maxActive="8"
            maxIdle="4"/>
</Context>该文件中定义了一个名为jdbc/DevDB的Resource,并配置了相关的数据库连接属性及连接池信息。
  META-INF下的context.xml文件在运行时会被复制到$CATALINA_HOME/conf/[Engine name]/[Host name]/[Path name].xml文件,相对于本例就是:$CATALINA_HOME/conf/Catalina/localhost/jndi.xml。

  方法二:在server.xml文件中配置:
  也可以把JNDI Resources配置在$CATALINA_HOME/conf/server.xml文件中。配置在<Server>/<Service>/<Engine>/<Host>/<Context>节点下,需指定URL访问路径,如:...
      <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
       
        <Context path="/jndi" >
          <Resource name="jdbc/DevDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="testuser"
            password="testpass"
            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
            url="jdbc:sqlserver://192.168.1.3;databaseName=DevDB"
            maxActive="8"
            maxIdle="4"/>
        </Context>
       
      </Host>
    </Engine>
  </Service>
</Server>.

  JNDI Resource配置完成后,访问Servlet所在的URL,就可以看到想要输出的"Cool,Successful!..."等信息了,表明已成功从连接池中取到数据库连接。
  注:META-INF下的context.xml和server.xml中对应于同一web path的Context节对同一个属性最好不要同时配置,否则会产生意想不到的结果。

  把JNDI Resource配置为Global:
  除上面的两种配置方法外,还可以在$CATALINA_HOME/conf/server.xml中的<Server>/<GlobalNamingResources>节配置Resource为Global Source,然后在$CATALINA_HOME/conf/server.xml中的Context节或META-INF/context.xml文件中通过ResourceLink进行引用,如下,Global Resource Name为jdbc/DevDBGlobal:...
<Server>
...
  <GlobalNamingResources>
    <Resource name="jdbc/DevDBGlobal"
        auth="Container"
        type="javax.sql.DataSource"
        username="testuser"
        password="testpass"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://192.168.1.3,41433;databaseName=DevDB"
        maxActive="8"
        maxIdle="4"/>
  </GlobalNamingResources>
...  在server.xml中的引用:...
      <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
       
        <Context path="/jndi" >
          <ResourceLink name="jdbc/DevDB" global="jdbc/DevDBGlobal" type="javax.sql.DataSource" />
        </Context>
       
      </Host>
    </Engine>
  </Service>
</Server>  在context.xml中的引用:<?xml version="1.0" encoding="UTF-8"?>
<Context>
     <ResourceLink name="jdbc/DevDB" global="jdbc/DevDBGlobal" type="javax.sql.DataSource" />
</Context>

分页: 1/7 第一页 1 2 3 4 5 6 7 下页 最后页 [ 显示模式: 摘要 | 列表 ]