无论如何,这是我的代码。
JSP页面
<%@ 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>
这是UserNameCheck类
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;
}
}
无论如何,我收到的错误如下
select name,password from mattroepracticetable where name='MattRoe' and password='MattRoe' java.sql.SQLException: Invalid column name
但是,如果我直接将SQL表达式复制并粘贴到SQLplus,它可以正常工作
我知道你应该在jspinit中初始化数据库连接,但我也认为你可以这样做。
如果有人可以解释我哪里出错了,请告诉我如何通过jspinit进行设置,或者只是如何将其设置为一般,我们将不胜感激。
答案 0 :(得分:4)
您的查询是:
select
name,password from mattroepracticetable
where
name='"+username+"' and password='"+password+"'"
但是你想从ResultSet中读到:
V = rslt.getString( "username" );
您应将其更改为:
V = rslt.getString( "name" );