Java 找不到双引号来关闭字符串
“字符串文字未被双引号正确关闭” 我在查找上述问题时遇到问题。我在代码中到处搜索,但仍然无法识别错误。它说错误出现在第12行 这是我的密码: 我正在用Java 找不到双引号来关闭字符串,java,html,string,jsp,Java,Html,String,Jsp,“字符串文字未被双引号正确关闭” 我在查找上述问题时遇到问题。我在代码中到处搜索,但仍然无法识别错误。它说错误出现在第12行 这是我的密码: 我正在用JSP编码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <
JSP
编码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%
response.addHeader("Cache-Control","no-cache");
response.addHeader("Pragma","no-cache");
response.addHeader("Expires","0");
// Login variables //
String username = request.getParameter("username");
String password = request.getParameter("password");
// SQL login variables //
// Role variables //
// Encryption of session ID //
String sessId = "" + (new java.util.Random()).nextInt(1000);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conURL= "jdbc:odbc:HOD_DATA";
Connection con = DriverManager.getConnection(conURL);
Statement st = con.createStatement();
String query = "select user_group from login
where username = '"+username+"'
and password = '"+password+"'
";
ResultSet rs = st.executeQuery(query);
Boolean result = rs.next();
// Might have to pass the login details over to the other pages //
if(result == true){
// Ashton //
if(rs.getString("user_group").equals("admin")){
String url = response.encodeURL("Test.jsp?sessId=" + sessId);
response.sendRedirect(url);
}
// Joseph //
else if(rs.getString("user_group").equals("clerk")){
String url = response.encodeURL("ClerkPage.jsp?sessId=" + sessId);
response.sendRedirect(url);
}
// Gaurav //
else if(rs.getString("user_group").equals("customer")){
String url = response.encodeURL("HomePage.jsp?sessId=" + sessId);
response.sendRedirect(url);
}
// Jiong xu //
else if(rs.getString("user_group").equals("sales")){
String url = response.encodeURL("SalesPage.jsp?sessId=" + sessId);
response.sendRedirect(url);
}
// Wen jie //
else if(rs.getString("user_group").equals("operations")){
String url = response.encodeURL("OperationsPage.jsp?sessId=" + sessId);
response.sendRedirect(url);
}
}
else {
response.sendRedirect("LoginPage.jsp"); // If the above fails, redirect to login //
}
rs.close();
st.close();
con.close();
%>
</body>
</html>
很可能是这一部分:
String query = "select user_group from login
where username = '"+username+"'
and password = '"+password+"'
";
在java中字符串的中间不允许行中断。 很可能是这个部分:
String query = "select user_group from login
where username = '"+username+"'
and password = '"+password+"'
";
在java中字符串的中间不允许行中断。 首先,您运行查询的方式有点太老,太危险了,最好使用<代码>准备好的语句< /C> >这里,像这个< /P>
PreparedStatement st = con.prepareStatement("select user_group from login where username=? and password=?");
st.setString(1,username);
st.setString(2,password);
ResultSet rs = st.executeQuery();
其次,如果您使用的是例如
postgresql
数据库,那么最好使用它自己的隐式驱动程序,而不是它的ODBC
驱动程序,以获得更好的性能。首先,您运行查询的方式有点太旧太危险,最好在这里使用准备好的语句,就像这样
PreparedStatement st = con.prepareStatement("select user_group from login where username=? and password=?");
st.setString(1,username);
st.setString(2,password);
ResultSet rs = st.executeQuery();
第二,如果你正在使用例如postgresql
数据库,最好使用它自己的隐式驱动程序,而不是它的ODBC
驱动程序,以获得更好的性能。你相信SQL注入
吗?你相信SQL注入
吗?我相信我的学校仍然在教我们旧的教学大纲。谢谢你向我强调了更现代的做事方法:)我会记住这一点,我相信我的学校仍然在教我们旧的教学大纲。谢谢你向我强调了更现代的做事方式:)我会记住的