• 内存数据库HSQL初学


    经常想写点简单的程序,或者演示系统,但是本地又没有数据库环境,

    采用内存数据库 HSQL 比较合适,以下程序用于内存数据库。

    ContextListener.java

     1 package com.xjh;
     2 
     3 import java.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 {
    10 
    11     public void contextInitialized(ServletContextEvent event) {
    12         try {
    13             System.out.println("********begin********");
    14             Class.forName("org.hsqldb.jdbcDriver");
    15             InputStreamReader in = new InputStreamReader(getClass()
    16                     .getClassLoader().getResourceAsStream("db.sql"));
    17             BufferedReader reader = new BufferedReader(in);
    18             DBUtil.setupDatabase(reader);
    19             System.out.println("********end********");
    20         } catch (ClassNotFoundException e) {
    21             e.printStackTrace();
    22         }
    23 
    24     }
    25 
    26     public void contextDestroyed(ServletContextEvent event) {
    27 
    28     }
    29 
    30 }

    DBUtil.java

     1 package com.xjh;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.sql.Connection;
     6 import java.sql.DriverManager;
     7 import java.sql.SQLException;
     8 import java.sql.Statement;
     9 
    10 public class DBUtil {
    11 
    12     public static void setupDatabase(BufferedReader reader) {
    13         Connection c = null;
    14         Statement stmt = null;
    15         try {
    16             c = openConnection();
    17             stmt = c.createStatement();
    18             String line = "";
    19             String readLine = "";
    20             while ((readLine = reader.readLine()) != null) {
    21                 line = line + readLine;
    22                 if (! readLine.endsWith(";")) {
    23                     continue;
    24                 } else {
    25                     stmt.execute(line);
    26                     line = "";
    27                 }                
    28             }
    29             stmt.close();
    30             c.close();
    31         } catch (IOException e) {
    32             e.printStackTrace();
    33         } catch (SQLException e) {
    34             e.printStackTrace();
    35         } finally {
    36             try {
    37                 stmt.close();
    38                 c.close();
    39             } catch (SQLException e) {
    40                 e.printStackTrace();
    41             }
    42         }
    43     }
    44 
    45     public static Connection openConnection() throws SQLException {
    46         Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:.", "sa",
    47                 "");
    48         return c;
    49     }
    50 
    51 }

    database.properties

    1 # hsql db set
    2 driver=org.hsqldb.jdbcDriver
    3 url=jdbc:hsqldb:mem:.
    4 username=sa
    5 password=

    db.sql

     1 create table orders (
     2       orderid int not null,
     3       userid varchar(80) not null,
     4       orderdate date not null,
     5       shipaddr1 varchar(80) not null,
     6       shipaddr2 varchar(80) null,
     7       shipcity varchar(80) not null,
     8       shipstate varchar(80) not null,
     9       shipzip varchar(20) not null,
    10       shipcountry varchar(20) not null,
    11       billaddr1 varchar(80) not null,
    12       billaddr2 varchar(80)  null,
    13       billcity varchar(80) not null,
    14       billstate varchar(80) not null,
    15       billzip varchar(20) not null,
    16       billcountry varchar(20) not null,
    17       courier varchar(80) not null,
    18       totalprice decimal(10,2) not null,
    19       billtofirstname varchar(80) not null,
    20       billtolastname varchar(80) not null,
    21       shiptofirstname varchar(80) not null,
    22       shiptolastname varchar(80) not null,
    23       creditcard varchar(80) not null,
    24       exprdate varchar(7) not null,
    25       cardtype varchar(80) not null,
    26       locale varchar(80) not null,
    27       constraint pk_orders primary key (orderid)
    28 );

    web.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>
    

      

  • 相关阅读:
    第一次作业
    第四次作业
    第五次作业《构建之法》心得体会
    第三次作业
    第二次作业
    第一次作业
    个人简介
    读《构建之法》有感
    第四次博客园作业(2)
    第四次博客园作业(1)
  • 原文地址:https://www.cnblogs.com/xiejh/p/6820154.html
Copyright © 2020-2023  润新知