views:

197

answers:

2

I want to insert value into SQL Server but there is problem is I pass both value in parameter then it's not insert otherewise if i selecting one value then it's insert my database name is sample and table is item

this is perfect insert statement or not ?

try
{
    int val = stmt.executeUpdate("INSERT item (patientid,itemid) VALUES(nPatientID," + LrBn.TestID+ ")");
    out.println("1 row affected");
}
catch (SQLException s)
{
   System.out.println("SQL statement is not executed!");
}

<%
if(testname!=null)
    {
         LrBn.beginInsert();
        for(int i=0; i<testname.length; i++)
          {
            nCount++ ;
              LrBn.ResultID=0;
               try
            { 
                 LrBn.TestID = Integer.parseInt(testname[i]) ;
             } catch( NumberFormatException ex)
             { 
                 LrBn.TestID = 0 ;
             }

          LrBn.GroupID = nGroupID ;
          LaBn.locateRecord(LrBn.TestID) ;

         short nemergencyType =  com.hims.emergencyType.normal ;
         try
         {
            nemergencyType = Short.parseShort(request.getParameter("emergencyType"));
           }
         catch( NumberFormatException ex)
         { 
          nemergencyType =  com.hims.emergencyType.normal ;
         }  
             LrBn.Emergency = nemergencyType;
           LrBn.ResultType =  LaBn.TestResultType ;
          LrBn.PatientID = nPatientID ;
          LrBn.DoctorID = LogUsr.DoctorID;
            LrBn.UnitID = LogUsr.UnitID ;
          LrBn.RequestTime = com.webapp.utils.DateHelper.requestDateTime(request, "RequestTime"); 
               LrBn.CollectionTime = null;
             LrBn.ResultTime = null;
          LrBn.CollectedBy = 0 ;
             LrBn.TestDoneBy = 0 ;
             LrBn.PathologyUnitID = 0 ;
          LrBn.BoolValue = 0;
          LrBn.ScalarValue = null ;
          LrBn.DescValue = null ;
          LrBn.TestStatus = com.hims.TestStatusType.REQUESTED ;
          LrBn.TestCharges = LaBn.TestCharge ;
             LrBn.PaymentStatus = com.hims.PaymentStatus.PENDING ;        
          LrBn.continueInsert();

         Class.forName("net.sourceforge.jtds.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/sample", "sa", "sa1234");
         java.sql.Statement stmt = conn.createStatement();
         try
         {
             int val = stmt.executeUpdate("INSERT item (patientid,itemid) VALUES(nPatientID," + LrBn.TestID+ ")");
          out.println("1 row affected");
          }
          catch (SQLException s)
         {
                 System.out.println("SQL statement is not executed!");
          }
         stmt.close();
         conn.close();

        } // end for
       LrBn.endInsert();
    }
%>
A: 

Looking at your first line of code:

int val = stmt.executeUpdate("INSERT item (patientid,itemid) VALUES(nPatientID,"
        + LrBn.TestID+ ")");

If you are getting SQL Exceptions AND patientid is some sort of IDENTITY field you can solve that by doing stmt.executeUpdate("INSERT item (itemid) VALUES(" + LrBn.TestID + ")"); instead.

I apologize if this wasn't the issue, you may have to clarify your question.

ajdams
A: 

i have problem in inserting in sqlserver but there was some problem in int val2 = stmt.executeUpdate("INSERT BillingRequestDetails(BRequestID,ItemID) VALUES(+ID+,+nechoType+)");this code please give me solution for that

Class.forName("net.sourceforge.jtds.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://172.168.0.1:1433/synergy", "sa", "makarand"); java.sql.Statement stmt = conn.createStatement(); try {

        int val = stmt.executeUpdate("INSERT BillingRequestHeader (PatientID) VALUES("+nPatientID+")");
       //int val1 = stmt.executeUpdate("INSERT BillingRequestDetails(ItemID) VALUES("+nechoType+")");
       java.sql.ResultSet rslt = stmt.executeQuery("SELECT MAX(ID) FROM BillingRequestHeader");
          while(rslt.next())
          {
            int ID = rslt.getInt(1);

       int val2 = stmt.executeUpdate("INSERT  BillingRequestDetails(BRequestID,ItemID) VALUES(+ID+,+nechoType+)");
       out.println(nechoType);
       out.println(ID);
       out.println("1 row affected");
          }             

       rslt.close();
      }
      catch (SQLException s)
     {
             System.out.println("SQL statement is not executed!");
      }

     stmt.close();
     conn.close();
nilesh
Better 1) post it as a new question if it's not an answer to this one, 2) Say what the problem is you are trying you solve, and what goes wrong where in your code at the moment, 3) format you code, at the moment it's absolutely unreadable.
sth