你的位置:首页 > Java教程

[Java教程]proxool+hsqldb


使用hsqldb数据库,开发环境eclipse,jdk8.0

Dsqldb数据库

1、下载包:hsqldb.jar

2、新建项目,复制到项目WebContent -> WEB -> INF -> lib下面,右键选择 Build Path  ->  Add to Build Path

3、在项目中找到Referenced Libraries点击,选择hsqldb.jar -> org.hsqldb.server -> Server.class -> Server 右键 选择 Run as -> 1 JAVA Application 启动项目

4、选择hsqldb.jar -> org.hsqldb.util -> DatabaseManagerSwing.class -> DatabaseManagerSwing 右键 选择 Run as -> JAVA Application 启动数据库

5、之后会弹出窗口,在第二个下拉框中 hsqldb database server ,点击OK登录(其他不需做改变),这里是指以hsqldb的客户端登录

Proxool

1、下载:官方网址:http://proxool.sourceforge.net/servlets.html (包:proxool-0.9.1.jar下载解压缩,之后会在lib有两个包,在jarjar有两个包,导入到项目lib中(包为:jarjar-0.6.jar、proxool-0.9.1.jar、proxool-cglib.jar、cglib-nodep-2.1_3.jar));

在之后的测试代码中,还需下载commons-dbcp-1.2.2.jar、commons-io-1.3.2.jar、commons-lang-2.4.jar、commons-logging-1.0.4.jar、commons-logging-api-1.1.jar、commons-pool-1.4.jar(commons包)

测试代码:

eclipse连接dsqldb数据库

package com.hsqldb;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;//使用hsqldb数据库连接public class hsqlconfig {  public static void main(String[] args) {    Connection conn = null;    ResultSet rs = null;    try {      //选择dsqldb.jar -> org.hsqldb.jdbc -> JDBCDriver.class -> JDBCDriver右键选择Copy Qualified Name       //粘贴到这里。这里的意思就是告诉eclipse说你去给我加载dsqldb的数据库驱动      Class.forName("org.hsqldb.jdbc.JDBCDriver");       //选择dsqldb.jar -> org.hsqldb.util -> DatabaseManagerSwing.class -> DatabaseManagerSwing右键选择Copy Qualified Name      //之后会弹出dsqldb客户端登录窗体,选择 URL 复制 jdbc:hsqldb:hsql://localhost/      //默认dsqldb账号为sa,密码为空;      conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/","SA","");      Statement stmt = conn.createStatement();      rs = stmt.executeQuery("select count(*) as ant from userinfo");      if(rs!=null){        while(rs.next()){          System.out.println(rs.getInt("ant"));        }      }    } catch (ClassNotFoundException e) {      e.printStackTrace();    } catch (SQLException e) {      e.printStackTrace();    }  }}

dsqldb

 

 

eclipse连接hsqldb数据库,使用JAVA配置

package com.proxool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;//直接java编写配置public class JavaProxool {  public static void main(String[] args) {    //Properties 配置文件,用户存放系统配置信息等    Properties info = new Properties();    info.setProperty("proxool.maximum-connection-count", "20");    info.setProperty("proxool.house-keeping-test-sql", "select CURRENT_DATE");    info.setProperty("user", "sa");    info.setProperty("password", "");    String alias = "test";    String driverClass = "org.hsqldb.jdbc.JDBCDriver";    String driverUrl = "jdbc:hsqldb:hsql://localhost/";    String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl;        try {      //加载驱动包(proxool的驱动包)      Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");    } catch (ClassNotFoundException e1) {      // TODO Auto-generated catch block      e1.printStackTrace();    }    Connection conn = null;    ResultSet rs = null;    try {      //执行连接过程,其中url是完整的路径,info是配置信息      conn = DriverManager.getConnection(url, info);      //声明一个可执行sql语句的对象      Statement stmt = conn.createStatement();      rs = stmt.executeQuery("select count(*) as ant from userinfo");      if(rs!=null){        while(rs.next()){          System.out.println(rs.getInt("ant"));        }      }    } catch (SQLException e) {      e.printStackTrace();    }  }}

Java配置

 

eclipse使用properties连接hsqldb数据库

package com.proxool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;//使用proxool透明模式连接数据库public class proxoolconfig {  public static void main(String[] args) {    Connection conn = null;    ResultSet rs = null;    try {      Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");      //proxool.example 是别名      //org.hsqldb.jdbc.JDBCDriver 是数据库的名称      //jdbc:hsqldb:hsql://localhost/ 是url地址      conn = DriverManager.getConnection("proxool.example:org.hsqldb.jdbc.JDBCDriver:jdbc:hsqldb:hsql://localhost/");      Statement stmt = conn.createStatement();      rs = stmt.executeQuery("select count(*) as ant from userinfo");      if(rs!=null){        while(rs.next()){          System.out.println(rs.getInt("ant"));        }      }    } catch (ClassNotFoundException e) {      e.printStackTrace();    } catch (SQLException e) {      e.printStackTrace();    }finally{      try {        conn.close();      } catch (SQLException e) {        e.printStackTrace();      }    }  }}

使用Properties连接hsqldb

 

eclipse连接hsqldb数据库,使用properties文件配置

