出錯提示
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
下面是我的測試的類
```public class myConn {
Connection conn;
// 初始化創建連接
public myConn() {
Context ic;
try {
ic = new InitialContext();
DataSource s = (DataSource) ic.lookup("java:comp/env/jdbc/conn");
conn = s.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 外界得到連接
public Connection getConnection() {
return conn;
}
// 外界關閉數據庫連接
public void closeConn() {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("數據庫連接關閉失敗");
}
}
}
這是tomcat的context中的配置
name="jdbc/conn"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/exchange"
username="root" password="500235"
maxActive="20" maxIdle="10" maxWait="1000"
/>
這是tomcat的web配置
exchange DataSource
jdbc/conn
javax.sql.DataSource
Container
需要在環境或系統中指定類名,或者一個子程序參數,或者在應用程序資源文件中初始化~