如何将 Table 值参数从 java 传递到 sql 服务器存储过程?
How to pass Table-Valued parameters from java to sql server stored procedure?
我有一个 Student
class 具有以下属性:
Name, Department, Address, Grade.
现在我有一个 ArrayList
,其中包含一些 Student
个像这样的对象,
List<Student> stuList = new ArrayList<Student>();
stuList.add(new Student("Tom","Comp", "123 street", "A"));
stuList.add(new Student("Jery","Comp", "456 street", "A+"));
stuList.add(new Student("Mac","Maths", "Dum Street", "B"));
我需要将此数组列表传递给 sql 服务器存储过程并将学生对象数据插入 table。
如何在 Java 中最好地实现这一点?我需要有一个存储过程。
Java 版本 8,Sql Server 2014(如果有用的话)。
根据 Mark Rotteveel I was able to do it. Thanks Mark, Sean 提供的意见,也感谢您的意见。以下是可能对您有用的工作代码。
String jdbcurl = "jdbc:sqlserver://TestServer:1433;DatabaseName=Student";
connection = DriverManager.getConnection(jdbcurl,"username","password");
SQLServerDataTable stuTypeDT = new SQLServerDataTable();
stuTypeDT.addColumnMetadata("StudentId", java.sql.Types.NUMERIC);
stuTypeDT.addColumnMetadata("Name", java.sql.Types.VARCHAR);
stuTypeDT.addColumnMetadata("Department", java.sql.Types.VARCHAR);
stuTypeDT.addColumnMetadata("Address", java.sql.Types.VARCHAR);
stuTypeDT.addRow("1","Tom", "A", "123 Street");
stuTypeDT.addRow("2","Jery", "B", "456 Street");
stuTypeDT.addRow("3","Mac", "C", "Vancour");
String ececStoredProc = "EXEC InsertStudentInfo ?";
SQLServerPreparedStatement pStmt = (SQLServerPreparedStatement)connection.prepareStatement(ececStoredProc);
pStmt.setStructured(1, "dbo.StudentInfoType", stuTypeDT);
pStmt.execute();
我有一个 Student
class 具有以下属性:
Name, Department, Address, Grade.
现在我有一个 ArrayList
,其中包含一些 Student
个像这样的对象,
List<Student> stuList = new ArrayList<Student>();
stuList.add(new Student("Tom","Comp", "123 street", "A"));
stuList.add(new Student("Jery","Comp", "456 street", "A+"));
stuList.add(new Student("Mac","Maths", "Dum Street", "B"));
我需要将此数组列表传递给 sql 服务器存储过程并将学生对象数据插入 table。 如何在 Java 中最好地实现这一点?我需要有一个存储过程。
Java 版本 8,Sql Server 2014(如果有用的话)。
根据 Mark Rotteveel I was able to do it. Thanks Mark, Sean 提供的意见,也感谢您的意见。以下是可能对您有用的工作代码。
String jdbcurl = "jdbc:sqlserver://TestServer:1433;DatabaseName=Student";
connection = DriverManager.getConnection(jdbcurl,"username","password");
SQLServerDataTable stuTypeDT = new SQLServerDataTable();
stuTypeDT.addColumnMetadata("StudentId", java.sql.Types.NUMERIC);
stuTypeDT.addColumnMetadata("Name", java.sql.Types.VARCHAR);
stuTypeDT.addColumnMetadata("Department", java.sql.Types.VARCHAR);
stuTypeDT.addColumnMetadata("Address", java.sql.Types.VARCHAR);
stuTypeDT.addRow("1","Tom", "A", "123 Street");
stuTypeDT.addRow("2","Jery", "B", "456 Street");
stuTypeDT.addRow("3","Mac", "C", "Vancour");
String ececStoredProc = "EXEC InsertStudentInfo ?";
SQLServerPreparedStatement pStmt = (SQLServerPreparedStatement)connection.prepareStatement(ececStoredProc);
pStmt.setStructured(1, "dbo.StudentInfoType", stuTypeDT);
pStmt.execute();