View Single Post
  #1  
Old 07-07-2009
jotmatta jotmatta is offline
New Born
 
Join Date: Jul 2009
Posts: 4
Rep Power: 0
jotmatta is on a distinguished road
not able to connect to the MySQL DB..[NEWBIE]

hi everyone this is my first post on the forum.... am a very amatuer coder... just learnt basic java and have moved to making some web based app using JAVA Servlets...

here is my code.. dont know where am I goin wrong while connecting to DB..

My home page which calls the servlet:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
 </HEAD>
 <BODY>
  <center><p><a href="http://localhost:8080/Login1">servlet</a></p></center>
 </BODY>
</HTML>
My Servlet:
Code:
import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Login1 extends HttpServlet
{
   Connection con;
   ResultSet rs;

   public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
       try
       {
            String dbURL = "jdbc:mysql://localhost:3306/test";
            String username = "root";
            String password = "";
            Connection con = DriverManager.getConnection(dbURL, username, password);
            Statement statement = con.createStatement();
            String sql = "SELECT * FROM LOGIN";
            rs = statement.executeQuery(sql);
            if(rs.next())
            {
                res.sendRedirect("http://localhost:8080/welcome.html");
            }
            else
            {
                res.sendRedirect("http://localhost:8080/invalid.html");
            }
            con.close();
       }
       catch (Exception e)
       {
            e.printStackTrace();
       }
    }
}
edited the web.xml file in the WEB-INF folder..
its looks like this..
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="Java 2 Platform, Enterprise Edition (J2EE) : XML Schemas for J2EE Deployment Descriptors"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="Java 2 Platform, Enterprise Edition (J2EE) : XML Schemas for J2EE Deployment Descriptors http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

<!-- JSPC servlet mappings start -->

    <servlet>
        <servlet-name>org.apache.jsp.index_jsp</servlet-name>
        <servlet-class>org.apache.jsp.index_jsp</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>org.apache.jsp.index_jsp</servlet-name>
        <url-pattern>/index.jsp</url-pattern>
    </servlet-mapping>   
    <servlet>
        <servlet-name>Login1</servlet-name>
        <servlet-class>Login1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Login1</servlet-name>
        <url-pattern>/Login1</url-pattern>
    </servlet-mapping>
<!-- JSPC servlet mappings end -->

</web-app>
I created a table login under the database "test". there no password in my MySQL server... have all the required file eg. invalid.html etc. under my root directory... created the classes folder under WEB-INF and placed the Login1.class file under it (got no error while compiling Login1.java) ...cannnot find out whats the problem...

I get no output... a plain blank white page with no error message... and the url reads like http://127.0.0.1:8080/Login1

using Apache tomcat 5.5 and Sun JAVA 6 jdk...
plz help me out with this... once I get the db connetivity done I can then proceed to make my project.... have to submit it by 15th of this month... urgent help required people...
Reply With Quote