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 5 of 5

Thread: Problem in MYSQL data base Connectivity with servelet

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Arrow Problem in MYSQL data base Connectivity with servelet

    Hi
    I m trying to pass the arraylist from Servelet to JSP page Which data is get t from My SQL data base.Plz anyone help me whats wrong in my code part


    Login.java

    package com;
     
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    public Login() {
    super();
    }
     
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
     
    	HttpSession session = request.getSession(true);
     
    	response.setContentType("text/html");
     
     
    	ArrayList<String>items1 = new  ArrayList<String>();
    	ArrayList <String>items2 = new ArrayList<String>();
     
    	try{
     
    			String url = "jdbc:mysql://localhost:3306/atopfinal";
    			String driver = "com.mysql.jdbc.Driver";
    			Class.forName(driver).newInstance();
    			Connection conn;
    			conn = DriverManager.getConnection(url, "root","root");
    			Statement s = conn.createStatement ();
    			s.executeQuery ("SELECT * from beml_112_gps");
    			ResultSet rs = s.getResultSet ();
    		while (rs.next ()) {
    	        items1.add(rs.getString ("LAT"));
    	        items2.add(rs.getString ("LON")); 
     
    		}
    		rs.close ();
    		s.close ();
    	}
     
    		catch(Exception ee){
    			System.out.println(ee.toString());	
    		} 
    		 String destination = "/FirstJSP.jsp"; 
    		    request.setAttribute("items1",items1);
    		    request.setAttribute("items2",items2);
    	        RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
    			   rd.forward(request, response);
    	       // request.getRequestDispatcher("FirstJSP.jsp").forward(request, response);
     
     
     
        }
    }



    FirstJSP.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>`
     <%@ page import="java.util.ArrayList" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org   /TR/html4/loose.dtd">`
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>`
    </head>
    <body>
    <form action="Login" method="post">
     
    <input type="submit" value="submit"/><br>
    <%
    ArrayList <String> str1=(ArrayList <String>)request.getAttribute("items1");
    ArrayList <String> str2=(ArrayList <String>)request.getAttribute("items2");
     
     out.println(str1);
     out.println(str2);
     if(request.getAttribute("items1")!=(""))
    	 out.println(request.getAttribute("items1"));
    	 else 
    	 out.println("");
    if(request.getAttribute("items2")!=(""))
    out.println(request.getAttribute("items2"));
    else 
    out.println("");
     
    %>
     </form>
     </body>
    </html>
    Last edited by pbrockway2; April 23rd, 2013 at 12:49 AM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problem in MYSQL data base Connectivity with servelet

    Hi kalaicse30, welcome to the forums.

    I've added code tags to your post. Basically you put [code] at the start of a section of code and [/code] at the end so that the code ends up readable.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem in MYSQL data base Connectivity with servelet

    Welcome kalaicse
    Quote Originally Posted by kalaicse30@gmail.com View Post
    Hi
    I m trying to pass the arraylist from Servelet to JSP page Which data is get t from My SQL data base.
    I do not understand your question. Can you ask it a different way?

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Arrow Re: Problem in MYSQL data base Connectivity with servelet

    Quote Originally Posted by jps View Post
    Welcome kalaicseI do not understand your question. Can you ask it a different way?
    Hi,
    Im trying to pass array list from servlet to jsp page.but it always shows null values.i do not know whether it s problem from data base connectivity or passing array list.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem in MYSQL data base Connectivity with servelet

    Print your array list and see what is in it just before you try to serve it

Similar Threads

  1. Replies: 1
    Last Post: April 9th, 2012, 05:13 PM
  2. java & mysql connectivity..
    By itssnigdha in forum JDBC & Databases
    Replies: 2
    Last Post: February 4th, 2012, 08:51 AM
  3. JDBC connectivity with MySQL
    By naqib in forum JDBC & Databases
    Replies: 1
    Last Post: January 14th, 2012, 10:14 AM
  4. Connectivity problem:MySQL and Java
    By Pragya in forum JDBC & Databases
    Replies: 2
    Last Post: January 22nd, 2010, 03:58 AM