3 Replies - 5880 Views - Last Post: 26 November 2012 - 02:50 AM

#1 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Displaying Data from MySQL server using through Servlets

Posted 21 August 2012 - 09:15 AM

I get an error when i run this program. I think its because I placed the MySql Connector File in the wrong place. I'm not sure. Can someone guide me through?

Appreciate it.

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/DataInsertion")
public class DataInsertion extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{	
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String url = "jdbc:mysql://localhost/CollegeStudents?user=root&password=12345";
		Connection conn;
		ResultSet rs;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url);
			Statement statement = conn.createStatement();
			String query = "insert into emp_sal values(15000)";
			int i = statement.executeUpdate(query);
			if(i!=0){
				out.println("The record has been inserted");
			}
			else{
				out.println("Sorry! Failure");
			}
			rs = statement.executeQuery("select * from emp_sal");
			while(rs.next()){
				out.println("<p><table>" + rs.getString(1) + " " + rs.getInt(2) + "</p></table>");
			}
			rs.close();
			statement.close();
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}
}





java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:169)
	at DataInsertion.doGet(DataInsertion.java:16)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:680)




Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Data from MySQL server using through Servlets

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Displaying Data from MySQL server using through Servlets

Posted 21 August 2012 - 09:18 AM

Moved to Java EE.
Was This Post Helpful? 0
  • +
  • -

#3 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Displaying Data from MySQL server using through Servlets

Posted 12 September 2012 - 12:51 AM

Try moving the mysql connector jar file into WebContent/WEB-INF/lib, and then restart your Tomcat server. See if that works.
Was This Post Helpful? 0
  • +
  • -

#4 demongill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-January 09

Re: Displaying Data from MySQL server using through Servlets

Posted 26 November 2012 - 02:50 AM

in project properties,
check your Java Build Path setting,
see Order and Export setting,
check your mySQl connector jar to be exported..

then try to export to war -> redeploy..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1