2016-08-25 84 views
0

如何在WebApp項目中實現嵌入式數據庫?所以,爲了測試我使用Tomcat的項目。有沒有一種方法可以在Web應用程序中擁有「本地」/獨立數據庫?DynamicWeb項目中的嵌入式數據庫

我的本地數據庫(db.script):

SET DATABASE UNIQUE NAME HSQLDB56C1DBAB62 
SET DATABASE GC 0 
SET DATABASE DEFAULT RESULT MEMORY ROWS 0 
SET DATABASE EVENT LOG LEVEL 0 
SET DATABASE TRANSACTION CONTROL LOCKS 
SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED 
SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE 
SET DATABASE TEXT TABLE DEFAULTS '' 
SET DATABASE SQL NAMES FALSE 
SET DATABASE SQL REFERENCES FALSE 
SET DATABASE SQL SIZE TRUE 
SET DATABASE SQL TYPES FALSE 
SET DATABASE SQL TDC DELETE TRUE 
SET DATABASE SQL TDC UPDATE TRUE 
SET DATABASE SQL TRANSLATE TTI TYPES TRUE 
SET DATABASE SQL TRANSLATE TTI TYPES TRUE 
SET DATABASE SQL CONCAT NULLS TRUE 
SET DATABASE SQL UNIQUE NULLS TRUE 
SET DATABASE SQL CONVERT TRUNCATE TRUE 
SET DATABASE SQL AVG SCALE 0 
SET DATABASE SQL DOUBLE NAN TRUE 
SET FILES WRITE DELAY 500 MILLIS 
SET FILES BACKUP INCREMENT TRUE 
SET FILES CACHE SIZE 10000 
SET FILES CACHE ROWS 50000 
SET FILES SCALE 32 
SET FILES LOB SCALE 32 
SET FILES DEFRAG 0 
SET FILES NIO TRUE 
SET FILES NIO SIZE 256 
SET FILES LOG TRUE 
SET FILES LOG SIZE 50 
CREATE USER "Chris" PASSWORD DIGEST '4a7d1ed414474e4033ac29ccb8653d9b' 
ALTER USER "Chris" SET LOCAL TRUE 
CREATE SCHEMA PUBLIC AUTHORIZATION DBA 
SET SCHEMA PUBLIC 
CREATE MEMORY TABLE PUBLIC.CUSTOMER(ID VARCHAR(20)) 
ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1 
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC 
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC 
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC 
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC 
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC 
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC 
GRANT DBA TO "Chris" 
SET SCHEMA SYSTEM_LOBS 
INSERT INTO BLOCKS VALUES(0,2147483647,0) 
SET SCHEMA PUBLIC 
INSERT INTO CUSTOMER VALUES('CHRIS') 

這是我的Java類conenct到數據庫中(只是一個測試類):

package jdbc; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

/** 
* Dies Klasse liefert eine JDBC Connection zurück 
* @author Christian 
* 
*/ 
public class JDBCConnector { 

    private Connection connection; 

    public JDBCConnector(String url, String user, String password){ 
     try{ 
      Class.forName("org.hsqldb.jdbcDriver"); 
      connection = DriverManager.getConnection(url, user, password); 
      if(connection != null){ 
       System.out.println("Verbindung hergestellt"); 
      } 
     }catch (ClassNotFoundException e){ 
      System.err.println("Der Datenbanktreiber konnte nicht geladen werden"); 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      System.err.println("Beim Erstellen der Verbindung ist ein Feheler aufgetreten.\n" 
        + "Bitte überprüfen Sie die URL, sowie Usernamen und Passwort"); 
     } 
    } 

    public Connection getConnection() throws Exception{ 
     if(connection != null){ 
      return connection; 
     }else{ 
      throw new Exception("Connection == null"); 
     } 
    } 

    public String getFirstEntry(){ 
     try { 
      Statement stmt = connection.createStatement(); 
      ResultSet rs = stmt.executeQuery("SELECT * FROM CUSTOMER"); 
      String s = ""; 
      while(rs.next()){ 
       s = rs.getString(1); 
       System.out.println("s: " + s); 
      } 
      return s; 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return ""; 
    } 


} 

這裏是JSP文件,被稱爲連接到數據庫:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ page import="jdbc.JDBCConnector" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<% JDBCConnector connector = new JDBCConnector("jdbc:hsqldb:file:/Gefaehrdungsbeurteilung_Full/gefaehrdungsbeurteilung_full/server/db/db", "Chris", "0000"); 
    out.append(connector.getFirstEntry()); 
    %> 
</body> 
</html> 

如果我執行,我得到以下錯誤:

Verbindung hergestellt 
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: CUSTOMER 
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) 
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) 
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) 
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source) 
    at jdbc.JDBCConnector.getFirstEntry(JDBCConnector.java:45) 
    at org.apache.jsp.gefaehrdungsbeurteilung_005ffull.server.jsp.Access_jsp._jspService(Access_jsp.java:121) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: CUSTOMER 
    at org.hsqldb.error.Error.error(Unknown Source) 
    at org.hsqldb.error.Error.error(Unknown Source) 
    at org.hsqldb.ParserDQL.readTableName(Unknown Source) 
    at org.hsqldb.ParserDQL.readTableOrSubquery(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadTableReference(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadFromClause(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadTableExpression(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source) 
    at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source) 
    at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source) 
    at org.hsqldb.ParserCommand.compilePart(Unknown Source) 
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source) 
    at org.hsqldb.Session.executeDirectStatement(Unknown Source) 
    at org.hsqldb.Session.execute(Unknown Source) 
    ... 31 more 

的路徑必須是正確的,因爲當我添加http://localhost:8080/Gefaehrdungsbeurteilung_Full/gefaehrdungsbeurteilung_full/server/db/db.script我看到腳本文件。並在腳本文件中,您看到該客戶被聲明...

+0

Gefaehrdungsbeurteilung_Full/gefaehrdungsbeurteilung_full/server/db/db.script位於何處? –

+0

在webcontent下,然後選擇路徑gefaehrdungsbeurteilung_full/sever/db /。 – Chris

回答

-2

您應該看看H2。 它是一個可嵌入的小。

http://www.h2database.com/html/main.html

+0

好的,但是當我使用h2或hsqldb的嵌入模式時,訪問數據庫時總會出現奇怪的錯誤。這個錯誤,我沒有得到當我運行相同的類和數據庫作爲簡單的Java項目... – Chris

+0

所以,你請向我們展示_curios errors_? –

+0

我已將上面的代碼添加到 – Chris

0

您可以選擇Derby - 這是相同的堆棧。或SQLLite

0

jdbc:hsqldb:file:/ Gefaehrdun ...是以/開頭的絕對路徑。如果使用絕對路徑,則需要將數據庫置於Tomcat之外的確切位置。

如果您使用jdbc:hsqldb:file:Gefaehrdun ...(不以/開頭)它將是相對的,但我不確定它是否相對於WEB-INF/classes,tomcat/bin或其他。你需要做實驗。

+0

謝謝,我試驗了一下:) – Chris