How to pass table parameters from a stored procedure of a Java server?

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 achieve this in Java? I must have a stored procedure.

Java version 8, Sql Server 2014, if used.

+7
java sql-server sql-server-2008 jdbc mssql-jdbc
source share
1 answer

With the Mark Rotteveel tabs, I was able to do this. Thanks Mark, thanks Sean for your input. Here is a working code for any of you that may be 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(); 
+10
source share

All Articles