我需要实现一个数据库查询,它会返回给玩家的用户名,得分和排名。由于我没有直接存储在数据库中的等级,我必须通过玩家的得分来计算它。
我无法获得SQL语法错误。
有人可以告诉我如何使用java servlet进行数据库连接。并在jsp中显示结果。
我使用MySql 5.6.21和Tomcat 7。
有人可以帮助我吗?
现在我在Traceback中遇到一个新的错误我确定这是因为准备好的语句是错误的,但我该如何解决这个问题。对我来说,在java servlet中我会更容易做到这一点,但我需要直接在这个页面上得到结果。
<!DOCTYPE html>
<html lang="en">
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Nicolas Almy">
<title>Sweep-It | Ranking</title>
<!-- Custom CSS -->
<link href="css/screen.css" rel="stylesheet">
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<div id="navbar-outer">
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="ranking.jsp">Ranking</a>
</li>
<li>
<a href="howtoplay.html">How to play</a>
</li>
<li>
<a href="aboutus.html">About us</a>
</li>
</ul>
<!--
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Register</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul> -->
</div>
</div>
</nav>
</div>
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-sm-12" id="welcome-text">
<div class="box">
<p class="box-text">On this page you can see which ones are the top players at the moment. If you are logged in you can see your own statics.</p>
<!-- SELECT * from user Order by rating DESC; -->
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/sweepit"
user="root" password="AbA011"
/>
<c:set var="prevValue" value="NULL" />
<c:set var="rankCount" value="0" />
<sql:query dataSource="${snapshot}" var="result">
SELECT UserId, Rating, Username,
CASE
WHEN prevValue = Rating THEN rankCount
WHEN prevValue = Rating THEN rankCount = rankCount + 1
END AS rank
FROM user
ORDER BY Rating
<sql:param value="$prevalue" />
<sql:param value="$rankCount" />
</sql:query>
<div id="table-div">
<table id="table-ranking" border="1" >
<tr>
<th>Rank</th>
<th>Username</th>
<th>Score</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr class="box-text">
<td><c:out value="${row.rank}"/></td>
<td><c:out value="${row.username}"/></td>
<td><c:out value="${row.rating}"/></td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</body>
</html>
create table `User` (
`UserId` int auto_increment NOT NULL,
`Username` varchar(25) NOT NULL,
`RegistrationDate` datetime NOT NULL DEFAULT NOW(),
`Email` varchar(50) NOT NULL,
`Authorization` enum('Admin', 'Default', 'Guest') NOT NULL,
`Rating` int NOT NULL DEFAULT 0,
`Password` varchar(32) NOT NULL,
PRIMARY KEY (`UserId`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=UTF8;
Apr 10, 2015 8:58:56 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/sweepit] threw exception [javax.servlet.ServletException: javax.servlet.jsp.JspException:
SELECT UserId, Rating, Username,
CASE
WHEN prevValue = Rating THEN rankCount
WHEN prevValue = Rating THEN rankCount = rankCount + 1
END AS rank
FROM user
ORDER BY Rating
: Parameter index out of range (1 > number of parameters, which is 0).] with root cause
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3711)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3695)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4546)
at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:3950)
at org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.setParameters(QueryTagSupport.java:304)
at org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.doEndTag(QueryTagSupport.java:223)
at org.apache.jsp.ranking_jsp._jspx_meth_sql_005fquery_005f0(ranking_jsp.java:352)
at org.apache.jsp.ranking_jsp._jspService(ranking_jsp.java:184)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
您忘记了用户名和CASE之间的SQL查询中的逗号。
SELECT UserId, Rating, Username,
CASE
WHEN prevValue = Rating THEN rankCount
WHEN prevValue := Rating THEN rankCount := rankCount + 1
END AS rank
FROM user
ORDER BY Rating