2009-11-11 128 views
1

我想插入值到SQL Server,但有问题是我通过两个值的参数,然后它不插入其他如果我选择一个值,然后它插入我的数据库名称是样品和表是项目插入值到SQL Server

这是完美的插入语句吗?

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(); 
    } 
%> 

回答

0

看你的第一行代码:

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

如果您收到SQL异常和patientid是某种身份字段,你可以解决,通过做stmt.executeUpdate("INSERT item (itemid) VALUES(" + LrBn.TestID + ")"); 代替。

我很抱歉,如果这不是问题,您可能需要澄清您的问题。