![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Location: Ireland
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|