在写数据库连接部分的代码时,考虑到使用连接池来优化查询效率,然后呢就是脑浆炸裂的一下午的debug,各种问题搞得心态都炸了,最后经过不断摸索成功解决。我使用的是tomcat 8.5和mysql 5.1 主要步骤为
1.添加mysql-connector-java-version
.jar 到$CATALINA_HOME/lib
2.修改$CATALINA_HOME/conf/server.xml文件
3.新建META-INF文件夹,新建context.xml文件
4.编写业务代码
问题1:javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
在webapp/下新建META-INF文件夹(如果已经有了就不要建了),新建context.xml文件写入如下内容:
<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
<ResourceLink name="jdbc/MySQLDB"
global="jdbc/MySQLDB"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/blog?serverTimezone=GMT%2B8"/> </Context>
注意这里的driverClassName,我看了很多以前的文章上面相关介绍都是老版本的com.mysql.jdbc.Driver,这个已经是过去式啦要注意。
server.xml中找到相关标签,修改成如下内容,:<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
...
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="123456"
maxIdle="30"
maxWaitMillis="10"
maxTotal="10"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/blog?serverTimezone=GMT%2B8" />
</GlobalNamingResources>
这里的maxTotal, maxWaitMillis 而不是maxActive和wait,这与官网的文档有些出入。然后context与server中的信息一定要完全一致。
问题2:Cannot create JDBC driver of class '' for connect URL 'null' java.sql.SQLException:
导致这个的原因主要是context的问题,如果DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");能够正常执行,且返回非NULL,说明数据源映射没有问题,如果你和我一样选择使用context.xml文件来设置context的话,则可能是因为context.xml中未设置url的值。设置成如下:
<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
<ResourceLink name="jdbc/MySQLDB"
global="jdbc/MySQLDB"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/blog?serverTimezone=GMT%2B8"/> </Context>
问题3:Cannot create PoolableConnectionFactory The server time zone value '???ú±ê×??±??' is unrecognizedor represents more than one time zone
主要原因在于未设置server time zone,解决方案:修改server.xml,添加参数serverTimezone
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
...
url="jdbc:mysql://127.0.0.1:3306/blog?serverTimezone=GMT%2B8" />
然后是业务代码,用来获取connection:
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;
import static java.lang.System.exit;
public class DbConPool {
public static Connection con=null;
private DbConPool(){}
public static Connection getCon(){
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
if(ds==null){
System.out.println();
System.out.println("数据连接打开错误");
exit(1);
}
System.out.println("数据连接打开成功");
con = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
注意事项:
1.有些博主还提到需要在web.xml中添加如下代码,我测试了下,结果有没有都行
<resource-ref>
<res-ref-name>jdbc/MyDBSQL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2.官网的文档上面关于server.xml的配置有个context标签,我试了下不添加也能正常运行
3.遇到错误先确认下.jar是否已经正确添加到classpath变量下。
4.如有不当之处,敬请指正。