Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

jsp: Connectivity issue using jsp and mysql in netbeans

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey friends,

Anybody can help me to sort out my problem in getting connected to mysql database using jsp in netbeans. I have a login page and i want to login using uid and pwd, when i enter it displays invalid username and password although i have inserted uid and pwd in table called 'login' in mysql!!! Here I have the following code---


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.lang.*" %>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login Page</title>
</head>
<body>
<%!
Connection con;
Statement st;
ResultSet rs;
String uid,pwd;

%>
<%
try
{
uid=request.getParameter("uid"); // uid is text field name of html page
pwd=request.getParameter("textfield"); // textfield is text field name of html page
uid=uid.trim();
pwd=pwd.trim();

Class.forName("com.mysql.jdbc.Driver");


con=DriverManager.getConnection("jdbc:mysql://localhost:3306/contact","root",""); // 'contact' database name

if(con==null)
{
out.println("Connection not opened");
}
else
{
out.println("Connection opened");
out.println("DB is connected");


st=con.createStatement();
String q="select * from login where username='"+uid+"' and password='"+pwd+"'";


rs=st.executeQuery(q);


while(rs.next())
{

%>

<jsp:forward page="Developers login page.html"></jsp:forward>

<%
}


out.println("Sorry...Invalid username and password.Please try again later");
}
}
catch(Exception e)
{
out.println(e.toString());
}

%>
</body>
</html>


instead of going to "Developers login page.html" its displaying error message as "Sorry...Invalid username and password.Please try again later" !!!


please help me in fixing out this issue! It will be very helpfull...
regards :)
 
Marshal
Posts: 67415
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: move all that code out of a JSP and into a Java class where it belongs! JSP is for view generation, not control.

If this is part of an authentication system, first you should find previous discussions where Tim will advise you not to roll your own. But it you insist, this sort of check should be done in filters.
 
Deepika Choudhari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks for the suggestion,but previously I have used jsp code for bussiness logic and it run very well but there I have used Oracle for Database,this time I m using mysql but its showing error. I am developing a live website for the first time,so I am not understanding how to connect to Database.
Thanks for the reply!
 
Bear Bibeault
Marshal
Posts: 67415
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me know when you have moved the code into a Java class and we can look at the it in its new home.
 
Die Fledermaus does not fear such a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic