首先,我是一名计算机科学专业的学生,而且我还没有进入计算机科学领域(也就是说,我自己很少有经验)。很抱歉没有掌握所有知识。
然后,在我的一个课程中,我学习了如何使用java(jsp,bean等)以及所有客户端(html,css,javascript等)创建Web应用程序。
我在NetBeans IDE上工作。
为了连接到MySQL数据库,我以这种方式使用连接池:
1)添加MySQL JDBC Driver jar
2)DBConnect.java java类,其中包含一个返回连接的方法:
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>
现在,我创建了一个小项目,我想在线免费发布。我遇到了OpenShift,我设法将所有文件都推送到它上面(即使文件夹的架构不同)。
问题是连接池不起作用,我不知道该怎么做。
运行应用程序,这些是例外:
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在/ WEB_INF / lib中,pom.xml有:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
也许解决方案很简单,但我不知道该怎么做。 谢谢。
答案 0 :(得分:1)
我认为问题是你的web.xml文件。这是多余的。 Context.xml指定具有适当配置的数据源,然后web.xml指定一个没有URL或驱动程序类名称的数据源。
尝试从web.xml中删除此resource-ref
块,然后重试:
<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>
您在context.xml中的URL属性中还有额外的引号:
url="jdbc:mysql://"whateverhost":"whateverport"/dbSchema?autoReconnect=true"/>
制作本:
url="jdbc:mysql://whateverhost:whateverport/dbSchema?autoReconnect=true"/>
答案 1 :(得分:1)
我自己在OpenShift和我的Tomcat(它安装在个人电脑上)遇到了这个问题。
似乎问题与context.xml文件有关。 即使我编辑了我克隆的openshift项目中的context.xml文件,似乎问题并没有消失。 我通过访问eclipse服务器目录设法解决了这个问题:/ servers / Tomcat v7.0 Server at localhost-config / context.xml
在context.xml中我必须手动添加:
<Resource name="jdbc/MySQLPool"
url="jdbc:mysql://localhost:3306/sakila"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="nbuser"
auth="Container"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="5"
maxWait="10000"
/>
保存文件,我的电脑上的Tomcat解决了所有问题。
此刻我还在尝试解决OpenShift问题。 我正在使用eclipse,即使我编辑了我的OpenShift克隆中的context.xml,似乎不知何故,必须访问和更新OpenShift-Tomcat平台上的context.xml文件,如上例所示。
希望这有帮助!
更新: 我设法以同样的方式为OpenShift平台解决了这个问题。 通过使用Filezilla(openshift指南可以在这里找到:https://blog.openshift.com/using-filezilla-and-sftp-on-windows-with-openshift/) 我已经连接到服务器,加入了Tomcat目录(在我的例子中:jbossews / conf / context.xml),我通过添加与上面相同的xml资源手动编辑了文件。
希望它有所帮助!