Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JSP (http://www.go4expert.com/forums/jsp/)
-   -   JSP to insert new records (http://www.go4expert.com/forums/jsp-insert-records-t26308/)

jpachar 18Jul2011 15:59

JSP to insert new records
 
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>



All times are GMT +5.5. The time now is 03:09.