Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2005, 03:54 AM
Registered User
 
Join Date: Oct 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default servlet accessing MySql ....error

hi friends,
          I am new at servlets and have attempted to make a connection with MySql but i get a new error each time i try to execute the stuff...i m placing my code here...please reply at the earliest if anybody is able to spot the error or has another code to execute the same...thanx!!

The code........

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
import java.sql.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;


public class Sample2 extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
        response.setContentType("text/html");
                PrintWriter out = response.getWriter();

        try
        {

            String host = "localhost:8080";
            String database="ans";
            String driver = com.mysql.jdbc.Driver.class.getName();
            String dbName = "jdbc:mysql://" + host + "/" +database ;
            String userName = "root";
            String password = "root";

            Class.forName(driver);

            Connection conn = DriverManager.getConnection(dbName, userName, password);
            Statement statement = conn.createStatement();
            String query="Select * from test";
            ResultSet rs=statement.executeQuery(query);
            printResultSet(response,rs);
            rs.close();
            statement.close();
            conn.close();

        }

        catch(SQLException ex)
        {
            out = response.getWriter();
            response.setContentType("text/html");

        out.println ("SQL Exception: " + ex.getMessage ());
        }
        catch (Exception ex)
        {
            out = response.getWriter();
        response.setContentType("text/html");
        out.println ("Exception: " + ex.getMessage ());
    }
 }


     private void printResultSet ( HttpServletResponse resp,ResultSet rs ) throws SQLException, IOException
     {
    try
    {
        PrintWriter out = resp.getWriter();
            out.println("<html>");
            out.println("<head><title>jbs jdbc/mysql servlet</title></head>");
            out.println("<body>");
            out.println("<center>");
            out.println("<h3>jbsJDBCServlet</h3>");
            out.println("<h3>Data Retrieved:</h3>");
            out.println("<table border='1'>");
               int numCols = rs.getMetaData().getColumnCount ();

                while ( rs.next() )
                {
             out.println("<tr>");
                    for (int i=1; i<=numCols; i++)
                    {
                            out.print("<td>" + rs.getString(i) + "</td>" );
                        }
                out.println("</tr>");
                }
        out.println("</table>");
        out.println("</center>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    catch ( IOException except)
    {
    }
  }
}




ans4u
 
Old January 17th, 2006, 03:41 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You seem to be trying to access the database on port 8080, which is usually the port Tomcat runs on. I believe you'd have more luck connecting to it on port 3306, MySQL's default port.

Jon Emerson
Computer Scientist
Adobe Systems, Inc.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can JSP integrate with Servlet and run on MySQL wesleygch JSP Basics 0 April 10th, 2007 02:22 AM
error while compiling servlet in Weblogic8.1 adityasharma003 Servlets 0 April 12th, 2006 05:30 AM
Accessing MySql db from a Servlet(Urgent) balarkavelidi Apache Tomcat 1 January 15th, 2006 10:03 AM
Accessing MySQL through Excel? Mantis MySQL 1 April 12th, 2005 06:43 AM
Accessing Java Servlet thru DotNet r_ganesh76 General .NET 0 June 23rd, 2004 11:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.