简体   繁体   中英

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.

Databases: So far I have 2 tables in that can be access via the " LOCALHOST. " prefix: Usercredentials and Pending_Challenges. Both were created using the following code:

Database: JavaDB

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:

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. Each one is to create a different set of results (code below ofc) to be printed in 2 different tables.

{... 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):

<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}&nbsp;&nbsp;</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} &nbsp;&nbsp;</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). My simple thinking was to include a "NOT LIKE" statement in the Pending query:

query = "SELECT P.fromUser FROM PendingChallenges P WHERE P.fromUser NOT LIKE '" + current_user + "'";

"current_user" is that EJB session String I mentioned.

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.

I would read about "SQL joins".

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 + "'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM