0
回答
JDBC MYSQL数据连接池报javax.naming.NoInitialContextException错误
终于搞明白,存储TCO原来是这样算的>>>   

Tomcat 8 + mysql-5.6.24-winx64 

开发软件是IntelliJ 12

驱动mysql-connector-java-5.1.37-bin.jar已放到tomcat/lib目录下。

package Town;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 * Created with IntelliJ IDEA.
 * User: cobra
 * Date: 15-11-5
 * Time: 上午11:37
 * To change this template use File | Settings | File Templates.
 */
public class DbPool {
	private Connection con = null;

	public DbPool() throws Exception
	{
		InitialContext initContext = new InitialContext();
		Context envContext  = (Context)initContext.lookup("java:/comp/env");
		DataSource ds = (DataSource)envContext.lookup("jdbc/lucom");
		con = ds.getConnection();
	}

	public void close() throws SQLException
	{
		if (con != null) {
			con.close();
		}
	}

	public int executeUpdate(String sql) throws Exception
	{
		Statement statement = con.createStatement();
		// 创建一个工作簿(表),使用完后一定要关闭,否则会出错!
		int x = statement.executeUpdate(sql);
		statement.close();
		return x;
	}

	public ResultSet executeQuery(String sql) throws SQLException
	{
		Statement statement = con.createStatement();
		ResultSet result = statement.executeQuery(sql);
		return result;
	}
}



\Tomcat 8.0\conf\Catalina\localhost\lucom.xml文件内容

<?xml version="1.0" encoding="UTF-8"?> 
<Context 
    docBase="E:\data\webapp\webserver\lucom\web" 
    reloadable="true"> 
	<Resource name="jdbc/lucom" auth="Container" type="javax.sql.DataSource"
		maxActive="100" maxIdle="30" maxWait="10000"
		username="root" password="******" driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost:3306/lucom?useUnicode=true&characterEncoding=UTF8"/>
</Context>



项目web\WEB-INF\web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
           version="2.5">
	<resource-ref>
		<description>DB Connection</description>
		<res-ref-name>jdbc/lucom</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

</web-app>



调试过程一直都报以下错误

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at Town.DbPool.<init>(DbPool.java:24)
at test.chkDb(test.java:31)
at test.main(test.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


在网上找了很久都找到错在什么地方,本人小白请大神指点。





举报
icecobra
发帖于2年前 0回/427阅

以下是问题补充:

  • @icecobra :发现是url中的useUnicode=true&characterEncoding=UTF8问题,去除后就可以了,另外问这两个参数可以通过什么方法设置? (2年前)
顶部