運行我的代碼時,出現以下例外:
java.sql.SQLSyntaxErrorException:列 'ASD' 不在 FROM 串列中的任何表中,或者出現在連接規范中并且超出連接規范的范圍,或者出現在 HAVING 子句中并且不在 GROUP BY 串列中。如果這是 CREATE 或 ALTER TABLE 陳述句,則“ASD”不是目標表中的列。這是為例外 e 列印的錯誤
這是我的代碼:
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionUrl = "jdbc:derby://localhost:1527/";
String database = "EmployeeDB";
String DBid = "app";
String DBpass = "app";
<%
try{
Connection connection = DriverManager.getConnection(connectionUrl database, DBid, DBpass);
Statement stt=connection.createStatement();
//String sql ="select * from USERPROFILE where username=" Sname;
String sql ="select * from USERPROFILE where username=" Sname;
out.print("<br>4Welcome to Session Page: SQL " sql);
out.print("<br>5Welcome to Session Page: result " Spass);
ResultSet resultSQL = stt.executeQuery(sql);
out.print("<br>6Welcome to Session Page: result " Spass);
while(resultSQL.next()){
out.print("<br>7Welcome to Session Page: Name " Sname " Pass " Spass);
%>
<!DOCTYPE html>
<html>
<body>
<button onclick="history.back()">Go Back</button>
<h1>Update data from database in jsp</h1>
<form method="post" action="update-process.jsp">
<br>
<input type="hidden" name="id" value="<%=resultSQL.getString("id") %>">
Username:<br>
<input type="text" name="username" value="<%=resultSQL.getString("username") %>">
<br>
Password:<br>
<input type="text" name="password" value="<%=resultSQL.getString("password") %>">
<br>
Contact:<br>
<input type="text" name="contact" value="<%=resultSQL.getString("contact") %>">
<br>
Email:<br>
<input type="text" name="password" value="<%=resultSQL.getString("email") %>">
<br>
Work hour per Week<br>
<input type="text" name="workhour" value="<%=resultSQL.getString("workhour") %>">
<br>
Reward:<br>
<input type="text" name="reward" value="<%=resultSQL.getString("reward") %>">
<br>
<br><br>
<input type="submit" value="submit" onclick="return confirm('Are you sure you want to update?');">
</form>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
uj5u.com熱心網友回復:
在您的代碼中,Sname是字串,那么查詢應該是SELECT * FROM USERPROFILE WHERE username='" Sname "'". 對于整數使用引號,如" Integer "字串,使用引號,如'" String "'
下面是您的代碼中的一些錯誤。
- 此查詢不安全,您必須使用它
Parameterized Queries來保護您的資料。 - 為避免任何語法錯誤,您必須
PreparedStatement改用Statement. - 將所有代碼放在
<form>標簽內,因為如果代碼在標簽外,<form>則不允許您以表單形式提交。
下面是修改后的代碼Parameterized Queries。
<!DOCTYPE html>
<html>
<body>
<button onclick="history.back()">Go Back</button>
<h1>Update data from database in jsp</h1>
<form method="post" action="update-process.jsp">
<%
try{
String sql ="SELECT * FROM USERPROFILE WHERE username = ?";
Connection connection = DriverManager.getConnection(connectionUrl database, DBid, DBpass);
PreparedStatement stt = connection.prepareStatement(sql);
stt.setString(1, Sname);
out.print("<br>4Welcome to Session Page: SQL " sql);
out.print("<br>5Welcome to Session Page: result " Spass);
ResultSet resultSQL = pst.executeQuery();
out.print("<br>6Welcome to Session Page: result " Spass);
while(resultSQL.next()){
out.print("<br>7Welcome to Session Page: Name " Sname " Pass " Spass);
%>
<br>
<input type="hidden" name="id" value="<%=resultSQL.getString("id") %>">
Username:<br>
<input type="text" name="username" value="<%=resultSQL.getString("username") %>">
<br>
Password:<br>
<input type="text" name="password" value="<%=resultSQL.getString("password") %>">
<br>
Contact:<br>
<input type="text" name="contact" value="<%=resultSQL.getString("contact") %>">
<br>
Email:<br>
<input type="text" name="password" value="<%=resultSQL.getString("email") %>">
<br>
Work hour per Week<br>
<input type="text" name="workhour" value="<%=resultSQL.getString("workhour") %>">
<br>
Reward:<br>
<input type="text" name="reward" value="<%=resultSQL.getString("reward") %>">
<br>
<br><br>
<input type="submit" value="submit" onclick="return confirm('Are you sure you want to update?');">
<%
}
sst.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/427209.html
標籤:爪哇 sql jsp netbeans-8
上一篇:如何通過spring mybatis框架實作配置使用oracle/postgreSQLdataSource?
下一篇:如何在JSP中為字串結果著色?
