Solved

h2 database issue in java

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
in SAS how to create a record for each combination of the two duplicates? 16 104
ejb mdb examples 1 8
corrupt Databases 9 65
Comparison query - 4 columns 9 24
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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