package com.proxool;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;import org.logicalcobwebs.proxool.ProxoolException;import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;//使用Properties配置文件信息public class Properties_Proxool {  public static void main(String[] args) {    Connection conn = null;    ResultSet rs = null;    Properties info = new Properties();      try {        //Properties_Proxool.class.getResourceAsStream是读取Src下的文件信息        info.load(Properties_Proxool.class.getResourceAsStream("/proxool.properties"));        //读取使用Java的特性文件编写的配置文件。        PropertyConfigurator.configure(info);      } catch (IOException e) {        e.printStackTrace();      }catch (ProxoolException e) {        e.printStackTrace();      }    try {      //加载驱动包      Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");      conn = DriverManager.getConnection("proxool.property-test");      Statement stmt = conn.createStatement();      rs = stmt.executeQuery("select count(*) as ant from userinfo");      if(rs!=null){        while(rs.next()){          System.out.println(rs.getInt("ant"));        }      }    } catch (ClassNotFoundException e) {      e.printStackTrace();    } catch (SQLException e) {      e.printStackTrace();    }    }}

Properties文件配置系统信息

记住使用properties配置,必须新建properties文件,同时放在准确的地方,这里是放在Src下,同时代码也是通过Src去找这个文件

jdbc-0.proxool.alias=property-testjdbc-0.proxool.driver-url=jdbc:hsqldb:hsql://localhost/jdbc-0.proxool.driver-class=org.hsqldb.jdbc.JDBCDriverjdbc-0.user=sajdbc-0.password=jdbc-0.proxool.maximum-connection-count=10jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE

Properties配置

 

eclipse连接hsqldb数据库,使用

package com.proxool;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.FileInputStream;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;import org.logicalcobwebs.proxool.ProxoolException;import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;//使用新建public class public static void main(String[] args) {    Connection conn = null;    ResultSet rs = null;    Statement stmt;      try {        Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");        try {          //InputStreamReader 实例一个读取流          ////使用//使用Properties配置,解析的时候用 PropertyConfigurator.configure()          JAXPConfigurator.configure(new InputStreamReader(class.getResourceAsStream("/proxool.false);          try {            conn = DriverManager.getConnection("proxool.);          } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();          }        } catch (ProxoolException e) {                    e.printStackTrace();        }        try {          stmt = conn.createStatement();          System.out.println(stmt);          rs = stmt.executeQuery("select count(*) as ant from userinfo");          if(rs!=null){            while(rs.next()){              System.out.println(rs.getInt("ant"));            }          }        } catch (SQLException e) {          // TODO Auto-generated catch block          e.printStackTrace();        }      } catch (ClassNotFoundException e1) {        e1.printStackTrace();      }    }  }


同上,这里也是放在Src文件下,代码也是通过Src去找

<proxool-config>  <proxool>    <alias>//localhost/</driver-url>    <driver-class>org.hsqldb.jdbc.JDBCDriver</driver-class>    <!-- <statistics>1m,15m,1d</statistics> -->    <driver-properties>     <property name="user" value="SA"/>     <property name="password" value=""/>    </driver-properties>    <maximum-connection-count>10</maximum-connection-count>    <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>  </proxool></proxool-config>


 

使用Tomcat访问网页,将该数据库的信息加载出来(Tomcat 8.0)

1、新建Servlet,编写代码

package com.proxool.servlet;import java.io.IOException;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.logicalcobwebs.proxool.ProxoolException;import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;import com.proxool.Properties_Proxool;import com.proxool."/helloproxool")public class helloproxool extends HttpServlet {  private static final long serialVersionUID = 1L;  public helloproxool() {    super();  }  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    Connection conn = null;    ResultSet rs = null;    Statement stmt;      try {        Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");        try {          try {            conn = DriverManager.getConnection("proxool.);          } catch (SQLException e) {                        e.printStackTrace();          }        } catch (Exception e) {          e.printStackTrace();        }        try {          stmt = conn.createStatement();          System.out.println(stmt);          rs = stmt.executeQuery("select count(*) as ant from userinfo");          if(rs!=null){            while(rs.next()){              System.out.println(rs.getInt("ant"));            }          }        } catch (SQLException e) {                    e.printStackTrace();        }      } catch (ClassNotFoundException e1) {        e1.printStackTrace();      }    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    // TODO Auto-generated method stub    doGet(request, response);  }}

Servlet代码

 

2、通过配置Web.

<?default.html</welcome-file>  <welcome-file>default.htm</welcome-file>  <welcome-file>default.jsp</welcome-file> </welcome-file-list>   <servlet> <servlet-name>proxool</servlet-name> <!--这是proxool自带的Servlet类 --> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>  </servlet><servlet-mapping> <servlet-name>proxool</servlet-name> <!--通过访问admin,访问AdminServlet --> <url-pattern>/admin</url-pattern></servlet-mapping><servlet> <servlet-name>ServletConfigurator</servlet-name> <!--解析class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> <init-param>  <param-name>class>com.proxool.servlet.helloproxool</servlet-class> </servlet>  <servlet-mapping>    <servlet-name>A</servlet-name>    <url-pattern>*.j12</url-pattern>  </servlet-mapping></web-app>

Web.

 

3、访问:http://localhost:8080/test_hsqldb/admin.j12,他会先去启动hsqldb数据库

访问http://localhost:8080/test_hsqldb/admin,会出现以下这个,就是可以的!