hello frnd
i am wriing program on in which i try to connect mysql with servlet but it gives error

here is my code

[B]Test.java[/B]
package my;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class Test
 */
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Test() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("sai ram ji run kr do");
		Context ctx=null;

		System.out.println("sai ram ji run kr do2");
		try {
			ctx= new InitialContext();

			System.out.println("sai ram ji run kr do3");
		} catch (NamingException e) {
			// TODO Auto-generated catch block

			System.out.println("sai ram ji run kr do4");
			e.printStackTrace();
		}
			DataSource ds=null;
			try {
				System.out.println("sai ram ji run kr do5");
				
				ds=(DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
			} catch (NamingException e1) {
				// TODO Auto-generated catch block
				System.out.println("sai ram ji run kr do6");
				
				e1.printStackTrace();
			}
			if(ds!=null){
				Connection con=null;
				System.out.println("sai ram ji run kr do7");
				
				try {System.out.println("sai ram ji run kr do8");
				
					con= ds.getConnection();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					System.out.println("sai ram ji run kr do9");
					
					e1.printStackTrace();
				}
				if(con!=null){
					
					System.out.println("hurrey finally i run   .........  "+con.toString());
					System.out.println();
					
				}
					
				
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
[B]context.xml[/B]
<context>
<Resource   name="jdbc/TestDB" 
			auth="Container"   maxWait="10000"   
			type="javax.sql.DataSource"
			maxActive="100" maxIdle="30"
   			driverClassName="com.mysql.jdbc.Driver"
   			username="nn"
   			password="rahul"
   			url="jdbc:mysql://localhost:3306/db" />
</Context>
[B]web.xml[/B]
<resource-ref>
		   <description>[Your Description]</description>
		   <res-ref-name>jdbc/jdbc/TestDB</res-ref-name>
		   <res-type>javax.sql.DataSource</res-type>
		   <res-auth>Container</res-auth>
   </resource-ref>
[B]my sqlcommands are[/B]
mysql> GRANT ALL PRIVILEGES ON *.* TO nn@mz.com IDENTIFIED BY 'nn' WITH GRANT OP
TION;
Query OK, 0 rows affected (0.09 sec)

mysql> CREATE DATABASE db;
Query OK, 1 row affected (0.05 sec)

mysql> USE db
Database changed
mysql> create table t values
    -> ;
mysql> create table t
    -> (
    -> id int not null auto_increment primary key,
    -> name varchar(25),
    -> points int
    -> );
Query OK, 0 rows affected (0.17 sec)

mysql> insert into t
    -> values (null,'rahul',99)
    -> ;
Query OK, 1 row affected (0.06 sec)

mysql> select * from t
    -> ;
+----+-------+--------+
| id | name  | points |
+----+-------+--------+
|  1 | rahul |     99 |
+----+-------+--------+
1 row in set (0.03 sec)

here error show on server
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'javauser'@'localhost' (using password: YES))
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at my.Test.doGet(Test.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Access denied for user 'javauser'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
... 17 more

please help me to find problem becouse i already wasted 1 week

Well, that can't be the configuration that is being used, as it is trying to use a user "javauser" not "nn". Also, if you are going to connect using "localhost" then don't forget to add a grant for "localhost" as that will not be the same as the grant for "mz.com" even if that machine is "mz.com".

masijade thanks for the reply
masijade i have change what you said but still getting the same error

hey masijade , i solve my problem
it need to concentrate on username and password
in my case password is nn
thanks for consideration

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.