[an error occurred while processing this directive]

Implementing Bean with script let in JSP

                          

This application illustrates how to create a bean class and how to implement it with script let of jsp for inserting the data in mysql table.

In this example we create a jsp page for taking the user name to insert in the database table. The bean.java class is used to fire the insert query and create the setter and getter method to set and get the value which come from jsp page.

 

 

The complete application is as follows:

 

 

Source Code of jspBean.jsp 

<%page language="Java" import="java.sql.*" %>

<html>

<head><title>JSP with Javabeans</title></head>

<body>
  <h1>JavaBeans example</h1>

  <form name="form1" method="POST">
    
    Your Name:<input type="text" name ="message"> <br>
    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type = "submit" value="Submit">

    <jsp:useBean id="sample" class="myexample.bean" scope="request">
      <jsp:setProperty name="sample" property="*"/>

           <br>
  <b>Welcome <jsp:getProperty name="sample" property="message"/></b>
    </jsp:useBean>



  </form>
  <% sample.insert();%>
</body>

</html>

 

Source Code of bean.java 

package myexample;

import java.io.*;
import java.sql.*;
 
public class bean {
  private int msgid;
  private String message;
    private Connection connection=null;
    private ResultSet rs = null;
    private Statement st = null;
  String connectionURL = "jdbc:mysql://localhost:3306/test";

  public bean() {
    try {
      Class.forName("com.mysql.jdbc.Driver");
      connection = DriverManager.getConnection(connectionURL, "root""root")
    }catch(Exception e){
      System.out.println("Exception is ;"+e);
    }
  }  

  public void setmsgid(int msgid){
    this.msgid = msgid;
  }

  public int getmsgid(){
    return (this.msgid);
  }

  public void setmessage(String message){
    this.message = message;
  }

  public String getmessage(){
    System.out.println("Message: "+message);
    return (this.message);
  }

  public void insert(){
    try{
      if(message!=null)
      {
      String sql = "insert into message(message)values('"+message+"')";
      Statement s = connection.createStatement();
      s.executeUpdate (sql);
      s.close ();
      }
    }catch(Exception e){
      System.out.println("Exception is ;"+e);
    }
  }
}

Download Complete Source Code

                          

[an error occurred while processing this directive]