使用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-test jdbc-0.proxool.driver-url=jdbc:hsqldb:hsql://localhost/ jdbc-0.proxool.driver-class=org.hsqldb.jdbc.JDBCDriver jdbc-0.user=sa jdbc-0.password= jdbc-0.proxool.maximum-connection-count=10 jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE Properties配置
eclipse連接hsqldb數據庫,使用XML文件配置
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; //使用新建xml編寫配置 public class XmlProxool { public static void main(String[] args) { Connection conn = null; ResultSet rs = null; Statement stmt; try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); try { //InputStreamReader 實例一個讀取流 //XmlProxool.class.getResourceAsStream 讀取的是src下的文件 //使用XML配置,解析的時候就是用 JAXPConfigurator.configure()解析 //使用Properties配置,解析的時候用 PropertyConfigurator.configure() JAXPConfigurator.configure(new InputStreamReader(XmlProxool.class.getResourceAsStream("/proxool.xml")),false); try { conn = DriverManager.getConnection("proxool.xml-test"); } 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(); } } } XML文件配置系統信息同上,這裡也是放在Src文件下,代碼也是通過Src去找XML文件
<proxool-config> <proxool> <alias>xml-test</alias> <driver-url>jdbc:hsqldb:hsql://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> XML配置
使用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.XmlProxool; @WebServlet("/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.xml-test"); } 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.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>test_hsqldb</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>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> <!--解析xml信息 --> <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> <init-param> <param-name>xmlFile</param-name> <param-value>WEB-INF/proxool.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>A</servlet-name> <servlet-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.xml配置
3、訪問:http://localhost:8080/test_hsqldb/admin.j12,他會先去啟動hsqldb數據庫
訪問http://localhost:8080/test_hsqldb/admin,會出現以下這個,就是可以的!