import java.sql.*;
import java.sql.Connection;
public class Conn {
static String url = "jdbc:oracle:thin:@localhost:1521:oradb";
static String user = "username";
static String pass = "password";
public static Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(Conn.url, Conn.user,
Conn.pass);
return conn;
} catch (Exception ms) {
ms.printStackTrace();
return null;
}
}
public static void main(String ss[]) {
Connection conn = Conn.getConnection();
}
}
public class TableTest {
int id;
String name;
String attribute1;
String attribute2;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAttribute1() {
return attribute1;
}
public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}
public String getAttribute2() {
return attribute2;
}
public void setAttribute2(String attribute2) {
this.attribute2 = attribute2;
}
}
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OraclePreparedStatement;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
public class OracleStoredProcedure {
private final String ORACLE_STRUCT = "T_TYPE";
private final String ORACLE_ARRAY = "TB_T_TYPE";
public void insertAll(List
Connection conn = null;
StructDescriptor structDescriptor = null;
ArrayDescriptor arrayDescriptor = null;
int iSize = records.size();
Object[] arrObj = null;
Object[][] recObj = null;
try {
conn = Conn.getConnection();
structDescriptor = StructDescriptor.createDescriptor(ORACLE_STRUCT,
conn);
arrayDescriptor = ArrayDescriptor.createDescriptor(ORACLE_ARRAY,
conn);
arrObj = new Object[3];
recObj = new Object[iSize][3];
// Structuring obj and arrays
for (int j = 0; j < iSize; j++) {
TableTest ob = (TableTest) records.get(j);
recObj[j][0] = ob.name;
recObj[j][1] = ob.attribute1;
recObj[j][2] = ob.attribute2;
// arrObj[j] = new STRUCT(structDescriptor, conn, recObj[j]);
}
ARRAY arr = new ARRAY(arrayDescriptor, conn, recObj);
oracle.jdbc.OraclePreparedStatement preparedStatement = (OraclePreparedStatement) conn
.prepareStatement("{call POPULATE_TABLE_TEST (?)}");
preparedStatement.setArray(1, arr);
preparedStatement.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
public static void main(String a[]) {
List
TableTest ab = new TableTest();
ab.setId(1);
ab.setName("user1");
ab.setAttribute1("m1");
ab.setAttribute1("m2");
list.add(ab);
TableTest abc = new TableTest();
abc.setId(2);
abc.setName("user2");
abc.setAttribute1("a1");
abc.setAttribute1("a2");
list.add(abc);
new OracleStoredProcedure().insertAll(list);
}
}
1 comment:
Excellent example, this is exactly what I have been looking for. Thank you!
Post a Comment