#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    94
    Rep Power
    14

    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
  2. #2
  3. No Profile Picture
    Not A Jedi Yet
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Salt Lake City
    Posts
    8
    Rep 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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    94
    Rep Power
    14

    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
    A gentle push and a mild arc -
    And the cowhide globe hit home

    Hot Rod Hundley
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Pittsburgh, PA
    Posts
    3
    Rep 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.
  8. #5
  9. No Profile Picture
    Not A Jedi Yet
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Salt Lake City
    Posts
    8
    Rep 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.
  10. #6
  11. No Profile Picture
    PHP Coder
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Ontario
    Posts
    66
    Rep Power
    14
    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
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Victoria, BC.
    Posts
    0
    Rep Power
    0
    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...

IMN logo majestic logo threadwatch logo seochat tools logo