|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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
![]() |
|
#7
|
|||
|
|||
|
Quote:
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... ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > trying to understand JSP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|