Specified JDBC Driver: org.h2.jdbc - initialization failed - jdbc

Can't connect to database.
I also added the latest h2-1.4.178.jar to classpath, create table, check connection through tools.
It's working, but Tomcat said: "HTTP Status 500 - com.vaadin.server.ServiceException: java.lang.RuntimeException: Specified JDBC Driver: org.h2.jdbc - initialization failed."
How can I call it? I use Eclipse. This is my src (I use Vaadin):
try {
JDBCConnectionPool pool = new SimpleJDBCConnectionPool(
"org.h2.jdbc",
"jdbc:h2:~test", "SA", "", 2, 5);
screen.polulate("TEST", pool);
} catch (SQLException e) {
throw new RuntimeException(e);
}
I found my problem (https://superuser.com/questions/290999/where-can-i-find-h2-jdbc-driver), but i cant understand what does it mean:
And then call
Class.forName("org.h2.Driver");

The answer you link to is not really relevant to your problem. The problem probably (educated guess) is that the first parameter of the constructor of SimpleJDBCConnectionPool expects the JDBC driver name, and "org.h2.jdbc" is not the JDBC driver name (it's "org.h2.Driver").
Using Class.forName("org.h2.Driver"); is no longer necessary for modern JDBC drivers. With old drivers (that don't include a file META-INF/services/java.sql.Driver with its driver implementation(s)) using Class.forName("org.h2.Driver"); will load the driver and then the driver will register itself with java.sql.DriverManager so it can be used to create connections.

Related

JDBC DatabaseMetaData method not implemented by JDBC(T4SQLMX) driver

I am setting up a Spring-boot application to connect to HP NonStop Tandem's SQL/MX. First I achieved this connection by hard-coding the jdbc parameters like dataSource, URL, etc in the service section of the application and it worked (I was able to access tables by executing query).
Now I am trying to remove the hard coded part and have my database related info in application.properties file, but now I am getting the following error
org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver; nested exception is java.lang.AbstractMethodError: Method com/tandem/t4jdbc/SQLMXConnection.isValid(I)Z is abstract
Can someone help me understand the root cause? The same driver jar is being used when hard-coding the datasource details and it worked but not working when having the data source properties in application.properties and needs an upgrade to the jar.
I encountered the same exception when using Spring Data JPA in a Spring Boot application, the JTDS driver and the Hikari connection pool. In my case I discovered that the following fixed the problem:
Examining the class com.zaxxer.hikari.pool.PoolBase, the following can be observed:
this.isUseJdbc4Validation = config.getConnectionTestQuery() == null;
Thus JDBC 4 validation will not be attempted if there is a connection test query configured. In a Spring Boot application, this can be accomplished like this:
spring.datasource.hikari.connection-test-query=select 1;
Regretfully I do not have any experience with the T4SQLMX driver but nevertheless hope this can be of some use.
I recently fought through the same issue, for me I was using a JDBC type 3 driver; but my spring implementation only supported a type 4 driver, thus when the method you linked above was attempted to be called, it caused the error.
I suggest you look for a type 4 driver for your particular database and see if that resolves your issue.

Connection properties along with JDBC URL for oracle thin driver

How to supply DB connection properties along with JDBC URL when we used Oracle thin driver
Oracle has a very good documentation on that point.
It boils down to:
Create a Properties file, add the connection properties you need and add them into your call to
getConnection(String URL, Properties info);
Given your comment, you could try the following - but I could find no documentation that this connection property is actually available on the thin driver. This document that points to that driver suggests it's part of the deprecated weblogic oracle driver.
Properties p = new Properties();
p.setProperty ("user", youruser);
p.setProperty ("password", yourpass);
p.setProperty("EnableCancelTimeout", "true");
Connection con = DriverManager.getConnection(jdbc:oracle:thin:#<host>:1521:<SID>), p);

CQ DataSourcePool java.lang.IllegalStateException: Pool not open

I have configured Adobe CQ to use connection pooling to handle connections with MySql database. Everything works fine, but after a few hours I see this exception while trying to connect to database.
Caused by: java.lang.IllegalStateException: Pool not open
at org.apache.commons.pool.BaseObjectPool.assertOpen(BaseObjectPool.java:99)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:917)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:96)
I have seen such errors being reported in several instances/applications. I wanted to know if there is something wrong with the way this works on CQ.
I am using CQ5.6.1 and below is the code snippet I use to get a connection
#Reference
private DataSourcePool dataSourceService;
DataSource ds;
Connection connection;
try{
ds = (DataSource) dataSourceService.getDataSource("datasource_name);
connection = ds.getConnection();//this line causes the exception
catch(Exception e){
e.printStackTrace();
}finally{ connection.close(); }
This is my datasource configuration
Is there anything that I am missing?
Thanks!
I had the similar problem. You need to double check your configurations. Check in OSGI, check on your code every little detail for your database settings. These are the two things to look at. Then try to install the bundle/package again.

JDBC connection driver issue in eclipse

I am working on jdbc connection and I am using eclipse. I have placed connection driver that is mysql-connector-java-5.1.6.jar file in WebContent/WEB-INF/lib folder. After that I am writing this code to simply create and test connection between application and driver
import java.lang.ClassNotFoundException;
public class implementation {
public static void main(String[]arg)
{
try
{
System.out.println("conneting to driver...");
Class.forName("com.mysql.jdbc.driver");
System.out.println("Connection Successful");
}
catch(ClassNotFoundException error)
{
System.out.println("Error:" + error.getMessage());
}
}
}
when I am running this program, I am getting this error.
connecting to driver.
Error:com.mysql.jdbc.driver
can you please help to solve this issue. thank you for giving me your important time.
You are getting ClassNotFoundException because the correct driver class name is com.mysql.jdbc.Driver and not com.mysql.jdbc.driver.
The 'D' of Driver is capital(standard Camel Case notation)
Hope this helps.
Add that jar file to BuildPath of the project.
Right Click on the project --> BuildPath -- Configure builaPath -->Add external jars.
Because You are not running a web application.
Class.forName("com.mysql.jdbc.driver");
By typing driver name manually like above, we are getting ClassNotFoundException because of small spell mistakes
That's why always better to use when the fully qualified class name is the input for method
for example,
Class.forName(Driver.class.getName().toString());
Before this we need to set the mysql-version.jar file into the buid path

how to change Java code to access database using JDBC after creating JDBC connection pool in GlassFish?

I'm new to JDBC. I've installed GlassFish 3.1.1 on Centos 6.2 and need to use it with an application that connects to an Oracle 11G database on another server. I've read through the documentation for GlassFish and think I understand how to create a JDBC connection pool as well as a JDBC resource. My question is, how do I use this information when coding the java middle-tier to connect to the database?
Currently (with just GlassFish install and no JDBC configuration), I am relying on the CentOS enviroment variables for java (such as CLASSPATH) to allow the web application to use the JDBC drivers. However, I'm getting the following error:
java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource
Thus, my attempt to create a JDBC connection pool and resource in GlassFish (so the app can use the JDBC driver). My java file starts out:
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class JDBCexample {
public static void main(String args[]) throws SQLException {
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
String person_firstName;
String person_lastName;
String person_email;
int person_salary;
// connect to database
OracleDataSource ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:myID/myPWD#192.168.0.1:1521:mySID");
conn = ds.getConnection();
// read something in database
stmt = conn.createStatement();
query = "SELECT first_name, last_name, email, salary FROM HR.Employees where rownum < 6";
rset = stmt.executeQuery(query);
while (rset.next()) {
person_firstName = rset.getString("first_name");
person_lastName = rset.getString("last_name");
person_email = rset.getString("email");
person_salary = rset.getInt("salary");
System.out.format(person_firstName + " " + person_lastName + " " + person_email + " %d%n", person_salary)
}
and so on...
QUESTION: How would I change the above code after I create a JDBC Connection Pool (named: myPool) and a JDBC Resource (named: myDBPool)? If it matters, I'm using Oracle 11.2, CentOS 6.2, GlassFish 3.1.1 with mod_jk and fronted by Apache 2.2 webserver, JDK 1.6. I don't have any clustering or load-balancing.
UPDATE 1: I thought this link was a good reference (see section titled: "Creating a Data Source Instance, Registering with JNDI, and Connecting"). But when I modify the above Java file as follows (just preparing the java file; haven't touched GlassFish yet),
// Add These:
import javax.naming.Context;
import javax.naming.InitialContext;
// Change from this:
// connect to database
OracleDataSource ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:myID/myPWD#192.168.0.1:1521:mySID");
conn = ds.getConnection();
// To this:
// connect to database
Context ctext = new InitialContext();
OracleDataSource ds = (OracleDataSource)ctext.lookup("jdbc/myDBPool");
conn = ds.getConnection();
I get the errors:
JitterClass.java:67: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
Context ctext = new InitialContext();
^
JitterClass.java:68: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
OracleDataSource ds = (OracleDataSource)ctext.lookup("jdbc/myDBPool");
^
UPDATE 2: I cleared those compile errors using cyril's comments below (to throw all exceptions). Then I created JDBC Connection Pool and JDBC Resource, and the Ping was successful. So then I run the application from the client and observe the following error:
java.lang.ClassCastException : com.sun.gjc.spi.jdbc40.DataSource40 cannot be cast to oracle.jdbc.pool.OracleDataSource
At this point, if I add an include javax.sql.DataSource to the program, and change this line:
OracleDataSource ds = (OracleDataSource)ctext.lookup("jdbc/myDBPool");
to become this line:
DataSource ds = (DataSource)ctext.lookup("jdbc/myDBPool");
it compiles without errors. But now I'm confused... aren't we supposed to be using OracleDataSource here? Or, does GlassFish somehow implement OracleDataSource since I do see a setting for this connection pool for Datasource Classname set to oracle.jdbc.pool.OracleDataSource (?). Hoping someone can explain this.
Do pings on your connection pool work?
If not, check your pool configuration w/ http://docs.oracle.com/cd/E18930_01/html/821-2416/beamw.html#beanh
Once pings work and the JDBC Resource is configured, you should be able to access it in your app code through JNDI:
InitialContext context = new InitialContext();
DataSource ds = (DataSource) context.lookup("jdbc/myDBPool"); // or whatever name you used when creating the resource
conn = ds.getConnection();
Hope it helps,
RESPONSE TO UPDATE 1:
That's just the compiler telling you to formally catch or declare a checked exception which may be thrown by JNDI.
For testing purposes, the easiest way out of this (and future errors like this) is to just widen your method signature to throw all exceptions, i.e.:
public static void main(String args[]) throws /*SQL*/Exception {
RESPONSE TO UPDATE 2:
There's no reason to cast JDBC interfaces down to Oracle implementations unless you need to access any custom feature not specified in the JDBC spec. The purpose of a DataSource is to be a factory for Connections, whose API is defined in the JDBC interface, so that should be all you need. When you define a connection pool and resource in GlassFish, the app server is adding value by wrapping the JDBC driver classes and proxying them seamlessly for you as long as you stick to import java.sql.*. No need for oracle imports :) The main advantage being that if you ever decide to switch to MySQL or some other data store later on, your code is then portable and doesn't need any change.
To add to cyril's good answer:
Instead of the JNDI lookup, you can also use Resource Injection to set up your DataSource:
#Resource(name = "jdbc/Your_DB_Res")
private DataSource ds;
On startup, the application server will then inject the JDBC ressource. This section of Java EE Tutorial has more on that matter.
By using resource injection, you can reduce the amount of boilerplate code. This article introduces the concepts.
Besides adding the driver to your classpath, you should try adding the appserv-rt.jar file to your project's build path (the jar is located in Glassfish's lib directory). If you don't want to include all the other jars you should first create a library containing the appserv-rt jar and then add it to your project's build path.

Resources