aspose file tools*
The moose likes JSP and the fly likes jsp: Connectivity issue using jsp and mysql in netbeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp: Connectivity issue using jsp and mysql in netbeans" Watch "jsp: Connectivity issue using jsp and mysql in netbeans" New topic
Author

jsp: Connectivity issue using jsp and mysql in netbeans

Deepika Choudhari
Greenhorn

Joined: Jul 17, 2013
Posts: 7
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 :)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Deepika Choudhari
Greenhorn

Joined: Jul 17, 2013
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

Let me know when you have moved the code into a Java class and we can look at the it in its new home.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp: Connectivity issue using jsp and mysql in netbeans