First of all, I'm a computer science student, and I'm not very much into computer science world yet (i.e. I've little experience doing stuff on my own). So sorry for not having all the knowledge possible on it.
Then, in one of my classes I learnt how to create web application with java (jsp, beans, etc.) plus all the client-side stuff (html, css, javascript, ect.).
I work on NetBeans IDE.
To connect to a MySQL database, I use connection pooling in this way:
1) Add MySQL JDBC Driver jar
2) A DBConnect.java java class with a method that returns a connection:
public static Connection getConnection() {
/* JNDI query to locate the DataSource object */
Context initContext;
try {
initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env"); // JNDI standard naming root
DataSource ds = (DataSource) envContext.lookup("jdbc/aName");
/* Ask DataSource for a connection */
Connection conn;
try {
conn = ds.getConnection();
return conn;
} catch (SQLException ex) {
Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException("cannot open Connection", ex);
}
} catch (NamingException ex) {
Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException("cannot find DataSource reference", ex);
}
}
3) web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<resource-ref>
<description>Resource reference to a DataSource for managing a connection pool.</description>
<res-ref-name>jdbc/aName</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
4) context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myApp">
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/aName"
username="username"
password="password"
type="javax.sql.DataSource"
url="jdbc:mysql://"whateverhost":"whateverport"/dbSchema?autoReconnect=true"/>
</Context>
Now, I created a small project and I wanted to publish it online for free. I ran into OpenShift and I managed to push all my files on it (even if the folders' schema is different).
The problem is that the connection pooling doesn't work, and I don't have a clue on what to do.
Running the application, these are the exceptions:
java.lang.RuntimeException: cannot open Connection
....
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
....
java.sql.SQLException: No suitable driver
....
mysql-connector jar is in /WEB_INF/lib and pom.xml has:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
Maybe the solution is quite simple but I don't know what to do.
Thank you.