HSQL基本使用(linux),安装+Demo

文章目录

  • 下载
  • 安装
  • 运行
  • 使用数据库
  • demo
  • 注意

下载

http://sourceforge.net/projects/hsqldb/files/

安装

将下载的包,解压到任意目录即可

运行

通过java命令启动

java -classpath /root/hsql/hsqldb-2.4.1/hsqldb/lib/hsqldb.jar org.hsqldb.Server -database.0 file:/home/hsqldbfile/jxima/hqdb -dbname.0 hqdb

-classpath表示启动需要依赖的jar包,指定到hsql安装目录下的对应地址
org.hsqldb.Server 表示server模式启动
-database.0 表示第一个数据库
file 表示数据库文件存储路径
-dbname.0 表示第一个数据库名称

[root@supervisordruid1 hsqldb-2.4.1]# java -classpath /root/hsql/hsqldb-2.4.1/hsqldb/lib/hsqldb.jar org.hsqldb.Server -database.0 file:/home/hsqldbfile/jxima/hqdb -dbname.0 hqdb
[Server@1fb3ebeb]: Startup sequence initiated from main() method
[Server@1fb3ebeb]: Could not load properties from file
[Server@1fb3ebeb]: Using cli/default properties only
[Server@1fb3ebeb]: Initiating startup sequence...
[Server@1fb3ebeb]: Server socket opened successfully in 11 ms.
[Server@1fb3ebeb]: Database [index=0, id=0, db=file:/home/hsqldbfile/jxima/hqdb, alias=hqdb] opened successfully in 841 ms.
[Server@1fb3ebeb]: Startup sequence completed in 856 ms.
[Server@1fb3ebeb]: 2019-04-08 06:37:26.042 HSQLDB server 2.4.1 is online on port 9001
[Server@1fb3ebeb]: To close normally, connect and execute SHUTDOWN SQL
[Server@1fb3ebeb]: From command line, use [Ctrl]+[C] to abort abruptly

使用数据库

项目中数据库配置文件配置

jdbc.url=jdbc:hsqldb:hsql://10.223.138.236(服务器IP)/hqdb (hqdb为数据库启动时配置的-dbname)
jdbc.username=sa
jdbc.password=

demo

import java.sql.*;

public class HsqlDemo {
	public static void main(String[] args) {
		Statement stmt = null;
		ResultSet rs = null;
		Connection connection = null;

		try {
			Class.forName("org.hsqldb.jdbcDriver");
//    		String URL="jdbc:hsqldb:hsql://10.0.1.21:5432/school";
			String URL="jdbc:hsqldb:hsql://localhost";
			//
			String USER="SA";
			String PASSWORD="";
//			connection = DriverManager.getConnection("jdbc:hsqldb:hsql://10.0.1.224:9001/", "SA", "");
			connection = DriverManager.getConnection("jdbc:hsqldb:hsql://10.0.3.42:9001/hqdb", "SA", "");

//            Connection c = DriverManager.getConnection(URL, USER, PASSWORD);
			if (connection != null) {
				System.out.println("Connected db success!");
//                String sql = "CREATE TABLE TBL_USERSSYXFA(ID INTEGER, NAME VARCHAR, BIRTHDAY DATE);";
				//增删改查
//                String sql = "CREATE TABLE TBL_USERSAARONAY(ID INTEGER, NAME INTEGER, BIRTHDAY DATE);";
//                sql = "INSERT INTO TBL_USERSAARONAY(ID, NAME, BIRTHDAY) VALUES ('2', '2', SYSDATE);";

				String sql1 = "CREATE TABLE TEST(NAME INTEGER,SEX INTEGER)";
				String sql2 = "INSERT INTO TEST VALUES('1','2')";
				String sql5 = "INSERT INTO TEST VALUES('3','4')";
				String sql3 = "select * from TEST";
				String sql4 = "DROP TABLE IF EXISTS TEST";
				stmt = connection.createStatement();
				stmt.execute(sql4);
				stmt.execute(sql1);
				stmt.execute(sql2);
				PreparedStatement prepareStatement1 = connection.prepareStatement(sql5);
				prepareStatement1.execute();
				PreparedStatement prepareStatement2 = connection.prepareStatement(sql3);
				rs  = prepareStatement2.executeQuery();
				while(rs.next()) {
					System.out.println(rs.getString("NAME")+"的性别是"+rs.getString("SEX"));
				}
//                stmt.executeUpdate(sql);
				if (stmt != null) {
					stmt.close();
				}
				connection.close();
			}
		} catch(Exception e) {
			System.out.println("ERROR:failed to load HSQLDB JDBC driver.");
			e.printStackTrace();
			return;
		}
	}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leesin</groupId>
    <artifactId>hsql_demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>hsql</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb-j5</artifactId>
        <version>2.2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
        <!-- https://mvnrepository.com/artifact/org.testifyproject.local-resources/hsql -->
        <!--<dependency>-->
            <!--<groupId>org.testifyproject.local-resources</groupId>-->
            <!--<artifactId>hsql</artifactId>-->
            <!--<version>1.0.2</version>-->
        <!--</dependency>-->

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <classesDirectory>${project.build.directory}/classes</classesDirectory>
                    <archive>

                        <manifest>

                            <addClasspath>true</addClasspath>

                            <classpathPrefix>lib/</classpathPrefix>

                            <mainClass>com.hlsq.HsqlDemo</mainClass>

                            <useUniqueVersions>false</useUniqueVersions>

                        </manifest>

                        <manifestEntries>

                            <Class-Path>${basedir}/src/lib/hsqldb.jar</Class-Path>

                        </manifestEntries>

                    </archive>

                </configuration>

            </plugin>


        </plugins>
    </build>

</project>

注意

server端(安装的hsql)和client端(java代码的 maven jar包)的版本可能会有不兼容

我这里server端用的2.2.4 client端用的 2.2.4

你可能感兴趣的:(#,Database,------,Other)