简体   繁体   English

JDBC和JSP数据库连接

[英]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. 我这里有一些system.out.print代码,所以我可以找出问题所在。 Anyway, here is the error I get, and it is caught in the try/catch that involves the actual SQL query. 无论如何,这是我得到的错误,它被包含在涉及实际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. 但是,如果我直接将SQL表达式复制并粘贴到SQLplus,则可以正常工作。
I know you are supposed to initialize a Database Connection in the jspinit, but I also thought you could do it this way. 我知道您应该在jspinit中初始化数据库连接,但是我也认为您可以通过这种方式进行。
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. 如果有人可以解释我哪里出了问题,请告诉我如何通过jspinit进行设置,或者只是我通常如何进行设置即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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