Results 1 to 10 of 10
- 12-01-2011, 01:14 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
(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>
- 12-01-2011, 04:00 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
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?
- 12-01-2011, 04:02 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 12-01-2011, 04:03 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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...:)
- 12-01-2011, 09:22 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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
- 12-01-2011, 09:33 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
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?
- 12-01-2011, 09:50 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 12-01-2011, 10:30 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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
- 12-02-2011, 09:36 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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...
- 12-02-2011, 01:36 PM #10
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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
-
reading from a file in java calling the parse method
By adjit in forum New To JavaReplies: 1Last Post: 04-29-2011, 10:02 PM -
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Java, MySQL and Data Type Float
By digidigdj in forum AWT / SwingReplies: 0Last Post: 03-11-2010, 07:42 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
MySQL 5.0 data backup from java
By sanjay_sharma77 in forum JDBCReplies: 1Last Post: 11-06-2007, 12:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks