Reputation: 176
I'm a total beginner and i'm doing a simple jee project using Tomcat 9.0, MySQL and jdbc.
While trying to connect to the DB i get:
javax.naming.NameNotFoundException: Name [jdbc/city2] is not bound in this Context. Unable to find [jdbc].
I've already tried to add config file to Tomcat folder apache-tomcat-9.0.24\conf\Catalina\localhost, but it did nothing. This is how my ConnectionProvider class looks like:;
private static DataSource dataSource;
public static Connection getConnection() throws SQLException {
return getDSInstance().getConnection();
}
private static DataSource getDSInstance() {
if (dataSource == null) {
try {
Context initContext = new InitialContext();
dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/city2");
} catch (NamingException e) {
e.printStackTrace();
}
}
return dataSource;
}
And my context.xml file looks like:
<Context>
<Resource name="jdbc/city2"
auth="Container"
type="javax.sql.DataSource"
initialSize="10"
maxTotal="100"
maxIdle="30"
maxWaitMillis="10000"
username="root"
password="admin"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/city2?useSSL=false&serverTimezone=UTC" />
</Context>
Upvotes: 2
Views: 1017
Reputation: 5207
Declare datasource in web.xml:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/city2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
See more details here.
Upvotes: 1