Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 26th, 2003, 03:44 AM
Sthir Sthir is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: San Francisco, California
Posts: 6 Sthir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old May 26th, 2003, 07:35 AM
jaffadan jaffadan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 13 jaffadan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #3  
Old May 26th, 2003, 03:13 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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;

Reply With Quote
  #4  
Old May 26th, 2003, 09:05 PM
Sthir Sthir is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: San Francisco, California
Posts: 6 Sthir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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
File Type: java sconnection.java (1.3 KB, 274 views)

Reply With Quote
  #5  
Old May 27th, 2003, 09:27 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
  #6  
Old May 28th, 2003, 12:32 PM
Sthir Sthir is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: San Francisco, California
Posts: 6 Sthir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks a lot!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Connecting to MySQL using servlet


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT