aspose file tools*
The moose likes JDBC and the fly likes Iam unable to execute insert and select statements using batches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Iam unable to execute insert and select statements using batches" Watch "Iam unable to execute insert and select statements using batches" New topic
Author

Iam unable to execute insert and select statements using batches

Raghavendra ReddyI
Greenhorn

Joined: Sep 10, 2012
Posts: 8
Raghavendra ReddyI
Greenhorn

Joined: Sep 10, 2012
Posts: 8
Raghavendra ReddyI wrote:
Raghavendra ReddyI wrote:
Raghavendra ReddyI wrote:import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class DeleteFlightData extends HttpServlet
{
Connection con=null;
Statement stmt=null;
int fid1,pid1;
String pname1;

public void init(ServletConfig config)throws ServletException
{
super.init(config);
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");
}
catch(Exception e)
{
}
}

public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
fid1=Integer.parseInt(req.getParameter("fid"));
try
{
stmt=con.createStatement();
con.setAutoCommit(false);
String query="select pid,pname form pilot where fid="+fid1;
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
pid1=rs.getInt("pid");
pname1=rs.getString("pname");
}
String query1="delete from flight where fid="+fid1;
stmt.addBatch(query1);
String query2="insert into pilot (pid,pname,fid,onduty,free,leave) values ("+pid1+",'"+pname1+"',0,'-','free','-')";
stmt.addBatch(query2);
int[] count = stmt.executeBatch();
con.commit();
}
catch(SQLException sqle)
{
out.println("<div align='center' style='color:#FF0000;font-size:25px;'>");
out.println("

Cannot be deleted........!!!

");
out.println("</div>");
}
catch(Exception e)
{

out.println("<div align='center' style='color:#FF0000;font-size:25px;'>");
out.println("

Cannot be added........

");
out.println("</div>");
}
}//End of doPost

}//end of class
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

The problem, I think, is that when an exception is thrown, your code throws away all the information in the exception and just displays something generic in the response. You should at least log the exception before doing that.

The reason I think this is the problem is that when you have "select ... form ..." instead of "select ... from ...", as your posted code does, then an exception will be thrown telling you that your SQL is not well-formed. But since you ignore the exception, you have no way of telling that.
Raghavendra ReddyI
Greenhorn

Joined: Sep 10, 2012
Posts: 8
but still iam unable to execute
exception thrown is
java.sql.BatchUpdateException: error occurred during batching: ORA-02291: integrity constraint (SYSTEM.FK_PILOT) violated - parent key not found
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

That's good -- at least you have an error message. Now you need the full stack trace, not just the error message. It will tell you what line of code threw the exception, and that's where you should start looking. So you should write out the full stack trace when an exception is thrown, and you can do that by calling the exception's "printStackTrace()" method.
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47
Your first sql query select has an issue. Revise your sql is needed to get your code getting executed correctly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iam unable to execute insert and select statements using batches