How to pass Table-Valued parameters from java to sql server stored procedure?

后端 未结 1 1391
攒了一身酷
攒了一身酷 2020-11-30 08:11

I have a Student class with the following attributes:

Name, Department, Address, Grade. 

Now I have an ArrayList

相关标签:
1条回答
  • 2020-11-30 08:43

    With the inputs provided by Mark Rotteveel I was able to do it. Thanks Mark, Sean thanks for your input as well. Here is the working code for any of you that may find it useful.

    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();
    
    0 讨论(0)
提交回复
热议问题