h2错误提示:Table not found
来源:互联网 发布:国产羽绒服 知乎 编辑:程序博客网 时间:2024/05/16 14:22
问题
使用h2做内存数据库时,查询某表,程序提示table不存在。
引用h2版本
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.196</version></dependency>
启动数据库
public static void startH2Server() { try { System.out.println("正在启动h2数据库..."); Server.createTcpServer().start(); System.out.println("h2数据库启动成功..."); } catch (SQLException e) { System.out.println("启动h2数据库出错:" + e.toString()); e.printStackTrace(); throw new RuntimeException(e); }}
初始化数据库
private static final String JDBC_URL = "jdbc:h2:tcp://localhost/mem:gacl";private static final String USER = "gacl";//用户名private static final String PASSWORD = "123";//密码public static void initTables() { Connection conn = null; Statement stmt = null; try { String sql = "......";//初始化数据SQL Class.forName(DRIVER_CLASS); conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD); stmt = conn.createStatement(); stmt.execute(sql); sql = "SELECT COUNT(1) FROM xxxxxx"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.print(" count:%s", rs.getInt(1)); } System.out.println("初始化H2数据库数据完成!"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != stmt) { stmt.close(); } if (null != conn) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } }}
查询数据库
public static List<Object> query(Map<String, String> params) { Connection conn = null; Statement stmt = null; List<Object> list = new ArrayList<Object>(); try { Class.forName(DRIVER_CLASS); conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD); stmt = conn.createStatement(); String sql = "SELECT ......";//查询SQL ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { //处理rs } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != stmt) { stmt.close(); } if (null != conn) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } return list;}
但是在使用此查询时,总是提示查询的table不存在。
org.h2.jdbc.JdbcSQLException: Table “xxxxxx” not found; SQL statement:…….
明明在初始化数据库时,使用count方法能查询到值,怎么单独查询,却又找不到table了呢!
首先怀疑是SQL拼写错误,将查询SQL,放入“初始化方法”中执行,也可以出查询结果,说明查询用的SQL是没有写错。
但是单独执行时,一直提示找不到table,这就真奇怪了~
解决
在官方网站:http://www.h2database.com/html/features.html#in_memory_databases找到这么一段话,感觉有了新的希望。
By default, closing the last connection to a database closes the database. For an in-memory database, this means the content is lost. To keep the database open, add ;DB_CLOSE_DELAY=-1 to the database URL. To keep the content of an in-memory database as long as the virtual machine is alive,use jdbc:h2:mem:test;DB_CLOSE_DELAY=-1.
大意就是关闭最后一个conn时,就关闭了database。如果要保持database 打开可用,需要的数据库URL中添加参数:;DB_CLOSE_DELAY=-1。
看来是因为在数据库初始化的finally中将conn关闭,导致关闭了database。所以再执行单独的查询SQL时,因为database已经关闭,所以才会提示找不到table。
将JDBC_URL修改为:
private static final String JDBC_URL = "jdbc:h2:tcp://localhost/mem:gacl;DB_CLOSE_DELAY=-1";
问题解决。
阅读全文
0 0
- h2错误提示:Table not found
- Caused by: org.h2.jdbc.JdbcSQLException: Table "T_STUDENT_INFO" not found; SQL statement
- Error querying database. Cause:org.h2.jdbc.JdbcSQLException:Table “t_user" not found;SQL statement
- 提示错误:arm-linux-gcc: Command not found
- Android Studio 提示错误 default activity not found
- Linux shell脚本执行提示错误[[: not found
- 提示错误:arm-linux-gcc: Command not found
- 提示错误:arm-linux-gcc: Command not found
- 在容器内运行Golang binary提示not found错误
- Nuttx-7.18编译时提示“cygpath: Command not found”错误
- STM32技巧: Keil错误提示“ File Not Found”
- automake --add-missing时错误提示“ltmain.sh' not found”
- 提示错误:arm-linux-gcc: Command not found
- sqlite3 提示 not found
- Django not found错误
- Django not found错误
- Source not found错误
- redhat提示command not found
- 服务端指南 | HTTPS 项目实战指南
- ionic浏览器运行
- 在应用中直接打开QQ聊天
- 【重大更新】DevExpress v17.1新版亮点(XAF篇)
- Html中文字过多,单行超出和多行超出显示省略号
- h2错误提示:Table not found
- 子进程的异步等待方式
- AES加密
- 常用正则表达式
- SignalR推送服务
- 【转】【WPF】IvalueConverter和TypeConverter
- html和jQuery常用的选择器
- 基于Retrofit2+OkHttp封装的Android网络操作库RetrofitClient
- fragment 理解