Error while inserting data in MySQL

Go To StackoverFlow.com

-4

While I perform insertion in the db table, the exception occurs, which says:

Data truncation error in mySQL while inserting data..

At:->

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'branchname' at row 1

the column type is varchar

JSP:

<form action="BranchServlet" method="post">
<table align="center">
<tr>
<td align="center" colspan="2"><hr>Add Branch Details<hr></td>
</tr>


<tr>
<td align="left">Branch Name</td>
<td align="center"><input type="text" name="branchname"></td>
</tr>
<tr>
<td align="left">Branch Code</td>
<td align="center"><input type="text" name="branchcode"></td>
</tr>
<tr>
<td align="center">

</td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" Value="Submit"></td>
</tr>
</table>
</form>

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        String bname = request.getParameter("branchname");
        String bcode = request.getParameter("branchcode");

        BranchModel bm = new BranchModel();
        BranchDAO bd = new BranchDAO();

        bm.setBranchcode(bcode);
        bm.setBranchname(bname);

        bd.insert(bm);

        response.sendRedirect("branches.jsp");
    }

DAO Method:

public void insert(BranchModel m)
    {
        try{
            Class.forName(driver);
            con = DriverManager.getConnection(url+db,"root","root");
            Statement st = con.createStatement();

            st.executeUpdate("insert into branches values('"+m.getBranchcode()+"','"+m.getBranchname()+"')");
            System.out.println("record inserted");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
2012-04-04 17:31
by sohel khalifa
I don't want to be unkind but posting HTML for a SQL data truncation error seems irrelevant. Adding URGENT to your post does you no good, this is a volunteer site. Also, your code is possibly subject to SQL injection attack - RedFilter 2012-04-04 17:34
And using strange words like guyz or plz doesn't improve the perception eithe - a_horse_with_no_name 2012-04-04 17:40
@RedFilter, only if Little Bobby Tables is using his site - Chris Benard 2012-04-04 17:44
@ahorsewithnoname, I am new here, and also not much experienced like you - sohel khalifa 2012-04-04 17:44
@SohelKhalifa: why does that imply you don't have to use proper english words? You are expecting free help, so the least you can do, is the courtesy to use a decent languag - a_horse_with_no_name 2012-04-04 17:46


4

You said the column is of type varchar, but not what size varchar. This seems to be the root of your problem. I don't see any validation enforcing a limit on the size of the text input. The input validations need to limit input size the size of the corresponding varchar column.

2012-04-04 17:37
by Seth
And like RedFilter said, this IS susceptible to injection - Seth 2012-04-04 17:39
yeah you were right.. the problem was with the size of the field, not its typ - sohel khalifa 2012-04-04 17:42
@SohelKhalifa, you need to accept the answer (green checkmark) if that solved your issue - Chris Benard 2012-04-04 17:43
plz 2 accept mi ansuh - Seth 2012-04-04 17:43
Ads