Results 1 to 10 of 10
  1. #1
    Drake is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    I want to get some data from a mysql DB using a method that's in a class. I call the method from a jsp file and it returns nothing.
    I hope you can help me.

    Here is the code

    JAVA FILE:

    Java Code:
    package org.Ampli;
    import java.sql.*;
       public class Connect
       {//-----------------------------------------------------This is de variable i will put the data into.
    	   static String todo="";
    	   
    	   //------------------------------------------------This is the method i want to use for getting the data
    		public   static String mostrarTodo() {
    			return(todo);
    		}
           public static void main (String[] args)
           {
               Connection conn = null;
               try
               {
                   String userName = "root";
                   String password = "admin";
                   String url = "jdbc:mysql://localhost/sakila";
                   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                   conn = DriverManager.getConnection (url, userName, password);
                   PreparedStatement statement=conn.prepareStatement("select * from country");
                   ResultSet result=statement.executeQuery();
    
        		while(result.next())
        		{
        			todo=((result.getString(1)+" "+result.getString(2))+todo);
        		}
        		
               }
               catch (Exception e)
               {
                   System.err.println ("No se puede conectar a la BD");
               }
               finally
               {
                   if (conn != null)
                   {
                       try
                       {
                           conn.close ();
                           System.out.println ("Conexion terminada");
                       }
                       catch (Exception e) { }
                   }
               }
           }
    
    
    	}
    ----------------------------------

    JSP FILE:


    Java Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@page import="org.Ampli.Connect"%>
       <html>
    <head>
    <title>Elige tu Amplificador (BETA)</title>
    </head>
    <body>
       <%String most=Connect.mostrarTodo();%>
       Resultado: <%=most%>
    </body>
    </html>

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    What do you expect it to return? When do you expect the todo variable to be set with something other than its default value?

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    Check your logs or console for an exception?
    ALso use printSTackTrace() for your exceptions, so you know what's actually caused it...or use a proper logging tool.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    Oh crikey...I didn't notice it was all in a main() method...:)

  5. #5
    Drake is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    Sorry guys but i'm a noob in java. I don't know what's wrong

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    When do you think the main method of your class will get called? Or will it get called at all in a web application?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    Put another way, main() is the entry point for a normal Java app. However, as a web application (which is what a JSP is), that entry point is inside the server (eg Tomcat), and your webapp gets called via that. So unless you call your main() from the JSP page then it is never called. Note, I would not suggest doing this that way.

    As a quick fix to simply get the method working stick the code in main() into the other method.

  8. #8
    Drake is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    I though calling he method implied the execution of the main. Sorry, i will try calling main and put the return clause on the main method

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    You should rename main() then, because that's not exactly descriptive of what it is doing...

  10. #10
    Drake is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: (Mysql Java and JSP) Getting Data from DB using a Method calling from JSP

    I decided to not use main method and only the other one.

    It still doesn't show me a thing, what do you think?

    Java Code:
    package org.Ampli;
    import java.sql.*;
       public class Connect
       {
    		public static String mostrarTodo() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    			Connection conn = null;
    			String todo="";
    		    String userName = "root";
    		    String password = "admin";
    		    String url = "jdbc:mysql://localhost/sakila";
    		    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    		    conn = DriverManager.getConnection (url, userName, password);
    		    PreparedStatement statement=conn.prepareStatement("select * from country");
    		    ResultSet result=statement.executeQuery();
    		    while(result.next())
    		    	{
    		    		todo=((result.getString(1)+" "+result.getString(2))+todo);
    		    	}
    			return(todo);
    		}
    	}

Similar Threads

  1. Replies: 1
    Last Post: 04-29-2011, 10:02 PM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  3. Java, MySQL and Data Type Float
    By digidigdj in forum AWT / Swing
    Replies: 0
    Last Post: 03-11-2010, 07:42 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. MySQL 5.0 data backup from java
    By sanjay_sharma77 in forum JDBC
    Replies: 1
    Last Post: 11-06-2007, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •