数据插入问题。

04-08-18 lulu163

环境: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();

}

}

}

主要的是插入数据库的代码,请大侠帮看看,谢谢!

banq
2004-08-21 17:40
调试需要看log文件,tomcat的是logs目录下。

单凭看程序,很少有这种慧眼的,我也没有。

猜你喜欢