我目前在使用建議的PareparedStatement類的java servlet中運行下面的代碼段。它目前將庫存數據插入到會計系統的SQL Server數據庫中。我有兩個問題。java servlets:更好的方式來做多個插入和鎖定表?
- 因爲insert語句將數據插入到3個不同的表中,我應該將這些sql語句移動到數據庫上的tsql過程調用並使用Transaction來鎖定表嗎?
- 每當用戶打開會計軟件清單屏幕時,最後2個插入語句失敗。我猜測軟件客戶端在用戶打開屏幕時會鎖定表格。
這裏是代碼:
try {
con = java.sql.DriverManager.getConnection(getConnectionUrl());
//get next itemkey
CallableStatement cstmt = con.prepareCall("{call spGetNextSurrogateKey (?,?)}");
cstmt.setString("iTableName","timitem");
cstmt.registerOutParameter("oNewKey", java.sql.Types.INTEGER);
cstmt.execute();
int rs4 = cstmt.getInt(2);
cstmt.close();
String query = "insert into timitem (itemkey, AllowCostOvrd,AllowDecimalQty,AllowDropShip,AllowPriceOvrd,AllowRtrns,AvailForSale,CompanyID,CreateDate,CreateUserID,CreateType,DateEstab,DfltSaleQty,HazMat,InclOnPackList,InternalLongDesc,IntrnlDeliveryReq,ItemClassKey,ItemID,ItemSubType,ItemType,MinGrossProfitPct,MinSaleQty,PerUsageOrdLimit,PriceSeq,PriceUnitMeaskey,PurchProdLineKey,PurchUnitMeasKey,RcptReq,RestockRate,SaleMultiple,SalesUnitMeasKey,Seasonal,ShelfLife,Status,STaxClasskey,StdPrice,StdUnitCost,StockUnitMeasKey,SubjToTradeDisc,TargetMargin,TrackMeth,UpdateCounter,ValuationMeth,WarrantyDays,WarrantyProvider) values ('" +rs4 + "', 0,0,1,1,1,1,'ens','" + DateFormat.format(Date) + "','admin',1,'" + DateFormat.format(Date) + "',1,0,1,0,0,"+itemclasskey+",'" + partnumber + "',1,5,0,1,0,0,112,"+PurchProdLineKey+","+UnitMeasKey+",1,0,0,112,0,0,1,12,"+ itemlistprice + ","+itemcost + ",112,0,0,2,0,5,0,0)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.executeUpdate();
pstmt.close();
String query_descrip = "insert into timitemdescription (itemkey, languageid, longdesc, shortdesc) values ('" + rs4 + "', 1033, '" + itemdescription + "','"+ "_" + "')";
PreparedStatement pstmt2 = con.prepareStatement(query_descrip);
pstmt2.executeUpdate();
pstmt2.close();
String query_UOM = "insert into timItemUnitOfMeas (itemkey, TargetUnitMeasKey, conversionfactor, unitvolume, unitweight,upc,useforpurchases,useforsales,usestdconv) values ('" + rs4 + "', "+UnitMeasKey+", '1',0,0,NULL,0,0,0)";
PreparedStatement pstmt3 = con.prepareStatement(query_UOM);
pstmt3.executeUpdate();
pstmt3.close();
}catch(java.sql.SQLException e){ e.printStackTrace(); } //end try
有什麼建議?提前致謝。
,我可以更好地在JPA閱讀起來比較有什麼建議? – phill 2009-05-22 18:32:48