FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Servlets





akshar
I want code of a simple servlet that will connecto a a remote database and show the contents of one tables.
I run tomacat server.
poppitz
Hi,

something like the following should work. Depending on the database you use, you have to use another jdbc driver. This code is for mysql.
You have to adjust the serverName, database, user and pwd.
Code:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.sql.*;
import java.io.*;

public class DBServlet extends HttpServlet {
   
   public void doGet(HttpServletRequest request, HttpServletResponse response){
       try {
         PrintWriter out = response.getWriter();
         Connection connection = null;
          // Load the JDBC driver
           String driverName = "com.mysql.jdbc.Driver";
           Class.forName(driverName);
           // Create a connection to the database
           String serverName = "herc333.chaos.loc";
           String mydatabase = "test";
           String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
           String username = "flo";
           String password = "garkeins";
           connection = DriverManager.getConnection(url, username, password);
         String query = "SELECT * FROM <TABLE>";
         PreparedStatement stmt = connection.prepareStatement(query);
         ResultSet rs = stmt.executeQuery();
         while( rs.next()){
            out.println(rs.getInt("col1") + "  " + rs.getString("col2") + "<br>");
         }
         stmt.close();
         connection.close();          
           }
       catch (ClassNotFoundException e) {
           System.out.println("Could not find the database driver.");
           }
       catch (SQLException e) {
           System.out.println("Could not connect to the database.");
       }
       catch (IOException e) {
           System.out.println("Could not get PrintWriter.");
       }
   }
}


You can find jdbc drivers here: http://developers.sun.com/product/jdbc/drivers
akshar
Thanks man but can you just tell me is it enough to have apache tomact installed? Or will I need Sun's Application server?

Where do I save the databse driver and where do I save teh servlet?
poppitz
Hi,

you should put the database driver into WEB-INF\lib or into <Tomcat Home>\shared\lib if you want to use it in further webapps.

You have to put the servlet into WEB-INF\classes and adapt you Web.xml like:
Code:

    <servlet>
        <servlet-name> name for servlet </servlet-name>
      <servlet-class> package.class </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name> name for servlet </servlet-name>
        <url-pattern> /servlet1 </url-pattern>
    </servlet-mapping>


And then you can call the servlet with http://hostname/servlet1

It seems that you have not built a webapp before, so I recommend you to read the Tomcat docu. It describes the development and deployment of web applications very well.
[url]
http://tomcat.apache.org/tomcat-5.5-doc/index.html
[/url]

Have fun with your Servlets!
akshar
that was more that helpful
Related topics
Java
Problems with Java
Java hosting
Java experts help us who know nothing
What's the best SERVLET ENGINE
Online Notepad
AJAX or servlets?
Movable windows on a web page.
Best Programming Language to Use
jsp and java servlet
Java Servlets vs PHP
Is Java Servlets supported
How about servlets or jsp?
Servlets or JSP
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.