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

Umakanth

I have a Student class with the following attributes:

Name, Department, Address, Grade. 

Now I have an ArrayList that contains some Student objects like this,

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"));

I need to pass this arraylist to the sql server stored procedure and insert the student object data into the table. How to best achieve this in Java? I am required to have a stored procedure.

Java version 8, Sql Server 2014 if its of any use.

Umakanth

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delphi - pass table valued parameter to SQL Server stored procedure

From Dev

Delphi - pass table valued parameter to SQL Server stored procedure

From Dev

How to pass each value of the column from a table to a stored procedure and save the result in a table in SQL-Server

From Dev

How to pass each value of the column from a table to a stored procedure and save the result in a table in SQL-Server

From Dev

How are parameters passed from SQL Server to a CLR based stored procedure?

From Dev

How to send and receive parameters to/from SQL Server stored procedure

From Dev

How to pass table parameters to SQL Server stored procedures in EF Core?

From Dev

how to change stored procedure into table valued function?

From Dev

How to read data table from SQL Server stored procedure

From Dev

How to read data table from SQL Server stored procedure

From Dev

Is it possible to pass a table valued parameter to a stored procedure and return a table result?

From Dev

Table-valued parameters into stored procedure using Hibernate

From Dev

How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

From Dev

How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

From Dev

Multiple table type parameters in sql server stored procedure

From Dev

How to use Table Adapter Fill with SQL Server stored procedure with two parameters

From Dev

How to pass column name as well as Table Name to a stored procedure as a parameter in SQL Server?

From Dev

Excel get parameters (not table) from a stored procedure SQL

From Dev

Why are table valued parameters to SQL Server stored procedures required to be input READONLY?

From Dev

Is it possible to pass a NULL into an SQL Server Stored Procedure from Typescript

From Dev

How to select stored procedure output parameters as a table in oracle sql developer

From Dev

How to call sql server stored procedure with input parameters in Qt

From Dev

How to call sql server stored procedure with input parameters in Qt

From Dev

How to create dynamic parameters in SQL Server stored procedure

From Dev

How to pass two values to a single parameter in SQL Server stored procedure?

From Dev

How to pass Nullable Bit type value into to SQL Server stored procedure?

From Dev

How to pass schema as parameter to a stored procedure in sql server?

From Dev

How to pass rows of values into SQL Server stored procedure?

From Dev

Calling MS Sql server stored procedure which has cursor and temporal table in it from java?

Related Related

  1. 1

    Delphi - pass table valued parameter to SQL Server stored procedure

  2. 2

    Delphi - pass table valued parameter to SQL Server stored procedure

  3. 3

    How to pass each value of the column from a table to a stored procedure and save the result in a table in SQL-Server

  4. 4

    How to pass each value of the column from a table to a stored procedure and save the result in a table in SQL-Server

  5. 5

    How are parameters passed from SQL Server to a CLR based stored procedure?

  6. 6

    How to send and receive parameters to/from SQL Server stored procedure

  7. 7

    How to pass table parameters to SQL Server stored procedures in EF Core?

  8. 8

    how to change stored procedure into table valued function?

  9. 9

    How to read data table from SQL Server stored procedure

  10. 10

    How to read data table from SQL Server stored procedure

  11. 11

    Is it possible to pass a table valued parameter to a stored procedure and return a table result?

  12. 12

    Table-valued parameters into stored procedure using Hibernate

  13. 13

    How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

  14. 14

    How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

  15. 15

    Multiple table type parameters in sql server stored procedure

  16. 16

    How to use Table Adapter Fill with SQL Server stored procedure with two parameters

  17. 17

    How to pass column name as well as Table Name to a stored procedure as a parameter in SQL Server?

  18. 18

    Excel get parameters (not table) from a stored procedure SQL

  19. 19

    Why are table valued parameters to SQL Server stored procedures required to be input READONLY?

  20. 20

    Is it possible to pass a NULL into an SQL Server Stored Procedure from Typescript

  21. 21

    How to select stored procedure output parameters as a table in oracle sql developer

  22. 22

    How to call sql server stored procedure with input parameters in Qt

  23. 23

    How to call sql server stored procedure with input parameters in Qt

  24. 24

    How to create dynamic parameters in SQL Server stored procedure

  25. 25

    How to pass two values to a single parameter in SQL Server stored procedure?

  26. 26

    How to pass Nullable Bit type value into to SQL Server stored procedure?

  27. 27

    How to pass schema as parameter to a stored procedure in sql server?

  28. 28

    How to pass rows of values into SQL Server stored procedure?

  29. 29

    Calling MS Sql server stored procedure which has cursor and temporal table in it from java?

HotTag

Archive