Start HSQLDB when the web application starts

Source: Internet
Author: User
Mport java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. statement;
Import javax. servlet. servletcontext;
Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;
/**
* @ Author zhoudong
* @ Created on 2005-2-1
* @ Email: eclipse@sina100.com
*/
Public class hsqldbservletcontextlistener implements servletcontextlistener {
/*
* (Non-javadoc)
*
* @ See javax. servlet. servletcontextlistener # contextinitialized (javax. servlet. servletcontextevent)
*/
Public void contextinitialized (servletcontextevent EVT ){
Try {
Servletcontext context = EVT. getservletcontext ();
String Params = "Port = 9001; silent = true; trace = false; database.0 ="
+ Context. getrealpath ("/") + "/WEB-INF/TM/HSQLDB/tmdb ";
Org. HSQLDB. Server = new org. HSQLDB. Server ();
Server. putpropertiesfromstring (Params );
Server. setlogwriter (null );
Server. seterrwriter (null );
Server. Start ();
} Catch (exception e ){
System. Err. println (E );
}
}
/*
* (Non-javadoc)
*
* @ See javax. servlet. servletcontextlistener # contextdestroyed (javax. servlet. servletcontextevent)
*/
Public void contextdestroyed (servletcontextevent arg0 ){
Connection conn = NULL;
Try {
Class. forname ("org. HSQLDB. jdbcdriver ");
Conn = drivermanager. getconnection ("JDBC: HSQLDB: hsql: // localhost: 9001 ",
"Sa ","");
Statement stmt = conn. createstatement ();
Stmt.exe cuteupdate ("shutdown ;");
Stmt. Close ();
} Catch (exception e ){
//
} Finally {
Try {
Conn. Close ();
} Catch (exception e ){
//
}
}
}
Public static void main (string [] ARGs ){
}
}

Add the listener to the Web. xml file of the project.

I. Introduction:
Hsql database is a free database written in pure Java. The license is the BSD-style protocol. If you are using Java programming, consider using it. Compared with other databases, it is small, only 563kb. Only one HSQLDB. jar file includes the database engine, database driver, and other user interface operations. In the Java open-source world, hsql is extremely popular (for Java itself). By default, JBoss application servers also provide this database engine. Because of its small size, it is a pure Java design and supports most of sql99 and sql2003 standards, so it is also a choice for commercial application display. Please download the hsql: http://prdownloads.sourceforge.net/hsqldb/hsqldb_1_7_3_3.zip? Download

Ii. Use hsql database:
1. The hsql database engine has several server modes: Common server mode, webserver mode, Servlet mode, standlone mode, and memory-only database.
2. The most common server mode:
1) First, switch to the Lib folder and run Java-cp hsqldb. Jar org. HSQLDB. Server-database.0 dB/mydb-dbname.0 XDB.
After the command is executed, a database mydb will be created in the DB folder. the alias (used to access the database) is XDB. If mydb database exists, it will be opened.
2) run the database interface operation tool: Java-cp hsqldb. Jar org. HSQLDB. util. databasemanager
Select the corresponding server mode in the type option. Here, select the hsql database engine server mode; the driver does not need to be modified; the URL is changed to JDBC: HSQLDB: hsql: // localhost/XDB (here, XDB is the alias we set above); set the user name in the user. During the first login, set the administrator user name and password to set the password. Click OK.
3) The first time you run the database engine, you have created the database. Now, you can open the DB folder and find several more files in it.
Mydb. properties file: it is an attribute file about the database.
Mydb. script: The main tables stored in hsql (here hsql stands for memory tables, which are the most commonly used). The formats in this table are all text formats and can be viewed in text, the statements are SQL statements. If you are familiar with SQL statements, you can manually modify them. Every time you run the database engine, it is loaded into the memory from here.
Mydb. LCK indicates that the database is open.
For other information, see the HSQLDB package manual.

3. the webserver mode is basically the same as the server running mode. It only supports HTTP and other protocols and is mainly used for firewalls. The default port is 9001. Start server, Java-cp hsqldb. Jar org. HSQLDB. webserver... and the rest are the same as above.

4. servlet mode allows you to access the database through the servlet container. Check the source code of hsqlservlet. Java, which is similar to webserver.

5. Another model worth thinking about is standalone: the database cannot be accessed through the network, which is mainly used in a JVM. In this way, the access speed will be faster. Although it is mentioned in the document that it is mainly used for development, we can assume that this method does not require an engine class, but returns a connection object similar to opening a file:
Connection c = drivermanager. getconnection ("JDBC: HSQLDB: file: mydb", "sa ","");
The mydb database files will be found in the current directory, and a connection object will be opened and returned. The advantage of this method is that you can operate data when necessary without using the engine. Therefore, this method is not especially required for databases, but you can use it if you need another method for database operations. This method can be used for those who don't want to spend extra money on the database engine. However, this method is not recommended. Remember that sessionfactory in hibernate can use opensession (connecttion C) to obtain a session object. Therefore, it can be used in testing or practical applications.

6. Memory-only Database: as the name suggests, it is mainly used in memory and is not used to save data. It can be used to exchange data in memory.

3. The specific links and operations are the same as common JDBC operations. In the corresponding server mode, the connection address is set by default when you run the database interface operation tool in the URL column. Practice on your own and compare the parameters.

This article introduces the general content in detail, but most of them are translated from the help of hsql. It is necessary for me to add some fresh blood.
What is lacking in this article is the lack of practical combat. Beginners often cannot take the first step, such as me.
The problem I encountered in practice was that I couldn't connect to the database even when I opened the Database Manager.
Java. SQL. sqlexception socket create Error
One night later, I woke up the next morning and thought that the database should be connected after the CMD window is created.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.