Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2005, 10:01 AM   #1
BizzyC
Newbie
 
Join Date: Feb 2005
Location: Ireland
Posts: 5
Rep Power: 0 BizzyC is on a distinguished road
Cant connect servlet to MySQL DB

I've been working on this code, it's just a simple servlet to read a username and password from a html form and compare it to a db table..
package conn;

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



public class login extends HttpServlet 
{
	Connection connect = null;

    public void init (ServletConfig config) throws ServletException 
    {
        super.init(config);

        try {Class.forName("com.mysql.jdbc.Driver");}
        catch (ClassNotFoundException e) {}

        try 
        {
        	connect = DriverManager.getConnection("jdbc:mysql://localhost/Project","Viewer","view1");
        }
        catch (SQLException e) {}
    }

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
	{
    	response.setContentType("text/html");
    	PrintWriter out = response.getWriter();
		String title = "Reading All Request Parameters";
    	out.println(ServletUtilities.headWithTitle(title));
    	
    	String validResult = "";
    	boolean valid = false;
    	String[] paramValues = request.getParameterValues("UserName");
    	String UserName = paramValues[0];
    	String[] paramValues2 = request.getParameterValues("PassWord");
    	String PassWord = paramValues2[0];
    	out.println("<BODY>");

    	try
    	{
    		
    		Statement stmt = connect.createStatement();
      		BufferedReader bp = new BufferedReader( new InputStreamReader(System.in),1);
      		String query = "select * from accounts where UserName = '"+UserName+"' and PassWord ='"+PassWord+"' ";
      		
      		ResultSet rs = stmt.executeQuery(query);
      		
      		while(rs.next())
      		{
      			int ccount = rs.getMetaData().getColumnCount();
				for(int i=1;i<=ccount;i++)
				{
					vaidResult += (rs.getString(i)+"\t");
					
				}
      		}
      		
      		if(validResult.length() > 0)
      			valid = true;
      			
      		if(connect!=null)
      		{
      			try
      			{
      				connect.close();
      			}
      			
      			catch(Exception e)
      			{
      				
      			}
      		}      		
    	}
    	catch( Exception x ) 
    	{
      		x.printStackTrace();
    	}
    
    	out.println(UserName+" "+PassWord);
    	out.println(valid+"</BODY></HTML>");
  
    }
    
      	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
  	{
    	doGet(request, response);
  	}
}

I know it's running through cause the </html> tags are being printed properly , but it's not connecting to the DB and i can't figure out why.
The code reads in the right usernames and passwords, and if i put the connection code into it's own main method it'll work.
I'm really stumped, I'd appretiate any help.
Thanks

Last edited by BizzyC; Mar 8th, 2005 at 10:04 AM.
BizzyC is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC