Solved

h2 database issue in java

Posted on 2013-11-27
4
1,394 Views
Last Modified: 2013-12-12
Hi;

I write the following code to test a simple h2 database and table, but failed. Code is as follows:

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.Statement;  

public class H2Example {  
    public static void main(String[] args) {  
  
        Connection connection = null;  
        ResultSet resultSet = null;  
        Statement statement = null;  
  
        try {  
            Class.forName("org.h2.Driver");  
            connection = DriverManager.getConnection(  
                    "jdbc:h2:C:\\H2DB\\database\\EMPLOYEEDB", "sa", "");  
            statement = connection.createStatement();  
            resultSet = statement  
                    .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");  
            while (resultSet.next()) {  
                System.out.println("EMPLOYEE NAME:"  
                        + resultSet.getString("EMPNAME"));  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                resultSet.close();  
                statement.close();  
                connection.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}  

Open in new window


The latest jar is in buildpath in my eclipse. The path for db is as C:\H2DB\database\EMPLOYEEDB.h2

The error is as follows:

org.h2.jdbc.JdbcSQLException: Table "EMPLOYEEDETAILS" not found; SQL statement:
SELECT EMPNAME FROM EMPLOYEEDETAILS [42102-174]
	at org.h2.message.DbException.getJdbcSQLException(DbException.java:332)
	at org.h2.message.DbException.get(DbException.java:172)
	at org.h2.message.DbException.get(DbException.java:149)
	at org.h2.command.Parser.readTableOrView(Parser.java:4942)
	at org.h2.command.Parser.readTableFilter(Parser.java:1159)
	at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1766)
	at org.h2.command.Parser.parseSelectSimple(Parser.java:1874)
	at org.h2.command.Parser.parseSelectSub(Parser.java:1760)
	at org.h2.command.Parser.parseSelectUnion(Parser.java:1602)
	at org.h2.command.Parser.parseSelect(Parser.java:1590)
	at org.h2.command.Parser.parsePrepared(Parser.java:418)
	at org.h2.command.Parser.parse(Parser.java:290)
	at org.h2.command.Parser.parse(Parser.java:262)
	at org.h2.command.Parser.prepareCommand(Parser.java:227)
	at org.h2.engine.Session.prepareLocal(Session.java:437)
	at org.h2.engine.Session.prepareCommand(Session.java:380)
	at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138)
	at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:72)
	at H2Example.main(H2Example.java:23)
java.lang.NullPointerException
	at H2Example.main(H2Example.java:32)

Open in new window


The table is there as seen in the screenshot.

Regards.
h2.jpg
0
Comment
Question by:jazzIIIlove
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39680591
That image shows an entirely different path - not even a Windows OS
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 39680640
It looks like you might be missing the .h2 extension of your DB file in the URL on line 16 of your code, ie. it should be like this...
            connection = DriverManager.getConnection(  
                    "jdbc:h2:C:\\H2DB\\database\\EMPLOYEEDB.h2", "sa", "");

Open in new window

Also, to prevent the NullPointerException at the end of the error that you posted, you should check each resource for null when closing them in the finally block, such as...
         } finally {  
            try {  
                if (resultSet != null) {
                    resultSet.close();  
                }
                if (statement != null) {
                    statement.close();   
                }
                if (connection != null) {
                    connection.close();   
                }
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 39680744
Try instead:

connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");

Open in new window

As it looks like a Unix-path fits all kind of thing
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39713786
:)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
configure dependency in POM for new database 3 25
SYbase 4 31
Mongo DB 18 46
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question