1

I have two tables in demo1 database. And i have to retrieve data from both of them. They have linked with foreign key. But when i tried i got data form only single table which is tbl1 but not getting data of referenced tbl2. So please help me for this. This is my code what i have done for this. There are two tables tbl1 and tbl2. And tbl1's primarykey id is given foreign key in tbl2,and tbl2's primary key is id_tbl2.

<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
     <%
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;  
            try{
              Class.forName("com.mysql.jdbc.Driver");
              con=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo1","root","root");
              st=con.createStatement();
              rs=st.executeQuery("SELECT tbl1.name, tbl2.id_tbl2, tbl1.surname  FROM tbl1 INNER JOIN tbl2 ON tbl1.id=tbl2.id_tbl1");
                             while(rs.next())
              {
                  out.write("</br>");
                  out.write(rs.getString(1));

                out.write(rs.getString(2));
                out.write("</br>");

                 out.write(rs.getString(3));
                out.write("</br>");

                out.write(rs.getString(4));
                out.write("</br>");
                           } rs.close();con.close();
             }catch(Exception e){
              out.write(" "+e);               
            }
        %>
    </body>
</html>
2
  • If you run your query outside your jsp application, do you get data?
    – Dan Bracuk
    Mar 29, 2014 at 12:40
  • Yes, i got data in mysql using query. But unfortunately not in jsp application. Mar 29, 2014 at 12:44

1 Answer 1

0

Your query is:

SELECT tbl1.name, tbl2.id_tbl2, tbl1.salary  
FROM tbl1 INNER JOIN tbl2 ON tbl1.id=tbl2.id_tbl1

There are only 3 fields in the select statements.
But you are trying to read the 4th field which does not exist.

out.write(rs.getString(1));

out.write(rs.getString(2));
out.write("</br>");

out.write(rs.getString(3));
out.write("</br>");

out.write(rs.getString(4)); // this will result a runtime exception
out.write("</br>");

Remove the 4th, i.e. getString(4), statement, and it should be working.

11
  • Thanks @Ravinder, I have removed that but still i am getting data from first table (tbl1) only. Not from tbl2. Mar 29, 2014 at 13:16
  • Create both tbl1 and tbl2 with data on SQL Fiddle and share with us. Mar 29, 2014 at 13:29
  • CREATE TABLE demo1.tbl1 ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(45) NOT NULL, surname VARCHAR(45) NOT NULL, PRIMARY KEY (id), ) ENGINE = InnoDB; INSERT INTO tbl1(name,surname) values('abc','xyz'),('pqr','mnc'),('vbc','lmn'),('itl','quk'); Mar 29, 2014 at 14:38
  • CREATE TABLE demo1.tbl2 ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, id_tbl1 INTEGER UNSIGNED NOT NULL, hobbies VARCHAR(45) NOT NULL, PRIMARY KEY (id_tbl2), CONSTRAINT FK_tbl4_1 FOREIGN KEY FK_tbl4_1 (id_tbl1) REFERENCES tbl1 (id) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB; INSERT INTO tbl2(id_tbl1,hobibes) values('1','tennis'),('1','football'),('2','carrom'),('2','cricket'),('3','rugby'),('4','reading'); Mar 29, 2014 at 14:39
  • Why don't you create the same on SQL Fiddle and share with us. Mar 29, 2014 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.