为什么这个查询rs=st.executeQuery(query)
;没有执行从数据库中选择一个表?
String gender = request.getParameter("gender");
if (gender != null) {
String table = gender.equals("teacher") ? "teacher2" : "student";
String query ="select username,password from "+table+" where username='"+name+"' AND password='"+abc+"'";
rs=st.executeQuery(query); //Why This statement having error
}
该表的{p> Mysql查询 enter image description here我认为此查询错误
"select username,password from "+table+" where username='"+name+"' AND password='"+abc+"'";
我有两个表,一个用于教师,一个用于学生,两个表都具有相同数据类型的相同列。
如何从mysql数据库中选择表来登录。我有两个表,一个用于学生,一个用于老师,如果用户选择老师,单选按钮并输入用户名和密码如果用户用户名和密码等于mysql数据库用户名和密码他将获得登录相同的情况是学生如果他选择学生如果用户名,密码等于mysql用户名的学生表,他将登录密码,输入用户名和密码。
为什么不执行此查询以选择表格
rs=st.executeQuery(query);
错误图片
enter image description here //错误
index.jsp
<form method="GET " action="statement.jsp" autocomplete="on">
<input id="username" name="username" required="required" type="text" placeholder="Username"/>
<input id="password" name="password" required="required" type="password" placeholder="Password" />
<input type="radio" name="gender" value="teacher" checked/> Teacher
<input type="radio" name="gender" value="Student"/>Student
<input type="submit" value="Login" />
</form>
statement.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.mysql.jdbc.Statement"%>
<%@page import="com.mysql.jdbc.Connection"%>
<%@page import=" java.sql.SQLException" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="db conn.jsp" %>
<%
String name=request.getParameter("username");
String abc=request.getParameter("password");
String gender = request.getParameter("gender");
if (gender != null) {
String table = gender.equals("teacher") ? "teacher2" : "student";
String query ="select username,password from "+table+" where username='"+name+"' AND password='"+abc+"'";
rs=st.executeQuery(query); //Why This statement having error
}
if(rs.next())
{
response.sendRedirect("main.jsp");
}
else
{
response.sendRedirect("index.jsp");
}
%>
db conn.jsp //对于数据库连接
<%@page import="com.mysql.jdbc.Connection"%>
<%@page import="com.mysql.jdbc.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%@ page import ="java.sql.*" %>
<%
Connection c1 = null;
Statement st = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
c1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root", "abcde");
{
System.out.println("Couldn't find the driver!");
System.out.println("Couldn't connect: print out a stack trace and exit.");
System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
st = (Statement) c1.createStatement();
System.out.println("Statement Created Successfully");
{
System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
}
if (c1!= null) {
System. out.println("Hooray! We connected to the database!");
} else {
System.out.println("We should never get here.");
}}
%>
错误
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /statement.jsp:29
26: String table = gender.equals("teacher") ? "teacher2" : "student";
27: // replace dots with your values
28: String query ="select * from " +table+ "where username='"+name+"' AND password='"+abc+"'";
29: st.executeQuery(query);
30:
堆栈跟踪:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
为什么此查询不从mysql数据库中选择表。我在mysql中有两个表,一个是学生,另一个是teacher2,它们都具有相同的数据类型。如果用户选择学生的单选按钮,那么如果用户选择教师,单选按钮,则会显示学生表用户名和密码,然后将显示teacher2用户名和密码以获得登录
答案 0 :(得分:1)
在你的字符串连接中,你忘了在一些工作结尾处放置空格,比如
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.footer {
position: absolute;
/*this controls how far bottom the banner goes*/
top: 110%; <---- THIS WAS THE MISSIN PIECE
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
background-color: #f5f5f5;
}
/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */
body > .container {
padding: 60px 15px 0;
}
.container .text-muted {
margin: 20px 0;
}
.footer > .container {
padding-right: 15px;
padding-left: 15px;
}
code {
font-size: 80%;
}
防止在查询中发生这种情况的好方法是使用PreparedStatements。在处理变量时使用它们是一种好习惯。您可以使用这样的PreparedStatements:
'from'+ table //'fromteacher2'
结论,您在SQLquery中只有一个简单的语法错误,可以通过添加空格或使用(这是一个好习惯,只需这样做)来解决PreparedStatements