Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java JSP
hi
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"%>
<%@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();
addCust_obj.setCustomer_fname(request.getParameter("add_customer_fname"));
addCust_obj.setCustomer_lname(request.getParameter("add_customer_lname"));
addCust_obj.setCustomer_address(request.getParameter("add_customer_cellnumber"));
addCust_obj.setCustomer_cellnumber(request.getParameter("add_customer_address"));
*/
//DBUtil.SaveCustomer(addCust_obj);
//System.out.println(adduser.getCustomer_fname());
/*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="INSERT INTO customers (customer_id, first_name,last_name,contact,address) VALUES('null','"+request.getParameter("add_customer_fname")+"','"+request.getParameter("add_customer_lname")+"','"+request.getParameter("add_customer_cellnumber")+"','"+request.getParameter("add_customer_address")+"')";
try{
	con = DBUtil.getConnection("jdbc:mysql://localhost:3306/","dry_cleaner","com.mysql.jdbc.Driver","root","");
	//Class.forName(driver);
	//con = DriverManager.getConnection (dbUrl+db,user,pass);
	System.out.println("connection success fully");
}catch(Exception ex){
	System.out.println("Connection error");
	}
try{
	updateQuery=stmt.executeUpdate(query);
	  if (updateQuery != 0) {
		  System.out.println("table is created successfully and " + updateQuery + " row is inserted.");
	  }
	//stmt = con.createStatement();
	//stmt.executeUpdate(query);
	/*
	 QueryString = "INSERT INTO user_master1(User_Name,UserId,User_Pwd)
 VALUES ('Mahendra',"+ "'mahendra25','1213456')";
  updateQuery = statement.executeUpdate(QueryString);
  if (updateQuery != 0) {
  System.out.println("table is created successfully and " + updateQuery
 + " row is inserted.");
	*/
	 /*if(stmt.executeUpdate(query)>0){
		   System.out.print("Records inserted");
		  }else{
		   System.out.print("insertion faild");
		  }*/
}catch(Exception ex){
	System.out.println("Query :"+query);
	//throw new Exception("Query Error");
}
response.sendRedirect("user_page.jsp");
%>
Posted 26-Aug-12 22:20pm
Edited 26-Aug-12 22:25pm
TorstenH.33.3K
v2
Comments
TorstenH. at 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
System.out.println(ex.getMessage());
Khdua Bux Baloch at 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();
addCust_obj.setCustomer_fname(request.getParameter("add_customer_fname"));
addCust_obj.setCustomer_lname(request.getParameter("add_customer_lname"));
addCust_obj.setCustomer_address(request.getParameter("add_customer_cellnumber"));
addCust_obj.setCustomer_cellnumber(request.getParameter("add_customer_address"));
*/
//DBUtil.SaveCustomer(addCust_obj);
//System.out.println(adduser.getCustomer_fname());
/*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;
try{
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");
System.out.println(ex.getMessage());
}
try{
// 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")+"')";
//System.out.println(query);
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");
}
response.sendRedirect("user_page.jsp");
%>
 

and showing this exception
 
connection success fully
Query Error:null
Flint-Bear at 29-Aug-12 9:39am
   
What is the URL you are testing with?
leejoyprakash at 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
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
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