I am trying to connect to mysql database from jsp page. The
connection code is as below
InitialContext ic=new InitialContext();
DataSource ds=(DataSource)ic.lookup("jdbc:mysql://localhost:3306/");
Connection con=ds.getConnection();
Statement stmt = con.createStatement();
when i open the page i get the following error
javax.servlet.ServletException: javax.naming.NamingException: Lookup
failed for ‘jdbc:mysql://localhost:3306/’ in SerialContext [Root
exception is javax.naming.NameNotFoundException: jdbc:mysql:]
can some one please tell me what is wrong with this…
You’re trying to look up a persistent connection using JNDI. JNDI is used to store and access resources in a servlet container, but you’re using a JDBC connection string instead of a JNDI reference.
If you want to connect direct to your database via JDBC in the page, you want something like the following to get your connection.
If, on the other hand you want to use JNDI, you need to store the connection in JNDI first, then access it in your JSP via the name you used to store it. It’s a more complex process, so here’s a link to the appropriate place in the Tomcat documentation that explains how to do it.