JSP to insert new records

Discussion in 'JSP' started by jpachar, Jul 18, 2011.

  1. jpachar

    jpachar New Member

    Joined:
    Jul 18, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    What is the error in the following code, I want to insert the records only when the button is pressed.
    Please teach me how to declare methods in JSP
    Code:
    <%@page import="java.sql.*;"%>
    <html>
    <title> </title>
    <head>
    <%!
     int rn;
     String sn;
     Connection conn=null; 
     Statement stm;
     String s;
     ResultSet r;%>
    <% try
     {
     Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); 
     conn = DriverManager.getConnection("jdbc:odbc:first", "", ""); 
     out.println ("Database Connected.");
     stm=conn.createStatement();
     s="select * from student";
     r=stm.executeQuery(s);
     //stm.executeUpdate(s);
     while(r.next())
     { 
     rn=r.getInt("rno");
     sn=r.getString("sname");
     }
     }
     catch(Exception e)
     {
     out.println(e.toString());
     }
     void add()
     {
      String s = "insert into student(rno,sname)values(' " & rno.value & " ', ' " &snam.value & " ')";
      r=stm.executeUpdate(s);
     }
    %>
    <body>
    <form>
    Roll Number:<input type="text" name="rno" value="<%=rn%>">
    Student Name:<input type="text" name="snam" value="<%=sn%>">
    <input type="button" value="Insert" onclick="add()">
    </form>
    </body>
    </html>
     
    Last edited by a moderator: Jul 18, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice