title: How to insert value in database using bean class in jsp?
body: <p>Hi  iam tring to insert value in database using bean class in jsp form and i am able to this but i am getting little bit problem whenever i insert value in table  null value also insert with them i tried  not i am not able to get  that where i m wrong please solve my problem</p>
  
  <p>my bean.java</p>
  
  <pre><code>package com.javabean;
  
  
  import java.io.*;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.Statement;
  import java.sql.ResultSet;
  
  public class bean 
  {
  
      private String msgid;
      private String message;
      private Connection connection=null;
      private ResultSet rs = null;
      private Statement st = null;
      String connectionURL = "jdbc:mysql://localhost:3306/JspBean";
  
  
      public bean() 
      {
           try {
               // Load the database driver
              Class.forName("com.mysql.jdbc.Driver");
              // Get a Connection to the database
              connection = DriverManager.getConnection(connectionURL, "root", ""); 
              }catch(Exception e){
              System.out.println("Exception is ;"+e);
              }
  
      }  
      public void setmsgid(String msgid)
      {
          this.msgid = msgid;
      }
  
      public String getmsgid()
      {
          return (this.msgid);
      }
  
      public void setmessage(String message)
      {
          this.message = message;
      }
  
      public String getmessage()
      {
          return (this.message);
      }
  
      public void insert()
      {
  
         try
         {
              String sql = "insert into login(messageid,message) values('"+msgid+"','"+message+"')";
              Statement s = connection.createStatement();
              s.executeUpdate (sql);
              s.close ();
          }catch(Exception e){
              System.out.println("Exception is ;"+e);
          }
      }
  
  }
  </code></pre>
  
  <p>here is my jspbean.jsp file</p>
  
  <pre><code>&lt;%@ page language="Java" import="java.sql.*" %&gt;
  
  &lt;html&gt;
      &lt;head&gt;&lt;title&gt;JSP with Javabeans&lt;/title&gt;&lt;/head&gt;
  &lt;body bgcolor="#ffccff"&gt;
  &lt;h1&gt;JSP using JavaBeans example&lt;/h1&gt;
      &lt;form name="form1" method="POST"&gt;
  
           ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
           &lt;input type="text" name ="msgid"&gt; &lt;br&gt;
           Message&lt;input type="text" name ="message"&gt; &lt;br&gt;
           &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
           &lt;input type = "submit" value="Submit"&gt;
           &lt;jsp:useBean id="sample" class="com.javabean.bean" scope="page"&gt;
              &lt;jsp:setProperty name="sample" property="*"/&gt;
          &lt;/jsp:useBean&gt;
      &lt;/form&gt;
      &lt;% sample.insert();%&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>how can i achieve this please solve  my problem
  hanks in advance
  T</p>