Site icon DataFlair

JSP useBean with Practical Examples

Program 1

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>

Program 2

package pojo;

public class Student 
{
   private int sid;
   private String sname;
   private int phy;
   private int chem;
   private int math;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getPhy() {
        return phy;
    }

    public void setPhy(int phy) {
        this.phy = phy;
    }

    public int getChem() {
        return chem;
    }

    public void setChem(int chem) {
        this.chem = chem;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
    
}

Program 3

<%-- 
    Document   : InsertStudent
    Created on : Nov 8, 2023, 11:56:00 AM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         <center> 
           <form method="post" action="SaveRecord.jsp">  
           <table border="1">  
               <tr>
                   <th>Roll No</th>
                   <td><input type="text" name="sid"></td>
               </tr>  
               <tr>
                   <th>Name</th>
                   <td><input type="text" name="sname"></td>
               </tr>  
               <tr>
                   <th>Physics</th>
                   <td><input type="text" name="phy"></td>
               </tr>  
               <tr>
                   <th>Chemistry</th>
                   <td><input type="text" name="chem"></td>
               </tr>  
               <tr>
                   <th>Maths</th>
                   <td><input type="text" name="math"></td>
               </tr>  
               <tr>
                   <td></td>
                  <td>
                      <input type="submit" value="Submit">
                      <input type="reset" value="Reset">
                  </td>
               </tr>
            </table>
               
           </form>    
         </center>    
    </body>
</html>

Program 4

<%-- 
    Document   : SaveRecord
    Created on : Nov 8, 2023, 12:00:13 PM
    Author     : admin
--%>

<%@page import="pojo.Student"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page  import="java.sql.*" %>
<!DOCTYPE html>
<jsp:useBean class="pojo.Student" id="S">
    <jsp:setProperty name="S" property="*"></jsp:setProperty>
</jsp:useBean>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <% 
//            out.println(S.getSid());
//            out.println(S.getSname());        
//            out.println(S.getPhy());        
//            out.println(S.getChem());
//            out.println(S.getMath());
           Class.forName("com.mysql.jdbc.Driver");
           Connection con=null;
           PreparedStatement ps=null;
           con=DriverManager.getConnection("jdbc:mysql://localhost:/dataflair","root","root@data");
           String sql;
           sql="insert into student values(?,?,?,?,?)";
           ps=con.prepareStatement(sql);
           ps.setInt(1, S.getSid());
           ps.setString(2, S.getSname());
           ps.setInt(3,S.getPhy());
           ps.setInt(4,S.getChem());
           ps.setInt(5,S.getMath());
           if(ps.executeUpdate()>0)
               out.println("Record inserted......");
           
           
           
        %>
    </body>
</html>

 

Exit mobile version