简体   繁体   中英

Unable to execute multiple SQL queries with Data Source connection

I am trying to execute multiple queries but only a part of the queries gets executed.

Here is my code:

public class PostPrReqDaoImpl implements PostPrReqDaoInterface {

    @Override
    public boolean validate(PostPrReqBean pprb1,PostPrReqBean pprb2,PostPrReqBean pprb3,PostPrReqBean pprb4) {
        System.out.println("Inside PostPrReq Dao");
        int resstat=1;
        //snuname,snuusrname,snupass,snuempid,snuemail,snudob,snuskill,snuexp,snudesg;
        java.io.PrintWriter out = null;
        try {
            out = pprb1.getBeanresponse().getWriter();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        pprb1.getBeanresponse().setContentType("text/html");



        try {
            /* get the DataSource from using the JNDI name */
            Class.forName("com.mysql.jdbc.Driver");
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Test");
            //username, pass,name, id,dob ,skillset, exp,designation ,email
            //Create connection and then continue as usual other JDBC calls 
            Connection conn=ds.getConnection();
            System.out.println("Connection succesful!");
            Statement s1= conn.createStatement();
            Statement s2= conn.createStatement();
            Statement s3= conn.createStatement();
            Statement s4= conn.createStatement();

            if(resstat == 1)
            {

                String sqlproj = "INSERT INTO tblproject (name,code,location,type,lastdate) " +
                        "VALUES ('" + pprb1.getBeanprojname() + "' , "
                        + "'" + pprb1.getBeanprojcode() +"' ,"
                        + "'" + pprb1.getBeanprojlocation() +"' ,"
                        + "'" + pprb1.getBeanprojtype() +"' ,"
                        + "'" + pprb1.getBeanprojlastdate() +"')";

                String sqlreq1="INSERT INTO tblrequirements (code,designation,skillset,exp,ncand)" +
                        "VALUES ('" + pprb1.getBeanprojcode() + "' , "
                        + "'" + pprb2.getBeandesg() +"' ,"
                        + "'" + pprb2.getBeanskill() +"' ,"
                        + "'" + pprb2.getBeanexp() +"' ,"
                        + "'" + pprb2.getBeanncand() +"')";

                String sqlreq2="INSERT INTO tblrequirements (code,designation,skillset,exp,ncand)" +
                        "VALUES ('" + pprb1.getBeanprojcode() + "' , "
                        + "'" + pprb3.getBeandesg() +"' ,"
                        + "'" + pprb3.getBeanskill() +"' ,"
                        + "'" + pprb3.getBeanexp() +"' ,"
                        + "'" + pprb3.getBeanncand() +"')";

                String sqlreq3="INSERT INTO tblrequirements (code,designation,skillset,exp,ncand)" +
                        "VALUES ('" + pprb1.getBeanprojcode() + "' , "
                        + "'" + pprb4.getBeandesg() +"' ,"
                        + "'" + pprb4.getBeanskill() +"' ,"
                        + "'" + pprb4.getBeanexp() +"' ,"
                        + "'" + pprb4.getBeanncand() +"')";


                s1.executeUpdate(sqlproj);
                System.out.println("Proj updation successful!");
                s2.executeUpdate(sqlreq1);
                System.out.println("Req1 updation successful!");
                s3.executeUpdate(sqlreq2);
                System.out.println("Req2 updation successful!");
                s4.executeUpdate(sqlreq3);
                System.out.println("Req3 updation successful!");
            }

        } catch (Exception e){

            out.println("Failed!"+ e);
            resstat=0;
        }

        if(resstat==1)
            return true;
        else
            return false;
    }

}

Only 1st 2 queries get executed (ie Console o/p is: Proj updation successful! Req1 updation successful!");

Here are my two table structures

Table tblproject:

Field        Type           Null     Key
-----------------------------------------
name        varchar(100)    YES         
code        varchar(100)    NO       PRI        
location    varchar(100)    YES         
type        varchar(100)    YES         
lastdate    varchar(11)     YES     

Table tblrequirements:

Field        Type           Null     Key
-----------------------------------------
code         varchar(100)    NO         
designation  varchar(100)   YES         
skillset     varchar(100)   YES         
exp          int(11)         NO         
ncand        int(11)         NO         
slno         varchar(45)     NO       PRI       

Where am I going wrong?

Please try to execute multiple insert statement as batch. Example given below

        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO ....  VALUES(?,?)");

        pstmt.setString(1, "some value 1");
        pstmt.setString(2, "other value 1");
        pstmt.addBatch();

        pstmt.setString(1, "some value 2");
        pstmt.setString(2, "other value 2");
        pstmt.addBatch();

        pstmt.setString(1, "some value 3");
        pstmt.setString(2, "other value 3");
        pstmt.addBatch();

        pstmt.executeBatch();

In your code SQL statements will get evaluated multiple time for no reason. Also not really readable and simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM