multiple data insert...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mak1084
    New Member
    • Jul 2007
    • 44

    multiple data insert...?

    Hi Friends,
    I'm facing a serius problem...in 2 days i have to submit my project and i'm stuck here..please help me...

    I'm getting some values from the database from one table say roll_no on one page....and then on that page i'm putting some values in front of that roll_no.
    Now I want to store the all data in a another table..
    P.S. roll no. are not fixed and that is it can any thing from 1 - 60

    here is my code.


    [CODE=java]
    <select size="1" name="subject" style="font-family: Arial; font-size: 10px; color:#0B333C"> </select>
    <--some value -->
    <select size="1" name="month" style="font-family: Arial; font-size: 10px; color:#0B333C"> </select>
    <--some value-->
    <select size="1" name="year" style="font-family: Arial; font-size: 10px; color:#0B333C"> </select></tr>
    <tr>
    <td><input type="submit" value="SUBMIT" name="action"/>
    <table width="800" border="4" cellpadding="1" cellspacing="1" >
    <td>
    <table width="800" border="2" bordercolor="#F FFFFF" cellpadding="0" cellspacing="0" >
    <tr> <td align="right">

    </tr>
    <td>
    <%! String stu_roll;%>
    <%
    String getsub1 = request.getPara meter("subject" );
    String getmonth = request.getPara meter("month");
    String getyear = request.getPara meter("year");
    %>
    <%
    try
    {
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    Connection con = DriverManager.g etConnection("j dbc:odbc:lala") ;
    Statement stmt = con.createState ment();
    String str1 = "select roll_no from student where sem_id = (select sem_id from subject where course_id ='bsc_it' and sub_id = '"+getsub1+"')" ;
    ResultSet rs = stmt.executeQue ry(str1);
    %>
    <table align="center" width="" cellpadding="0" cellspacing="0" border="1" cellspacing="1" cellpadding="1" >
    <tr>
    <td><input type="text" value="Total Lecture" readonly=""/></td>
    <td><input type="text" name="total_att " maxlength="2"></td>
    </tr>
    <tr>
    <td><input type="text" value="Student roll no." readonly="" /></td>
    </tr>
    <%
    int no = 0;
    while(rs.next() )
    {
    stu_roll = rs.getString("r oll_no");
    no = no + 1;
    %>
    <tr bordercolor="#C C3366">
    <td>
    <input type="text" name="roll<%=no %>" value="<%= stu_roll %>" readonly=""/>
    </td>
    <td align="center" style="color:#0 052A4 ">:</td>
    <td>
    <input type="text" name="att<%=no %>" />
    <input type="hidden" name="no" value="<%=no %>" />
    </td>
    </tr>


    <%
    }

    %>
    [/CODE]
    second page from where i'm inserting.

    [CODE=java]
    <%!
    int no1 = request.getPara meter("no");
    int i;
    %>
    <%
    for(i=1;i<no1;i ++)
    {
    %>
    <%
    String getsub1 = request.getPara meter("subject" );
    String getmonth = request.getPara meter("month");
    String getyear = request.getPara meter("year");
    String getatt = request.getPara meter("att"+i);
    String stu_roll = request.getPara meter("roll"+i) ;
    %>

    <%


    try
    {
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    Connection con = DriverManager.g etConnection("j dbc:odbc:lala") ;
    PreparedStateme nt ps=con.prepareS tatement("inser t into attendance values(?,?,?,?, ?,?)");

    ps.setString(1, stu_roll);
    ps.setString(2, getsub1);
    ps.setString(3, getmonth);
    ps.setString(4, getyear);
    ps.setString(5, getatt);
    ps.setString(6, "10");

    ps.executeUpdat e();
    %>
    [/CODE]
    please help me......
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) You are creating database connections from a JSP, you should separate code that does different things into different modules(classes packages). Code that connects to a database to get data should be written separately from code that displays data and that should be written separately from code that processes data e.t.c
    2.) You didn't specify your exact problem.

    Comment

    • mak1084
      New Member
      • Jul 2007
      • 44

      #3
      Originally posted by r035198x
      1.)
      2.) You didn't specify your exact problem.
      I dont know whether the method which I'm using is right or wrong. Here I'm using a variable 'no' to count the total roll_no. This variable is also being used in the <input> tag to give a diffrent name for the diffrent values of the roll_no.
      but here i'm not able to get the values in next page in getParameter...
      and geting this error...


      [CODE=java]type Exception report

      message

      description The server encountered an internal error () that prevented it from fulfilling this request.

      exception
      org.apache.jasp er.JasperExcept ion: java.lang.Numbe rFormatExceptio n: null
      org.apache.jasp er.servlet.JspS ervletWrapper.h andleJspExcepti on(JspServletWr apper.java:541)
      org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 435)
      org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:320)
      org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :266)
      javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:803)


      root cause
      java.lang.Numbe rFormatExceptio n: null
      java.lang.Integ er.parseInt(Unk nown Source)
      java.lang.Integ er.parseInt(Unk nown Source)
      org.apache.jsp. lala.attendance _jsp._jspServic e(attendance_js p.java:71)
      org.apache.jasp er.runtime.Http JspBase.service (HttpJspBase.ja va:70)
      javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:803)
      org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 393)
      org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:320)
      org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :266)
      javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:803)

      note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.[/CODE]


      Is I'm doing in right way???
      I think HashMap can also be used here but i have never played with the HashMap..

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        In which JSP are you doing the Integer.parseIn ts? One of them is receiving null as argument( that's what your stack trace says)

        Comment

        • mak1084
          New Member
          • Jul 2007
          • 44

          #5
          Originally posted by r035198x
          In which JSP are you doing the Integer.parseIn ts? One of them is receiving null as argument( that's what your stack trace says)
          Oh!! I did some change before posting...
          here where I chaned it in second jsp.

          [CODE=java]<%!
          int no1 = Integer.parseIn t(request.getPa rameter("no"));
          int i;
          %>
          <%
          for(i=1;i<no1;i ++)
          {
          %>[/CODE]

          you can see that in first jsp file i'm sending through a Hidden input.
          Code:
          <input type="hidden" name="no" value="<%=no %>" />
          I don't know why its not sending the value to the next page.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            The parameter is never submitted to the second JSP then. Is it on the form that was submitted? If you had separated the Java from the HTML it would have been easy to pick up

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              I second the plea for you to write better code. JSP should have no database nonsense in it. In fact it should have no Java at all embedded in it! If your database code were separate, it would be so much easier to design, write, debug, maintain and generally understand.

              If your instructor is teaching you to write the sort of code you posted, I, well, I can't post the words I'm thinking!

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by BigDaddyLH
                ...., I, well, I can't post the words I'm thinking!
                Thinking bad things is not allowed in the public forums.
                ADMIN.

                Comment

                • mak1084
                  New Member
                  • Jul 2007
                  • 44

                  #9
                  Originally posted by BigDaddyLH
                  I second the plea for you to write better code. JSP should have no database nonsense in it. In fact it should have no Java at all embedded in it!
                  You mean that JSP should not have any java code....then how should i use the database?

                  If your instructor is teaching you to write the sort of code you posted, I, well, I can't post the words I'm thinking!
                  You can think it coz in college the professor don't know any think. They are not concerned about us, they will just give us internal marks...that all.

                  can you please send me some code example, so that i can see how to write the code?

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by mak1084
                    You mean that JSP should not have any java code....then how should i use the database?
                    In the usual way :-)

                    HTML gets/posts should be handled by a proper servlet, not a jsp. This servlet is a Controller in the MVC pattern. If you are using a framework like Struts or JSF, this servlet is already written for you as part of the framework. It delegates to other objects to handle the request. If database access is required, this is encapsulated in DAOs. The resulting data is stored in beans that are forwarded to a jsp and displayed.

                    Here's a discussion of some options in architecture: http://java.sun.com/developer/Books/...ges/Chap12.pdf

                    Comment

                    Working...