Often want to write a simple program, or demo system, but there is no database environment in the local,
The use of in-memory database HSQL is more appropriate, the following programs for in-memory database.
Contextlistener.java
1 packageCOM.XJH; 2 3 ImportJava.io.BufferedReader; 4 Import Java.io.InputStreamReader; 5 6 import javax.servlet.ServletContextEvent; 7 Import Javax.servlet.ServletContextListener; 8 9 public class Contextlistener implements Servletcontextlistener {ten public void contextinitialized (Serv Letcontextevent event) {Try {System.out.println ("********begin********" ), and Class.forName (" Org.hsqldb.jdbcDriver ""), inputstreamreader in = new InputStreamReader (GetClass (). getClassLoader (). getResourceAsStream ("Db.sql" )), BufferedReader reader = new BufferedReader (in); Dbutil.setupdatabase (reader), System.out.println ("********end********" ); catch ( ClassNotFoundException e) { e.printstacktrace (); }23 }25-public void contextdestroyed (Servletcontextevent event) {}29}
Dbutil.java
1 packageCOM.XJH; 2 3 ImportJava.io.BufferedReader; 4 ImportJava.io.IOException; 5 ImportJava.sql.Connection; 6 ImportJava.sql.DriverManager; 7 ImportJava.sql.SQLException; 8 ImportJava.sql.Statement; 9 public classDbutil {One-to-one public static voidSetupdatabase (BufferedReader reader) {Connection c = null; Statement stmt = null; try{c =OpenConnection (); stmt =C.createstatement (); String line = ""; String readLine = ""; + while ((ReadLine = Reader.readline ())! = NULL) {line = line + readline;22 if (! Readline.endswith (";" ) {Continue} else { Stmt.execute (line), line = "", "}29" stmt.close () ; C.close (); ( IOException e) { e.printstacktrace ();) Catch (SQLException e) {
e.printstacktrace (); n} finally
{$ try {PNS stmt.close (); sqlexcept c.close (); Ion e) { e.printstacktrace (); }42 }43}44 public static Connection OpenConnection () throws< c24> SQLException {Connection c = drivermanager.getconnection ("Jdbc:hsqldb:mem:.", "sa", "return<"); c27> c;49 }50
Database.properties
# Hsql DB set2 driver=org.hsqldb.jdbcDriver3 url=jdbc:hsqldb:mem:.4 username=sa5 password=
Db.sql
1CREATE TABLE orders (2 OrderID int NOT NULL, 3 userid varchar (NOT NULL), 4 orderdate date not NULL, 5 shipaddr1 varchar (+) NOT NULL, 6 SHIPADDR2 varchar (+) NULL, 7 shipcity varchar (+) NOT NULL, 8 shipstate varchar (+) NOT null , 9 shipzip varchar (a) NOT null , ShipCountry varchar (a) Not null,11 billaddr1 varchar (+) NOT null , BILLADDR2 varchar ($) NULL , billcity varchar (+) NOT null , 1 4 billstate varchar (+) NOT null , Billzip varchar (.) NOT null , Billcountry varchar (+) NOT null , 17 Courier varchar (+) NOT null , Totalprice decimal (10,2) is not null , Billtofirstname varchar (+) not Null
,20 billtolastname varchar (+) NOT null
, Shiptofirstname varchar (a) NOT null , Shiptolastname varchar ( Not null , CreditCard varchar (+) NOT null , Exprdate varchar (7) is not NULL , Cardtype varchar (80) Not null , and the $ locale varchar (n) is not null , constraint pk_orders primary key (OrderID);
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Web-app public "-//sun Microsystems, INC.//DTD Web application 2.2//en" "http://java.sun.com/dtd/ Web-app_2_3.dtd "><web-app><display-name>xiejiaohui</display-name><description> Online Fish Store Sample application</description> <listener><listener-class> Com.xjh.contextlistener</listener-class></listener></web-app>
Memory Database Hsql Beginner