Errors in establishing a database connection using data source objects

is that when I was learning Servlet, I saw that there was another way to connect to the database, but I made a mistake in the test.
this is what the book says

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
conn = ds.getConnection();

after I made a mistake, I read another way to write it on the Internet

.
Context ctx = new InitialContext();
Context envContext = (Context)ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/bookstore");
conn = ds.getConnection();

is still wrong.
the following is an error

 Name [jdbc/bookstore] is not bound in this Context. Unable to find [jdbc].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at ch04.servlet.GetDBInfoServlet2.doGet(GetDBInfoServlet2.java:48)

here is the configuration information:
context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/bookstore" auth="Container"
    type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000"
    username="root" password="104679"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/bookstore?autoReconnect=true"
    />
</Context>
Mar.04,2021

Wow, I've tried it. I just added the Resource xml code to the context.xml in the conf folder of the Tomcat directory.

Menu