Solved

h2 database issue in java

Posted on 2013-11-27
4
1,592 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 36

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

622 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