Reputation: 285
Hey so I am a little confused at the moment. I have a few system.out.print code in here just so I can find out what the problem is. Anyway, here is the error I get, and it is caught in the try/catch that involves the actual SQL query.
Anyway here is my code.
JSP page
<%@ page import="webtest.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%String username = request.getParameter("username");
String password = request.getParameter("password");
UserNameCheck unc = new UserNameCheck();
%>
<H1>
<%=unc.LoginResults("select name,password from mattroepracticetable" +
" where name='"+username+"' and password='"+password+"' ")%>
</H1>
</body>
</html>
Here is the UserNameCheck class
package webtest;
import java.sql.*;
public class UserNameCheck{
DWConnect dwc = new DWConnect();
public String LoginResults(String sql){
Statement stmt;
ResultSet rslt;
System.out.println(sql);
String V=null;
try{
stmt = dwc.conn.createStatement();
System.out.print(sql);
rslt = stmt.executeQuery(sql);
if (rslt.next()){
V = rslt.getString("username");
System.out.print("hey");
}
else V = "Invalid UserName or Password";
System.out.print("not hey");
}
catch (Exception e){System.out.print(e);}
return V;
}
}
Anyway the error I am receiving is as follows
select name,password from mattroepracticetable where name='MattRoe' and password='MattRoe' java.sql.SQLException: Invalid column name
However if I directly copy and paste the SQL expression to SQLplus, it works fine.
I know you are supposed to initialize a Database Connection in the jspinit, but I also thought you could do it this way.
If someone could explain where I went wrong, tell me how I can set this up through jspinit, or just how I can set this up in general, would be appreciated.
Upvotes: 0
Views: 14181
Reputation: 23982
Your query is:
select
name,password from mattroepracticetable
where
name='"+username+"' and password='"+password+"'"
But from ResultSet you are trying to read:
V = rslt.getString( "username" );
You should change it to:
V = rslt.getString( "name" );
Upvotes: 4