给定一个简单的存储过程,它有一个以单列整数作为参数的表:
CREATE PROCEDURE [dbo].[table_sel]
@tbl INT32Table READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT value FROM @tbl
END
如何从 Java 执行此存储过程?从 C# 中,这可以满足我的要求:
SqlConnection sqlconn;
System.Data.DataTable tbl = new System.Data.DataTable("INT32Table", "dbo");
tbl.Columns.Add("value", typeof(int));
tbl.Rows.Add(2);
tbl.Rows.Add(3);
tbl.Rows.Add(5);
tbl.Rows.Add(7);
tbl.Rows.Add(11);
using (SqlCommand command = new SqlCommand("table_sel"))
{
command.Connection = sqlconn;
command.Parameters.AddWithValue("@tbl", tbl);
command.CommandType = System.Data.CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
//do stuff
}
哦对不起,
据我所知,Java 中没有这样的表对象,您可以使用 cs.setObject() 但您应该从某个地方获取该对象(在 mssql 服务器上查询?)。
如果您正在寻找使用 JDBC 插入的性能,那么您应该考虑准备语句和批量执行。
java.sql.connection connection = //driver, url, database, credentials ...
try
{
PreparedStatement ps =
connection.prepareStatement("insert into tbl values (?)");
ps.setInt(1, your 1st int);
ps.addBatch();
ps.setInt(1, your 2nd int);
ps.addBatch();
ps.setInt(1, your 3rd int);
ps.addBatch();
ps.executeBatch();
}
catch (SQLException e)
{
// err handling goes here
}
finally
{
// close your resources
}
问候
S
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)