Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 5
There are 1 users currently browsing forums.
JavaScript Coding JavaScript allows rich, browser-based web-applications with interactivity and complex visual effects. For discussion on JavaScript, use this forum.

Reply
  #1  
Old 07-07-2009
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
  #2  
Old 07-07-2009
New Born
 
Join Date: Jul 2009
Posts: 4
Rep Power: 0
jotmatta is on a distinguished road
Re: not able to connect to the MySQL DB..[NEWBIE]

UPDATE:
On my friends advice I tried the following code:
Code:
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
       try
       {
            String dbURL = "jdbc:mysql://localhost:3306/test";
            String username = "root";
            String password = "";
            Properties props = new Properties();
            props.put("user", "root");


            //Connection con = DriverManager.getConnection(dbURL, username, //password);
Connection con = DriverManager.getConnection(dbURL, props);
            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();
       }
    }
he said the password should not be blank... so I tried the above code... didnt work.. same o/p.. blank white screen no error msg...

changed the password for root in mysql.. so I dont have to leave password blank... but still no luck... same o/p...

everything else seems fine... just for the record, even when I tried using access as a db, I still was not able to connect..... tried DSN and DSN less connection both... didnt work....

when I tried access I wrote a sendRedirect in catch block... it sent me to the page I redirected to in the catch block..
when I do the same thing with MySQL as DB... it doesnt even redirect me to the catch blocks redirect...

I desperately need to connect to a db... be it MySQL or Access....
Reply With Quote
  #3  
Old 07-07-2009
New Born
 
Join Date: Jul 2009
Posts: 4
Rep Power: 0
jotmatta is on a distinguished road
Re: not able to connect to the MySQL DB..[NEWBIE]

someone plz help me..... really need to sort this one out...
Reply With Quote
  #4  
Old 07-17-2009
New Born
 
Join Date: Jul 2009
Posts: 4
Rep Power: 0
jotmatta is on a distinguished road
Re: not able to connect to the MySQL DB..[NEWBIE]

thts a shame... over a week since I posted this and not even a ingle reply till now.. other than my own replies... echo...

over 100 views and no one knows nethin in this regard???
cmon guys get out of ur sleep...
Reply With Quote
  #5  
Old 07-20-2009
New Born
 
Join Date: Jul 2009
Posts: 2
Rep Power: 0
fajiboy is on a distinguished road
Re: not able to connect to the MySQL DB..[NEWBIE]

Entering your php/mysql codes isn't the first step to connect to your database.The steps to take are ;

1. Create a table in 'mysql phpadmin' if u are making use of WAMP Server(note that phpadmin is located within the icon of your Wamp link. Also, if using WAMP server note that the server,hostname and password are "localhost", "root" and "" respectively).

2. Secondly enter your php/mysql codes into notepad and save as appropriate i.e in your wamp server.
If u still hv a problem with this, seek for the help of someone who can use WAMP server or send a reply to this forum.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collect 27 ebook learn php quyvphan PHP Articles 1 04-23-2009 05:52 PM