This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Functions

 
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to call this class whenever I need to show the list values of the countries but being a begginner in java unable to understand right now. Can anybody Please help me?

Thanks & best regards

<blockquote>code:
<pre name="code" class="core">import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class countries extends HttpServlet{

Connection cnn=null;
ResultSet rs=null;

public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{

res.setContentType("text/html");
PrintWriter out=res.getWriter();

try{

if ((cnn==null)||cnn.isClosed()){
cnn=db.getOracleConnection();
}

String country="",country1="",country2="";

String query="select * from country";
PreparedStatement ps=cnn.prepareStatement(query);
rs=ps.executeQuery();

while(rs.next()){
int id=rs.getInt(1);
String col2=rs.getString(2);

country1+="<select name=country class=khan>"+
"<option value=''>-- Select Country --</option>"+
"<optioin value=\""+id+"\">"+col2+"</option>";
}
country2="</select>";

country=country1+country2;
ps.close();


}catch(Exception e){
out.println(e.toString());
System.out.println("countries.java "+e.toString());
}

finally {
try {
if ( rs != null ) rs.close();
if ( cnn != null ) cnn.close();
} catch(SQLException ex) {
System.out.println("countries..java "+ex.toString());
}
}

}
}
</pre>
</blockquote>
[ July 15, 2008: Message edited by: Campbell Ritchie ]
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you don't call a class - you call a method. you need an instance of this class to call the method. somewhere, you would have done a

countries myCountries = new countries();

(note: traditionally, a class name starts with a capital letter)

Then, you could call myCountries.doGet(<parameters go here> .

Alternately, you could make the method static and declare the the two parameters inside the method, then call it by simply saying

countries.doGet(<parameters go here>
 
Marshal
Posts: 22460
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhm, Fred? You are well aware that this is a servlet, and it should be run from a web container like Apache Tomcat?

Farakh, if you do that correctly all you need to do is open a web browser and navigate to the servlet.
 
fred rosenberger
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then shouldn't this be in the servlet forum?
 
Rob Spoor
Marshal
Posts: 22460
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably.
 
Farakh khan
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I didn't understand. Can you Please alter my provided code

Thanks & best regards
 
Marshal
Posts: 74069
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
then shouldn't this be in the servlet forum?

If you and Rob agree about it, then it shall go to the servlets forum.

Moving . . .
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to be able to reuse a piece of code, don't put it in a servlet.
Create a bean or a plain old Java object that can be instanciated and have its methods called by your servlet.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farakh khan although its not recommended to mix the business logic with presentation,
if you want it that way, you need to write the entire html in your servlet code so as to generate the desired output.
Or include this in the output of another JSP.


Hope this helps
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yes UseAMeaningfulSubjectLine

Java functions is too generic to make sense.
 
Good night. Drive safely. Here's a tiny ad for the road:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic