当前位置:Gxlcms > 数据库问题 > 内存数据库HSQL初学

内存数据库HSQL初学

时间:2021-07-01 10:21:17 帮助过:34人阅读

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>

  

内存数据库HSQL初学

标签:data   otf   list   turn   ssl   stack   exti   prim   listen   

人气教程排行