servlet that connects to database
This is a simple servlet to test everything is working ok. If this fails you have configuration problems. Best to keep it simple here so its easier to debug any config problems.

1. Start kawa.
Create a new file (name it DBConnection.java) and copy the text below or download the files here.



import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class DBConnection extends HttpServlet
{
 public void doGet(HttpServletRequest req, HttpServletResponse res)
                     throws ServletException, IOException
 {
   res.setContentType("text/html");
    PrintWriter out = new PrintWriter(res.getOutputStream());
  try
  {
   Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        }
        catch (Exception E)
  {
         out.println("Unable to load driver.");
         E.printStackTrace();
        }                    
  try
  {
   Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/javatutes?user=username&password=passwd");            
            // Do something with the Connection
            try
   {
    // Use some connection we've already created
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT jtutes_users.user_name FROM jtutes_users");
    
    out.println("<html>");
             out.println("<head>");
             out.println("<title>search engine</title>");
             out.println("</head>");
    out.println("<body>");
    out.println("<br>test database connection");
    while (rs.next())
    {
     out.println("<br>debug user_name" + rs.getString("user_name"));
    }
    out.println("</body>");
          out.println("</html>");
    // Clean up after ourselves
                rs.close();
                stmt.close();
                conn.close();
      }
      catch (SQLException E)
   {
          out.println("SQLException: " + E.getMessage());
                out.println("SQLState:     " + E.getSQLState());
                out.println("VendorError:  " + E.getErrorCode());
            }
  }
        catch (SQLException E)
  {
         out.println("SQLException: " + E.getMessage());
   out.println("SQLState:     " + E.getSQLState());
   out.println("VendorError:  " + E.getErrorCode());
        }
        out.close();
 }
 public String getServletInfo()
    {
  return "Database connection servlet";   
}
}

2. Add a new context to tomcat (use the admin tool)
3. Upload the directories and add the web.xml config file.
\webapps\jtutes\WEB-INF\web.xml
\webapps\jtutes\WEB-INF\classes
\webapps\jtutes\jsp\
4. restart tomcat
5. update mod_jk.conf-auto if your using manual configuration (ie a copy of this file is imported into apache httpd.conf)
6. restart tomcat
7. add the class files to your new context.
8. test the changes, should work like this.
9. Add some data, lets create an access database on our local development machine and connect to the mysql database on the remote linux server (across the internet). Create an access linked table.

10. choose odbc connection.

11. choose machine data source.

12. Add all the tables and select save password.

13. Edit the mysql database and check the changes using the servlet above, too easy.


last java webhosting next
bl br
copyright