JavaDemo——Hsqldb内存数据库模式基本使用

一开始在eclipse里用maven引用hsqldb,结果发现Maven  Dependencies里是有jar包的,也有jdbcDriver.class,但代码里的Class.forName("org.hsqldb.jdbcDriver")就是找不到driver类,不知啥原因,查了一番好像是存在这样问题,最后只好去maven仓库里把jar拷贝过来,加到classpath里,不使用maven。(文章最后找到解决方案了)

项目结构:

JavaDemo——Hsqldb内存数据库模式基本使用_第1张图片

Demo:

/**
 * 2019年4月17日上午10:35:33
 */
package testhsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

/**
 * @author XWF
 *
 */
public class TestHsqldb {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
//			try {
//				Class.forName("org.hsqldb.jdbcDriver");
//			} catch (ClassNotFoundException e) {
//				e.printStackTrace();
//			}
			Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:sqldb", "root", "");
			Statement stat = conn.createStatement();
			//建表
			stat.executeUpdate("create table mytable(id integer, value VARCHAR(512), createtime TIMESTAMP default now())");
			
			//插入数据
			stat.executeUpdate("insert into mytable (id,value) values(1,'hello world'),(2,'java')");
			stat.executeUpdate("insert into mytable values(3,'abcd',now())");
			
			//查询
			int x = 3;
			PreparedStatement preparedStatement = conn.prepareStatement("select * from mytable where id = ?");
			preparedStatement.setInt(1, x);
			ResultSet resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				String s = resultSet.getInt(1)+">"+resultSet.getString(2)+">"+resultSet.getDate("createtime")+" "+resultSet.getTime("createtime");
				System.out.println(s);
			}
			
			//修改
			PreparedStatement preparedStatement2 = conn.prepareStatement("update mytable set value = ? where id = ?");
			preparedStatement2.setString(1, "hsqldb");
			preparedStatement2.setInt(2, 3);
			int updateResult = preparedStatement2.executeUpdate();
			System.out.println("update : "+updateResult);
			
			//查询列表
			PreparedStatement preState = conn.prepareStatement("select * from mytable");
			boolean result = preState.execute();
			if(result) {
				ResultSet resultset = preState.getResultSet();
				while(resultset.next()) {
					int id = resultset.getInt("id");
					String value = resultset.getString("value");
					Date d = resultset.getDate("createtime");
					String time = resultset.getTime("createtime").toString();
					System.out.println("id="+id+";val="+value+";date="+d+" time="+time);
				}
			}
			
			//删除
			PreparedStatement preState2 = conn.prepareStatement("delete from mytable where id = ?");
			preState2.setInt(1, x);
			int deleteResult = preState2.executeUpdate();
			System.out.println("delete : "+deleteResult);
			
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

结果:

JavaDemo——Hsqldb内存数据库模式基本使用_第2张图片

 

2019-04-22,在看Derby数据库的时候找到maven加载不到jdbcDriver.class的原因了。在maven里把test注释掉就可以了。

解决方案参考自:https://stackoverflow.com/questions/21262316/java-lang-classnotfoundexception-org-apache-derby-jdbc-embeddeddriver

你可能感兴趣的:(JavaDemos,hsqldb,内存数据库)