資料插入問題。

lulu163發表於2004-08-18

環境:jbx+tomcat4.1+MSSQL
我遇到的問題是:我想往一個表(I_item)中插入一行記錄,其中itemID為主鍵。但是並不成功。我的想法是:先從I_item讀取
itemID,再將其增1,再和其他資料一起插入表中,我不知道失敗原因,請大家指點。主要程式碼如下:



主要的是這裡:I_X.jsp 是數巨庫插入程式碼
<jsp:useBean class="firm.firm" id="s_itemDeclare" scope="page"></jsp:useBean>
<%
String sql4="select top 1 * from I_s_item order by itemID DESC";
ResultSet rs4= s_itemDeclare.executeQuery(sql4);
int z=rs4.getInt("itemID");
z++;
int x=new Integer(z);
String itemID=x.toString();
String sql="insert into I_s_item(itemID,unit_ID,item_name,sort1,sort2,begin_date,finish_date,applicant,grade,education,unit_name,fill_date,purport,status,future,point,analysis,way,moment,result,reference,market,commend_mind) values("+itemID+",'"+unit_ID+"','"+item_name+"','"+sort[0]+"','"+sort[1]+"','"+begin_date+"','"+finish_date+"','"+applicant+"','"+grade+"','"+education+"','"+unit_name+"','"+fill_date+"','"+purport+"','"+status+"','"+future+"','"+point+"','"+analysis+"','"+way+"','"+moment+"','"+result+"','"+reference+"','"+market+"','"+commend_mind+"')";
s_itemDeclare.executeUpdate(sql);
%>


firm的程式碼:
package firm;
import java.sql.*;

public class firm {

ResultSet rs=null;
private Connection conn=null;
private Statement stmt=null;
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shen";
public firm()
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("firm():" + e.getMessage());

}

}


public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(url,"sa","sa");
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(url,"sa","sa");//建立資料庫連線物件
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}


public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}

}

主要的是插入資料庫的程式碼,請大俠幫看看,謝謝!

相關文章