Solved

h2 database issue in java

Posted on 2013-11-27
4
1,335 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Read about achieving the basic levels of HRIS security in the workplace.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now