简体   繁体   中英

JDBC and JSP Database connection

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.

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" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM