Click here to Skip to main content
12,701,601 members (35,080 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java JSP
i am trying to insert in database in this way using jsp but it is not working can any one help me where i have make an mistake....

<%@page import="com.dryclean.web.controller.DBUtil"%>
Posted 26-Aug-12 23:20pm
Updated 26-Aug-12 23:25pm
TorstenH. 27-Aug-12 4:27am
don't do in case of Exception
System.out.println("Query :"+query);

You need to see the exception text - make it at least
Khdua Bux Baloch 28-Aug-12 1:10am
hi now i am adding this in this way but it is showing query null if you have any solution please share with me..

<%@page import="com.dryclean.web.controller.DBUtil"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
/*AdduserBeans addCust_obj=new AdduserBeans();
/*String dbUrl = "jdbc:mysql://localhost:3306/";
String db = "dry_cleaner";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";*/
Connection con =null;
Statement stmt = null;
int updateQuery=0;
String query=null;
con = DBUtil.getConnection("jdbc:mysql://localhost:3306/","dry_cleaner","com.mysql.jdbc.Driver","root","");
System.out.println("connection success fully");
}catch(Exception ex){
//System.out.println("Connection error");
// query="INSERT INTO customers (customer_id, first_name,last_name,contact,address) VALUES('','"+request.getParameter("add_customer_fname")+"','"+request.getParameter("add_customer_lname")+"','"+request.getParameter("add_customer_cellnumber")+"','"+request.getParameter("add_customer_address")+"')";
updateQuery=stmt.executeUpdate("INSERT INTO customers (customer_id, first_name,last_name,contact,address) VALUES('','"+request.getParameter("add_customer_fname")+"','"+request.getParameter("add_customer_lname")+"','"+request.getParameter("add_customer_cellnumber")+"','"+request.getParameter("add_customer_address")+"')");
//if (updateQuery != 0) {
// System.out.println("table is created successfully and " + updateQuery + " row is inserted.");
}catch(Exception ex){
//System.out.println("Query :"+query);
System.out.println("Query Error:"+ex.getMessage());
//throw new Exception("Query Error");

and showing this exception

connection success fully
Query Error:null
Flint-Bear 29-Aug-12 9:39am
What is the URL you are testing with?
leejoyprakash 21-Sep-12 9:36am
I think you are missing the statement stmt = con.createStatement(); add this before stmt.executeUpdate(query);

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

you have to use con.createStatement() before update query.It is better to use prepare statement for inserting data into dB.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100