SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old October 15th, 2001, 02:22 PM
coach coach is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 94 coach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 10 sec
Reputation Power: 7
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

Reply With Quote
  #2  
Old October 15th, 2001, 04:35 PM
ACShafer ACShafer is offline
Not A Jedi Yet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: Salt Lake City
Posts: 8 ACShafer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ACShafer
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

Reply With Quote
  #3  
Old October 16th, 2001, 01:27 AM
coach coach is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 94 coach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 10 sec
Reputation Power: 7
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

Reply With Quote
  #4  
Old October 16th, 2001, 08:53 AM
ehsiung ehsiung is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: Pittsburgh, PA
Posts: 3 ehsiung User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old October 16th, 2001, 03:50 PM
ACShafer ACShafer is offline
Not A Jedi Yet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: Salt Lake City
Posts: 8 ACShafer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ACShafer
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.

Reply With Quote
  #6  
Old October 18th, 2001, 05:53 PM
sLiPkNoT rUlEz sLiPkNoT rUlEz is offline
PHP Coder
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Ontario
Posts: 66 sLiPkNoT rUlEz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to sLiPkNoT rUlEz Send a message via AIM to sLiPkNoT rUlEz Send a message via Yahoo to 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

Reply With Quote
  #7  
Old October 26th, 2001, 06:51 PM
Agent0range_ Agent0range_ is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: Victoria, BC.
Posts: 0 Agent0range_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > trying to understand JSP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway