0
嘿,所以我現在有點困惑。我在這裏有一些system.out.print代碼,這樣我就可以找出問題所在。無論如何,這是我得到的錯誤,並且它被捕獲在涉及實際SQL查詢的try/catch中。JDBC和JSP數據庫連接
無論如何,這裏是我的代碼。
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設置它,或者我可以如何設置它,我將不勝感激。
+1。你是對的。我的回答是錯誤的。刪除。 – kosa 2012-07-10 15:11:18
謝謝。不能相信我錯過了這樣一個愚蠢的錯誤。 現在一切正常完美 – Matt 2012-07-10 15:14:44
@Matt勾選接受鏈接。 :) – 2012-07-10 15:17:57