The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
trying to understand JSP
Discuss trying to understand JSP in the Java Help forum on Dev Shed. trying to understand JSP Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 15th, 2001, 02:22 PM
|
|
Contributing User
|
|
Join Date: Oct 2001
Posts: 94
Time spent in forums: 51 m 10 sec
Reputation Power: 12
|
|
|
trying to understand JSP
I've programmed using PHP and now I have a project that requires JSP. While there are some good similarities between PHP and JSP - I am finding some common tasks very tedious and wondered if someone might point me in the right direction.
I am trying to authenticate using MySQL - and while everything is ok so far - I get errors with the following code.
Code:
//user has entered email and password
<%@ page language="java" import="java.sql.*" %>
<%
if (request.getParameter("email").length() == 0 ||
request.getParameter("password").length() == 0) {
%>
<jsp:forward page="admin.jsp" >
<jsp:param name="errorMsg"
value="You must enter an email address and Password!" />
</jsp:forward>
<% } %>
<%
String conn;
Class.forName("org.gjt.mm.mysql.Driver");
// create connection string
conn = "jdbc:mysql://localhost/profile?user=user&password=pass";
// pass database parameters to JDBC driver
Connection Conn = DriverManager.getConnection(conn);
// query statement
Statement SQLStatement = Conn.createStatement();
String query = "SELECT * FROM auth_users WHERE email ='email' AND password='password' ";
// get result code
int SQLStatus = SQLStatement.executeUpdate(query);
if(SQLStatus != 0)
{
}
else
{
out.println("Error! Please try again.");
}
// close connection
SQLStatement.close();
Conn.close();
%>
First, I am getting syntax errors on my SQL statement to check "email' and "password" and second, what is the syntax to check if the email and password row is valid?
This is pretty easy in PHP - I have been pulling my hair out trying to find the syntax for JSP.
Any help would be appreciated.
Coach
__________________
A gentle push and a mild arc -
And the cowhide globe hit home
Hot Rod Hundley
|

October 15th, 2001, 04:35 PM
|
|
Not A Jedi Yet
|
|
Join Date: Aug 2001
Location: Salt Lake City
Posts: 8
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Coach
Hey Coach,
I didn't check all your code, but I can see your problem for the syntax errors on the SQL.
String query = "SELECT * FROM auth_users WHERE email ='email' AND password='password' ";
I'm sure if you read a bit about java Strings, you would figure it out in a snap.
query is being passed to the DB just as you have typed it, if you want the variables email and password to be interpreted you need to do something that looks like this:
String query = "SELECT * FROM auth_users WHERE email ='" + email + "' AND password='" + password + "' ";
I live in SLC, how are the Jazz going to do?
You can send me email if you have any more problems.
Andrew
|

October 16th, 2001, 01:27 AM
|
|
Contributing User
|
|
Join Date: Oct 2001
Posts: 94
Time spent in forums: 51 m 10 sec
Reputation Power: 12
|
|
|
duh of course
Thanks Andrew for the syntax tip.
I am learning quickly that the SQL is similar to PHP but not completely compatible.
I do have one more question regarding my code.
As I mentioned - I am trying to authenticate against a MySQL DB and wanted to know the correct syntax for creating a result that would check if the username and password exists - for example
Code:
// query statement
Statement SQLStatement = Conn.createStatement();
String query = "SELECT * FROM auth_users WHERE email ='" + email + "' AND password='" + password + "' ";
// get result code
ResultSet SQLResult = SQLStatement.executeQuery(query);
if(SQLResult!=null)
{
out.println("woo hoo.");
}
else
{
out.println("Error! Please try again.");
}
// close connection
SQLStatement.close();
Conn.close();
I know the result code is incorrect. What is the correct way to find out if the query is true or false?
thanks
Coach
|

October 16th, 2001, 08:53 AM
|
|
Junior Member
|
|
Join Date: Jul 2001
Location: Pittsburgh, PA
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Try this instead:
Code:
if(SQLResult.next())
{
out.println("woo hoo.");
}
else
{
out.println("Error! Please try again.");
}
Note: executeQuery() always returns a resultset. You need to check if the resultset is empty or not.
|

October 16th, 2001, 03:50 PM
|
|
Not A Jedi Yet
|
|
Join Date: Aug 2001
Location: Salt Lake City
Posts: 8
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Eric hooked you up.
Don't think of it as trying to learn JSP, learn Java and JSP will be cake.
Your question is really about JDBC.
In some ways, JSP is not as slick as PHP, (but hey, what is?) but Java is robust.
|

October 18th, 2001, 05:53 PM
|
|
PHP Coder
|
|
Join Date: May 2001
Location: Ontario
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
But then you get into the fun stuff like custom tags, MVC architecture, and templates and question yourself for ever doubting the robustness of JSP 
|

October 26th, 2001, 06:51 PM
|
|
Junior Member
|
|
Join Date: Oct 2001
Location: Victoria, BC.
Posts: 0
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: Originally posted by sLiPkNoT rUlEz
But then you get into the fun stuff like custom tags, MVC architecture, and templates and question yourself for ever doubting the robustness of JSP |
I second that. Taglibs and the MVC architechtire make webapps so much easier to maintain. Now, if only I could get those damn beans to work for me... 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|