Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Implementing Bean with script let in JSP

Related Tutorials


 
 

Share on Google+Share on Google+

Implementing Bean with script let in JSP

Advertisement
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.

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 2, 2009

Related Tutorials

Discuss: Implementing Bean with script let in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
ayesha
February 11, 2012
software development

d above code runs but doesnt insert anythiing into d table.... pleaze help
DMCA.com