Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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....




JavaScript
<%@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
Updated 26-Aug-12 22:25pm
v2
Comments
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
System.out.println(ex.getMessage());
Khudha Bakhsh Mari 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 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

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900