954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JSP query form

I have a table called school and a table called students.
I have a foreign key in students called school_id which is connected with table school , field id.
I made a form where field **title** appears to the registration form of the student.
The problem is that i want title to appear as it does but when submiting the form the school_id must be written in the database.

Here is the code:

<select name="SCHOOL_ID"   />
       <%
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection connection =java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/dbproject1","", "");
    String query1 ="SELECT TITLE FROM SCHOOL ";
    java.sql.PreparedStatement Stmt1 = connection.prepareStatement(query1);
    ResultSet rs = Stmt1.executeQuery();                     
while (rs.next()) {
   out.print("<option>");
   out.print(rs.getString("TITLE"));
   out.print("</option>");
}
%>
        </select>


thank you!

kostas_213
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Help plz!!!

kostas_213
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Many thanks for the reply peter. I 've runned it and show it. It's very good.

The thing is that i have my project almost ready and our teacher asked us not to use servlets.
So i'm stack with it.:-O:(

I can make a selection in my query like :

SELECT ID,TITLE FROM SCHOOL ORDER BY ID


but i don't know how only id will be passed to student's table

For example: The student can see a form with : New York School , London School , Dublin School. But when he choose London School and submits id=23 must be written in student's table.

I've tried so many things.I have a headache!:confused:

<td>
        <select name="SCHOOL_ID"   />
       <%
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection connection =java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/dbproject1","kostas9", "");
    String query1 ="SELECT TITLE , ID FROM SCHOOL ";
    java.sql.PreparedStatement Stmt1 = connection.prepareStatement(query1);
    ResultSet rs = Stmt1.executeQuery();                     
while (rs.next()) {
   out.print("<option>");
   out.print(rs.getString("TITLE"));
   out.print("</option>");
}
%>
                


        </select>
        </td>
kostas_213
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Hi kostas_213,

you already get good tutorial from Mr.peter_budo... Since you asked,
You can use sessions to achieve that

session.setAttribute( "id", stu_id);
session.setAttribute( "id", stu_id);
anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: