Rahul Patil
Rahul Patil

Reputation: 11

Cannot insert data in MYSQL Using Java

Need help with the below. I am unable to understand what is incorrect in the below code. I have also check the MYSQL names and HTML Form. No errors there.

<%@page import="java.sql.*"%>
<%
String Week=request.getParameter("Week");
String Metric=request.getParameter("Metric");
String Method=request.getParameter("Method");
String Region=request.getParameter("Region");
String TI=request.getParameter("TI");
String ASSA=request.getParameter("ASSA");
String TM=request.getParameter("TM");
String Skilled=request.getParameter("Skilled");
String center=request.getParameter("center");
String caller_id=request.getParameter("caller_id");
String observer=request.getParameter("observer");
String Observations=request.getParameter("Observations");

try{
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance_form","root","mypass");
    Statement st=con.createStatement();
    st.executeUpdate("insert into form1 (Week,Metric,Method,Region,TI,ASSA,TM,Skilled,center,caller_id,observer,Observations) values('"+Week+"','"+Metric+"','"+Method+"','"+Region+"','"+TI+"','"+ASSA+"','"+TM+"','"+Skilled+"','"+center+"','"+caller_id+"','"+observer+"','"+Observations+"')");
    response.sendRedirect("save.html");
}
catch(Exception e)
{
    response.sendRedirect("error.html");
}

%>

Upvotes: 0

Views: 57

Answers (1)

Roland Kreuzer
Roland Kreuzer

Reputation: 992

If the data was not inserted there was probably an exception. I'd start by trying to capture possible error messages.

catch(Exception e)
{
    e.printStackTrace();
    response.sendRedirect("error.html");
}

This should give you additional error information in your IDE console or application server log you can proceed on.

Code in JSP is generally pretty hard to debug. If you have the option you might want to migrate the logic into a Java class.

Upvotes: 1

Related Questions