java JDBC 和 JSP 数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11416292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:01:22  来源:igfitidea点击:

JDBC and JSP Database connection

javasqljspjdbc

提问by Matt

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.

嘿,所以我现在有点困惑。我这里有一些 system.out.print 代码,这样我就可以找出问题所在。无论如何,这是我得到的错误,它在涉及实际 SQL 查询的 try/catch 中被捕获。

Anyway here is my code.

无论如何,这是我的代码。

JSP page

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>

Here is the UserNameCheck class

这是 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;
}
}

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.

但是,如果我直接将 SQL 表达式复制并粘贴到 SQLplus,它就可以正常工作。
我知道你应该在 jspinit 中初始化一个数据库连接,但我也认为你可以这样做。
如果有人能解释我哪里出错了,告诉我如何通过 jspinit 设置它,或者我如何设置它,将不胜感激。

回答by Ravinder Reddy

Your query is:

您的查询是:

select
   name,password from mattroepracticetable
where 
   name='"+username+"' and password='"+password+"'"

But from ResultSet you are trying to read:

但是从 ResultSet 您试图阅读:

V = rslt.getString( "username" );

You should change it to:

您应该将其更改为:

V = rslt.getString( "name" );