日期:2014-05-16  浏览次数:20379 次

jsp连接h2例子

一、通过server形式连接

1.将h2.jar复制到web-inf/lib

2.配置web.xml



<listener>? 
??? <listener-class>org.h2.server.web.DbStarter</listener-class>? 
</listener> 
<!-- H2 DB Info -->  
<context-param>  
    <param-name>db.url</param-name>  
    <param-value>jdbc:h2:tcp://localhost/~/test</param-value>  
</context-param>  
<context-param>  
   <param-name>db.user</param-name>  
   <param-value>sa</param-value>  
</context-param>  
<context-param>  
   <param-name>db.password</param-name>  
   <param-value>sa</param-value>  
</context-param>  
<context-param>  
   <param-name>db.tcpServer</param-name>  
   <param-value>-tcpAllowOthers</param-value>  
</context-param> 

?3.jsp连接h2

String driver="org.h2.Driver"; 
String url="jdbc:h2:tcp://localhost/~/test;USER=sa;PASSWORD=sa"; 

Connection conn=null; //数据库连接 
Statement stmt=null; 
ResultSet rs = null; //查询结果 
Class.forName("org.h2.Driver");   
Connection con = DriverManager.getConnection(url);   
stmt=con.createStatement();   
rs = stmt.executeQuery("select * from  acct_group");   

?

?

二、通过file形式连接

这个就比较简单,将jar复制进lib,无需配置web.xml,直接在jsp内写入

String url="jdbc:h2:~/test;USER=sa;PASSWORD=sa";