1

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

my bean.java

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);
        }
    }

}

here is my jspbean.jsp file

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

<html>
    <head><title>JSP with Javabeans</title></head>
<body bgcolor="#ffccff">
<h1>JSP using JavaBeans example</h1>
    <form name="form1" method="POST">

         ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type="text" name ="msgid"> <br>
         Message<input type="text" name ="message"> <br>
         <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type = "submit" value="Submit">
         <jsp:useBean id="sample" class="com.javabean.bean" scope="page">
            <jsp:setProperty name="sample" property="*"/>
        </jsp:useBean>
    </form>
    <% sample.insert();%>
</body>
</html>

how can i achieve this please solve my problem hanks in advance T

1
  • Off subject comment: You really shouldn't be doing DB manipulations in the UI code. This is typically frowned upon for many reasons, not least of which it's almost impossible to debug. Also, you should use PreparedStatements and pass in the values to your insert as parameters. Concatenation like you are doing leads to problems like SQL injection. Enough of my soapbox. Commented Mar 6, 2014 at 13:34

1 Answer 1

1

Your doing some mistake:

public void setmsgid(String msgid)
    {
        this.msgid = msgid;
    }
    public String getmsgid()
    {
        return (this.msgid);
    }

you should change as:

public String getmsgid()
    {
        return msgid;
    }
public void setmsgid(String msgid)
    {
        this.msgid = msgid;
    }

and add this one also:

public String toString()
{
    return "User[msgid="+msgid+",message="+message+"];

}

you should must change this one:

insert into login(username,password) values('"+msgid+"','"+message+"')";

insert into login(username,password) values(?,?)";

update:

insert into login(messageid,message) values(?,?)";
Sign up to request clarification or add additional context in comments.

14 Comments

where should i add this
first you should change the insert part and then compile
i changed in insert part as u said but now there is no value is inserting in db
i had one doubt, where did you use the username and password
i used username and password in table field name
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.