Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I am trying to show database contents in the browser through servlets

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I am trying to show database contents in the browser through servlets

    I have this servlet code for retrieving contents from database and displaying it on the browser.But when I am running this code it is giving "ClassNotFound Exception" at Class.forname().I have built the path with db2jcc.jar and db2jcc_license.jar file but still this exception is thrown.I also tried to do it with JSP but over there also same exception was thrownPlease help me


     
     
    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
     
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.*;
     
     
     
    /**
     * Servlet implementation class HelloServlet
     */
    public class HelloServlet extends HttpServlet {
     
    	public Statement statement;
    	public Connection connection;
     
     
    	private static final long serialVersionUID = 1L;
     
     
        /**
         * @see HttpServlet#HttpServlet()
         */
        public HelloServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
     
     
     
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
     
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
     
    	PrintWriter out=response.getWriter();
    	out.print("dkfjdkfjdkjfd");
     
    	     // Statement statement = null;
     
    	      ResultSet rs = null;
    	    System.out.println("loading class") ;
     
    	      try {
    			Class.forName("com.ibm.db2.jcc.DB2Driver");//Exception thrown
    			System.out.println("abcd");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	System.out.println("after loading class"); 
     
    	      try {
    			connection  = DriverManager.getConnection("jdbc:db2://localhost:50000/Simple","administrator","welcome2sa2");                                                                         // Exception thown
    		System.out.println(connection.getClass());
    	      } catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	  System.out.println("after conncetion class");
    	     try {
    		 statement = connection.createStatement();                                //Exception thrown
    		 System.out.print("abcd");
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
     
     
    	      String QueryString = "select * from question";
     
    	      try {
    			rs = statement.executeQuery(QueryString);
    			while(rs.next()){
    				for(int i=1;i<=6;++i)
    					out.print(rs.getString(i)+"\t");
     
    			}
    					} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		// TODO Auto-generated method stub
    	}
     
     
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: I am trying to show database contents in the browser through servlets

    It helps to post the full exception...that being said, if it cannot find the class then the class is not on the classpath - make sure the driver is on the of your webapp.

  3. The Following User Says Thank You to copeg For This Useful Post:

    abhiM (August 12th, 2011)

  4. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to show database contents in the browser through servlets

    Quote Originally Posted by copeg View Post
    It helps to post the full exception...that being said, if it cannot find the class then the class is not on the classpath - make sure the driver is on the of your webapp.


    Thanks for help.That problem is solved now.Now I am having some logical errors which I am trying to solve.If you could help me with those errors it will be great.I have posted my query in this forum only with title "I have a prob in my JSP code".you can search for this title with my name and can get what the errors are about.Thanks once again

Similar Threads

  1. How to tranfer contents from Database onto the web using Java
    By abhiM in forum Java Theory & Questions
    Replies: 4
    Last Post: August 5th, 2011, 09:42 AM
  2. Replies: 1
    Last Post: August 3rd, 2011, 02:28 PM
  3. how to write code using servlets, jsp and mysql database
    By bvn456 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 28th, 2011, 01:51 PM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  5. Reading contents of another window
    By jimmys in forum Java Theory & Questions
    Replies: 8
    Last Post: October 4th, 2010, 11:40 PM

Tags for this Thread