#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    San Francisco, California
    Posts
    6
    Rep Power
    0

    Connecting to MySQL using servlet


    Hi,
    I am trying to connect to the MySql database using a servlet. I have been downloaded the mysql-connector-java-3.0.7-stable-bin.jar file into my Lib directory in my WEb-INF dir.

    I am getting the following compilations errors.

    Connection.java:28: incompatible types
    found : java.sql.Connection
    required: Connection
    con = DriverManager.getConnection("jdbc:mysql://localhost/work");
    ^
    Connection.java:29: cannot resolve symbol
    symbol : method prepareStatement (java.lang.String)
    location: class Connection
    PreparedStatement pstmt = con.prepareStatement("
    SELECT * FROM employee");
    ^
    Connection.java:42: cannot resolve symbol
    symbol : method close ()
    location: class Connection
    try {con.close();}
    ^

    Can anyone please tell me why I am getting this error.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    13
    Rep Power
    0
    Well without the rest of your code we can not see if you are importing java.sql.*

    You should also check and see if this type of database connection is really what you want to do. Creating and using a datasource would be a safer route to go.

    What app server are you using. So that i could help you more
  4. #3
  5. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    If I had to guess, I would say you named your class "Connection", correct? And you likely have a line like this
    Code:
    Connection con;
    ...
    
    con = DriverManager.getConnection("jdbc:mysql://localhost/work");
    It thinks the Connection con; is the class you created, not the java.sql.Connection class. Rename your class or qualify the con variable like this
    Code:
    java.sql.Connection con;
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    San Francisco, California
    Posts
    6
    Rep Power
    0
    Thanks Nemi. I got rid of the compilation errors. But I when try to run the program it gives me the following error(list). I have checked my web.xml file. Changed my class Connection to SConnection (so that there is no confusion). Could you please help me out again.

    Thanks a lot

    The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    java.lang.NullPointerException
    at SConnection.doGet(SConnection.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:288)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:263)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:561)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1018)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:196)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:561)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1018)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2748)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:186)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:561)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1018)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:561)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1018)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:199)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:630)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.jav a:463)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:568)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:631)
    at java.lang.Thread.run(Unknown Source)

    I am also attaching my code
    Attached Files
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    The reason you are likely getting a null pointer exception is because you are encountering a problem connecting to the database and 'con' never gets set to anything. Was there a "Error Occured" output to the browser?

    In your finally block, since the code execution can reach it and the con variable may not be set, you should always check that the con variable is not null before trying to close it. Somehting like this:
    Code:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    				throws  IOException, ServletException
    		{
    					
    					response.setContentType("text/html");
    					PrintWriter out = response.getWriter();
    					java.sql.Connection con = null;
    					out.println("<html> <head> <title> Connection Successful");
    					out.println(" </title> </head> <body>");
    					
    			try { 
    				
    				Class.forName("com.mysql.jdbc.Driver");
    			        con = DriverManager.getConnection("jdbc:mysql://localhost/work");
    				PreparedStatement pstmt = con.prepareStatement("SELECT * FROM employee");
    				ResultSet results = pstmt.executeQuery();
    			    }
    			catch (Exception ex) 
    			{
    			 out.println("<h2> Error Occured </h2>");
    			}	
    			
    		
    						
    			
    			finally 
    			{
    				try {
    					if(con != null) con.close();
    				} 
    				catch (SQLException e)
    				{
    				out.println("<h2> Error in closing the connection <h2>");
    				e.printStackTrace();
    				}
    			} 
    			out.println("</body></html>");
    					
    		}
    
    }
    After fixing that you will need to figure out why you are not connecting to the database. Probably because you are not using a username and password.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    San Francisco, California
    Posts
    6
    Rep Power
    0
    Thanks a lot!!

IMN logo majestic logo threadwatch logo seochat tools logo