I tried to run project on tomcat 7.0.52
and initialize to DB through context.xml
file.
But it throws bunch of exceptions, I couldn't figure out what is wrong there.
Here is console output:
java.sql.SQLException: com.mysql.jdbc.Driver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:554)
at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:242)
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:141)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
Here is full stack trace.
Here is output to tomcat catalina log
snippet of web.xml:
<resource-ref>
<description>Travel Agency Datasource</description>
<res-ref-name>jdbc/onlinedb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
context.xml:
<Context>
<Resource name="jdbc/onlinedb"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxActive="20" maxIdle="10"
maxWait="-1"
username="root"
password="secret"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/travelagency?characterEncoding=utf8"/>
</Context>
ConnectionManager class:
public class ConnectionManager {
private static Logger log = Logger.getLogger(ConnectionManager.class);
public static Connection getConnection() throws SQLException {
Connection con = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource datasource = (DataSource) envContext.lookup("jdbc/onlinedb");
con = datasource.getConnection();
} catch (NamingException e) {
log.error(e);
}
return con;
}
}
mysql-connector-java-5.1.27-bin.jar
is added to cp:
I tried to change content of context.xml file:
<resource-env-ref>
<description>Travel Agency Datasource</description>
<resource-env-ref-name>jdbc/onlinedb</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
But it keep failing.
How to solve this trouble?
You have to add a MySQL jdbc driver to the classpath.
Either put a MySQL binary jar to tomcat lib folder or add it to we application WEB-INF/lib folder.
You can find binary jar (Change version accordingly): https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.27