[英]Get column data in JavaDB via foreign key
Outset: I am building a page in JSP / EJB (Java EE 8) that displays 2 tables: 1 for every user registerd in the database with their name and score and another one that displays all pending challenges to rock-paper-scisors for the currently logged in user. 开始:我在JSP / EJB(Java EE 8)中构建一个页面,显示2个表:1表示在数据库中注册的每个用户的名称和分数,另一个表示所有未决的岩石纸张的挑战。 目前登录用户。
Databases: So far I have 2 tables in that can be access via the " LOCALHOST. " prefix: Usercredentials and Pending_Challenges. 数据库:到目前为止,我有2个表可以通过“ LOCALHOST。 ”前缀访问:Usercredentials和Pending_Challenges。 Both were created using the following code:
两者都是使用以下代码创建的:
Database: JavaDB 数据库:JavaDB
Usercredentials: Usercredentials:
CREATE TABLE USERCREDENTIALS (
username varchar(10) primary key,
password varchar(10) not null
);
Yes, yes I know these are not the best fields to use but I blame my team mate! 是的,是的,我知道这些不是最好的领域,但我责备我的队友! :)
:)
Pending_Challenges: Pending_Challenges:
CREATE TABLE PENDING_CHALLENGES (
challenge_id integer primary key,
from_user varchar(10) references Usercredentials(username),
to_user varchar(10) references Usercredentials(username),
);
The tables hold the following data so far: 到目前为止,表格包含以下数据:
Username | Password | Score
---------------------------
tom | tom | 5
mo | mo | 2
jon | jon | 0
Challenge_ID | From_User | To_User
----------------------------------
1 | tom | mo
2 | jon | tom
I call on these tables in a file named ListUserServlet.java where I run 2 queries. 我在名为ListUserServlet.java的文件中调用这些表,在那里运行2个查询。 Each one is to create a different set of results (code below ofc) to be printed in 2 different tables.
每一个都要创建一组不同的结果(下面的代码为c),以便在两个不同的表格中打印。
{... working Java code...}
System.out.println("Creating EMF query for userList");
String query = "SELECT U FROM Usercredentials U ORDER BY U.score DESC";
List users = em.createQuery(query).getResultList();
request.setAttribute("userList", users");
{... working Java code...}
In my .jsp file that then displays the table, I get the following (don't mind the ID tags, we're changing that): 在我的.jsp文件中然后显示表,我得到以下内容(不介意ID标记,我们正在改变它):
<h3>Current rankings:</h3>
<form id="challengeUserForm" action="ChallengeUser" method="POST">
<table id="userListTable" border="3">
<tr >
<th>Username</th>
<th>Challenge others</th>
<th>Score</th>
</tr>
<c:forEach var="user" begin="0" items="${requestScope.userList}">
<tr>
<td>${user.username} </td>
<td><input type="submit" id="${user.username}" value="Challenge!"></td>
<td>${user.score}</td>
</tr>
</c:forEach>
</table>
Using this approach, I also managed to print out ALL of the currently held challenges in that table by using: 使用这种方法,我还设法通过使用以下方式打印出该表中当前持有的所有挑战:
{... working Java code...}
query = "SELECT P FROM PendingChallenges P";
List chall = em.createQuery(query).getResultList();
request.setAttribute("pendingList", chall);
{... working Java code...}
Now, using the same logic, this is how I print the table: 现在,使用相同的逻辑,这是我打印表格的方式:
<h3>Pending challenges:</h3>
<form id="acceptChallengeForm" action="AcceptChallenge" method="POST">
<table id="pendingTable" border="3">
<tr>
<th>Username</th>
<th>Accept challenge</th>
</tr>
<c:forEach var="pending" begin="0" items="${requestScope.pendingList}">
<tr>
<td>${pending.fromUser.username} </td>
<td><input type="submit" id="${pending.fromUser.username} " value="Accept!"></td>
</tr>
</c:forEach>
</table>
</form>
I need to get every row from column "from_user" that is NOT the currently logged in user (session variable is in place already). 我需要从“from_user”列获取不是当前登录用户的每一行(会话变量已经到位)。 My simple thinking was to include a "NOT LIKE" statement in the Pending query:
我的简单想法是在Pending查询中包含一个“NOT LIKE”语句:
query = "SELECT P.fromUser FROM PendingChallenges P WHERE P.fromUser NOT LIKE '" + current_user + "'";
"current_user" is that EJB session String I mentioned. “current_user”是我提到的EJB会话字符串。
However, every time I run it, I either get errors saying that either "P.fromUser" is not a String, "P.fromUser.username" does not belong to "entity[Usercredentials]" or the like OR the keyword "LIKE" has not been recognised -> Something along the lines of SQLException Error Code 30000. 但是,每次运行它时,我都会收到错误,说“P.fromUser”不是字符串,“P.fromUser.username”不属于“entity [Usercredentials]”之类或者类似关键字“LIKE” “尚未得到认可 - > SQLException错误代码30000的内容。
I would read about "SQL joins". 我会读到关于“SQL连接”的内容。
The query should be something like this to get the fields from the user credentials table: 查询应该是这样的,以从用户凭据表中获取字段:
SELECT * FROM PendingChallenges P
INNER JOIN UserCredentials U ON P.fromUser = U.username
WHERE P.fromUser != '" + current_user + "'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.