Avatar of jazzIIIlove
jazzIIIlove
Flag for Sweden asked on

h2 database issue in java

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
JavaDatabases

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

That image shows an entirely different path - not even a Windows OS
SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

:)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy