Click here to Skip to main content
15,564,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display from 3 different table and with particular user id. I've tried do and after run it will display HTTP status 500, and it said the problem at here :
rs = st.executeQuery("select * from tbl_totalmonday where idEmploy = '" + session.getAttribute("uidUser") + "'");
 rs = st.executeQuery("select * from tbl_totalsunday where idEmployE = '" + session.getAttribute("uidUser") + "'");
  rs = st.executeQuery("select * from tbl_totalholiday where idEmployP = '" + session.getAttribute("uidUser") + "'");


Then, I want to know the exact code how to display 3 database in one HTML page, is it impossible to do? I also try one with use executeQuery(query); but if I use this one it will give all output from database:
String query = "select * from tbl_totalmonday, tbl_totalsunday, tbl_totalholiday";
                                Statement st = con.createStatement();

                                rs = st.executeQuery(query);


If anyone know how to do the exact code to display all database take from particular user id, please help me, Thanks a lot for answering my question ^-^.

What I have tried:

<%
           Connection con;
           PreparedStatement pst;
           ResultSet rs;

           Class.forName("com.mysql.cj.jdbc.Driver");
           con = DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance?zeroDateTimeBehavior=CONVERT_TO_NULL", "root", "");

           String query = "select * from tbl_totalmonday, tbl_totalsunday, tbl_totalholiday";

           Statement st = con.createStatement();

           rs = st.executeQuery("select * from tbl_totalmonday where idEmploy = '" + session.getAttribute("uidUser") + "'");
            rs = st.executeQuery("select * from tbl_totalsunday where idEmployE = '" + session.getAttribute("uidUser") + "'");
             rs = st.executeQuery("select * from tbl_totalholiday where idEmployP = '" + session.getAttribute("uidUser") + "'");

           while (rs.next()) {
               Double idEmploy = rs.getDouble("idEmploy");
               Double idEmployE = rs.getDouble("idEmployE");
               Double idEmployP = rs.getDouble("idEmployP");
       %>
Posted
Updated 8-Jun-22 19:55pm
v2

1 solution

Try using a JOIN: SQL Joins[^]

But ... Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
v2
Comments
aufa 2022 12-Jun-22 22:54pm    
Okay I'm understand, thank you for sharing the info, and yes I do the backup, thanks for reminded me... Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900