java传值到sql decode语句,如何将表值参数从java传递到sql 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 stuList = new ArrayList();

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值