Trivial create, insert with jdbctemplate - spring

I'm trying to explore the basics of Spring's jdbctemplate. I'm having trouble with a simple example:
public static void main(String[] args) throws Exception {
JdbcDataSource dataSource = new JdbcDataSource();
String url = "jdbc:h2:mem:test";
dataSource.setURL(url);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table something (id int primary key, name varchar(100))");
jdbcTemplate.execute("insert into something (id, name) values (1, 'Brian')");
}
Triggers this:
Caused by: org.h2.jdbc.JdbcSQLException: Table "SOMETHING" not found; SQL statement:
insert into something (id, name) values (1, 'Brian') [42102-177]
If I convert that code to use the raw JDBC api, it works fine:
public static void main(String[] args) throws Exception {
JdbcDataSource dataSource = new JdbcDataSource();
String url = "jdbc:h2:mem:test";
dataSource.setURL(url);
try (Connection dbConnection = dataSource.getConnection()) {
try (Statement statement = dbConnection.createStatement()) {
statement.execute("create table something (id int primary key, name varchar(100))");
statement.execute("insert into something (id, name) values (1, 'Brian')");
try (ResultSet rs = statement.executeQuery("select id, name from something")) {
while (rs.next()) {
System.out.println(String.format("got result. id=%d. name=%s", rs.getInt(1), rs.getString(2)));
}
}
}
}
}
If it matters, my Gradle dependencies:
compile 'org.springframework:spring-jdbc:4.0.6.RELEASE'
compile 'com.h2database:h2:1.4.177'

please try to use update method instead execute.

Related

SpringBoot+JDBCTemplate+Parent Key not found error

Unable to insert into child table in a single transaction using springboot and jdbctemplate.
Controller:
#RequestMapping(value = "/addUser", method = RequestMethod.POST)
public Response addUser(#RequestBody UserVo userVo) throws Exception
{
service.addUserRecord(userVo);
}
Service Layer:
#Autowired
private Dao dao;
#Transactional(readOnly = false)
public void addUserRecord(#RequestBody UserVo userVo) throws Exception
{
int parentSeqNo = dao.addUser();
int result = dao.addUserDetails(parentSeqNo, list);
}
DAOImple class:
#Autowired
private JdbcTemplate jdbcTemplate;
public int addUser()
{
try
{
String sql = "INSERT INTO user_table() VALUES (?,?,?,?,?) ";
jdbcTemplate.update(sql,....);
return seqNo;
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public void addUserDetails(String seqNo, List<String> list){
String sql="insert into user_details_table values(?,?)";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
String img = list.get(i);
ps.setString(1, seqNo);
ps.setString(2, img);
}
public int getBatchSize() {
return list.size();
}
});
return;
}
Issue/Error:
ORA-02291: integrity constraint (FK3) violated - parent key not found; nested exception is java.sql.SQLIntegrityConstraintViolationException:
Database tables and constraints are looks good and tried to insert using sql editor, just working fine. While testing from postman tool, getting parent key not found exception. Greatly appreciated for any suggestions on this issue.

InstantiationException when using SQLData and OracleCallableStatement Struct Mapping

I cannot figure out an answer to the following problem. Hope somebody can help me. I am mapping JAVA class to a Struct as described here:
http://docs.oracle.com/cd/F49540_01/DOC/java.815/a64685/samapp4.htm
I have an Oracle Object:
create or replace TYPE DK1 AS OBJECT( zahl CHAR(1) );
and corresponding JAVA class:
public class DK1 implements SQLData {
private String sql_type;
public static final int _SQL_TYPECODE = OracleTypes.STRUCT;
private String zahl;
public DK1(String sql_type, String z) {
this.sql_type = sql_type;
setZahl(z);
}
public String getSQLTypeName() throws SQLException {
return sql_type;
}
public void readSQL(SQLInput stream, String typeName) throws SQLException {
sql_type = typeName;
this.setZahl(stream.readString());
}
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(getZahl());
}
public String getSql_type() {
return sql_type;
}
public void setSql_type(String sql_type) {
this.sql_type = sql_type;
}
public String getZahl() {
return zahl;
}
public void setZahl(String zahl) {
this.zahl = zahl;
}
}
my test method is the following:
public class SQLDataExample {
public static void main(String args[]) throws Exception {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
OracleConnection conn = (OracleConnection) DriverManager.getConnection(
"jdbc:oracle:thin:#................", "...",
"...");
Dictionary<String, Class<?>> map = (Dictionary) conn.getTypeMap();
map.put("BONI.DK1", Class.forName("com.gwb.db.objects.DK1"));
OracleCallableStatement cs = (OracleCallableStatement) conn.prepareCall("{call BOX.DK(?)}");
DK1 dd = new DK1("BONI.DK1", "1");
cs.setObject(1, dd);
cs.registerOutParameter(1, OracleTypes.STRUCT, "BONI.DK1");
cs.execute();
DK1 df = (DK1) cs.getObject(1);
}
}
Now, the last step
DK1 df = (DK1) cs.getObject(1);
in this procedure fails and although I have tried so many things in the last couple of days, I cannot get it to run! I get a
Exception in thread "main" java.sql.SQLException: Inconsistent Java-
ans SQL-Objecttypes: InstantiationException: com.gwb.db.objects.DK1
If I replace getObject with getSTRUCT I see that the DB procedure works and returns values as expected. I cannnot figure out why a I unable to map a JAVA Object.
I would be very grateful for any help or tipps!
Thank you in advance
I forgot the default constructor
public DK1() { }

What is proper way to use PreparedStatementCreator of Spring JDBC?

As per my understanding the use of PreparedStatement in Java is we can use it multiple times.
But I have some confusion using PreparedStatementCreator of Spring JDBC.
For example consider following code,
public class SpringTest {
JdbcTemplate jdbcTemplate;
PreparedStatementCreator preparedStatementCreator;
ResultSetExtractor<String> resultSetExtractor;
public SpringTest() throws SQLException {
jdbcTemplate = new JdbcTemplate(OracleUtil.getDataSource());
preparedStatementCreator = new PreparedStatementCreator() {
String query = "select NAME from TABLE1 where ID=?";
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement(query);
}
};
resultSetExtractor = new ResultSetExtractor<String>() {
public String extractData(ResultSet resultSet) throws SQLException,
DataAccessException {
if (resultSet.next()) {
return resultSet.getString(1);
}
return null;
}
};
}
public String getNameFromId(int id){
return jdbcTemplate.query(preparedStatementCreator, new Table1Setter(id), resultSetExtractor);
}
private static class Table1Setter implements PreparedStatementSetter{
private int id;
public Table1Setter(int id) {
this.id =id;
}
#Override
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setInt(1, id);
}
}
public static void main(String[] args) {
try {
SpringTest springTest = new SpringTest();
for(int i=0;i<10;i++){
System.out.println(springTest.getNameFromId(i));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
As per this code when I called springTest.getNameFromId(int id) method, it returns name from given id, Here I've used PreparedStatementCreator for creating PreparedStatement and PreparedStatementSetter for setting input parameters and I got result from ResultSetExtractor.
But performance is very slow.
After debugging and looking into what happens inside PreparedStatementCreator and JdbcTemplate I got to know that PreparedStatementCreator creates each and every time new PreparedStatement...!!!
Each and every time when I am calls method jdbcTemplate.query(preparedStatementCreator, preparedStatementSetter, resultSetExtractor), it creates new PreparedStatement and this slow downs performance.
Is this right way to use PreparedStatementCreator? Because in this code I unable to reuse PreparedStatement. And if this is right way to use PreparedStatementCreator than how to get benefit of re-usability of PreparedStatement?
Prepared Statements are usually cached by underlying connection pool, so you don't need to worry about creating a new one every time or not.
So I think that your actually usage is correct.
JdbcTemplate closes the statement after executing it, so if you really want to reuse the same prepared statement you could proxy the statement and intercept the close method in the statement creator
For example (not tested, only as example):
public abstract class ReusablePreparedStatementCreator implements PreparedStatementCreator {
private PreparedStatement statement;
public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
if (statement != null)
return statement;
PreparedStatement ps = doPreparedStatement(conn);
ProxyFactory pf = new ProxyFactory(ps);
MethodInterceptor closeMethodInterceptor = new MethodInterceptor() {
#Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return null; // don't close statement
}
};
NameMatchMethodPointcutAdvisor closeAdvisor = new NameMatchMethodPointcutAdvisor();
closeAdvisor.setMappedName("close");
closeAdvisor.setAdvice(closeMethodInterceptor);
pf.addAdvisor(closeAdvisor);
statement = (PreparedStatement) pf.getProxy();
return statement;
}
public abstract PreparedStatement doPreparedStatement(Connection conn) throws SQLException;
public void close() {
try {
PreparedStatement ps = (PreparedStatement) ((Advised) statement).getTargetSource().getTarget();
ps.close();
} catch (Exception e) {
// handle exception
}
}
}
You are on the right way to use PreparedStatementCreator.
In each new transaction, you should create brand new PreparedStatement instance, it's definitely correct. PreparedStatementCreator is mainly designed to wrap the code block to create PreparedStatement instance easily, not saying that you should resue the new instance each itme.
PreparedStatement is mainly designed to send the templated and pre-compiled SQL statement DBMS which will save some pre-compiled time for SQL execution.
To summarize, what you did is correct. use PreparedStatement will have better performance than Statement.
After debugging and looking into what happens inside PreparedStatementCreator and JdbcTemplate I got to know that PreparedStatementCreator creates each and every time new PreparedStatement...!!!
I'm not sure why that's so shocking since it's your own code that creates a new PreparedStatement each time by calling connection.prepareStatement(query);. If you want to reuse the same one, then you shouldn't create a new one.

Incorrect number of arguments for PROCEDURE; expected 1, got 0. Cant determine the error from code

//set input parameters
Map<String,Object> inParams = new HashMap<String,Object>();
inParams.put("Sig",resourceHistoryBean.getId());
List<ResourceHistoryBean> resourceHistoryList= new ArrayList<ResourceHistoryBean>();
// define stored procedure
try{
SimpleJdbcCall readResult = new SimpleJdbcCall(getDataSource())
.useInParameterNames("Sig")
.declareParameters(new SqlParameter("Sig", Types.VARCHAR))
.withProcedureName("SP_ResourceAllocationDtls")
.withSchemaName("hrms")
.returningResultSet("ResourceHistory", new ParameterizedRowMapper<ResourceHistoryBean>() {
public ResourceHistoryBean mapRow(ResultSet rs, int rowNum)
throws SQLException {
ResourceHistoryBean bean = new ResourceHistoryBean();
resourceHistoryBean.setProjectName(rs.getString(RH_PROJECT_NAME));
return bean;
}
});
readResult.compile();
// execute stored procedure
Map<String, Object> out = readResult.execute(inParams);
resourceHistoryList = (List<ResourceHistoryBean>) out.get("ResourceHistory");
Looks like I was able to find an alternative solution to above problem (Parameter passing to stored procedure and use a mapping class as well ):
public List<ResourceHistoryBean> getResourceHistory(final ResourceHistoryBean resourceHistoryBean)throws Exception{
try {
// call stored procedure and pass parameter to it
List resourceHistoryList = getJdbcTemplate().query(
"call hrms.SP_ResourceAllocationDtls(?)",
new Object[] {resourceHistoryBean.getId()}, new HistoryMapper());
return resourceHistoryList;
} catch (Exception e) {
throw e;
} finally {
closeTemplate();
}
}
// mapper class
class HistoryMapper implements RowMapper, IDatabaseConstants {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
ResourceHistoryBean resourceHistoryBean = new ResourceHistoryBean();
resourceHistoryBean.setProjectName(rs.getString(RH_PROJECT_NAME));
return resourceHistoryBean;
}
}

How to call Oracle function or stored procedure using spring persistence framework?

I am using Spring persistence framework for my project.
I want to call oracle function or stored procedure from this framework.
Can anybody suggest how can I achieve this.
Please give solution for both * oracle function and *stored procedure.
Thanks.
Assuming you are referring to JdbcTemplate:
jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) throws SQLException{
CallableStatement cs = con.prepareCall("{call MY_STORED_PROCEDURE(?, ?, ?)}");
cs.setInt(1, ...); // first argument
cs.setInt(2, ...); // second argument
cs.setInt(3, ...); // third argument
return cs;
}
},
new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException{
cs.execute();
return null; // Whatever is returned here is returned from the jdbcTemplate.execute method
}
}
);
Calling a function is almost identical:
jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) {
CallableStatement cs = con.prepareCall("{? = call MY_FUNCTION(?, ?, ?)}");
cs.registerOutParameter(1, Types.INTEGER); // or whatever type your function returns.
// Set your arguments
cs.setInt(2, ...); // first argument
cs.setInt(3, ...); // second argument
cs.setInt(4, ...); // third argument
return cs;
}
},
new CallableStatementCallback {
public Object doInCallableStatement(CallableStatement cs) {
cs.execute();
int result = cs.getInt(1);
return result; // Whatever is returned here is returned from the jdbcTemplate.execute method
}
}
);
Simpler way of calling a Oracle function in Spring is subclassing StoredProcedure like below
public class MyStoredProcedure extends StoredProcedure{
private static final String SQL = "package.function";
public MyStoredProcedure(DataSource ds){
super(ds,SQL);
declareParameter(new SqlOutParameter("param_out",Types.NUMERIC));
declareParameter(new SqlParameter("param_in",Types.NUMERIC));
setFunction(true);//you must set this as it distinguishes it from a sproc
compile();
}
public String execute(Long rdsId){
Map in = new HashMap();
in.put("param_in",rdsId);
Map out = execute(in);
if(!out.isEmpty())
return out.get("param_out").toString();
else
return null;
}
}
And call it like this
#Autowired DataSource ds;
MyStoredProcedure sp = new MyStoredProcedure(ds);
String i = sp.execute(1l);
The Oracle function used here just takes in a numeric parameter and returns a numeric paramter.
In my opinion this is one of the easiest approaches:
public class ServRepository {
private JdbcTemplate jdbcTemplate;
private SimpleJdbcCall functionGetServerErrors;
#Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.setResultsMapCaseInsensitive(true);
this.functionGetServerErrors = new SimpleJdbcCall(jdbcTemplate).withFunctionName("THIS_IS_YOUR_DB_FUNCTION_NAME").withSchemaName("OPTIONAL_SCHEMA_NAME");
}
public String callYourFunction(int parameterOne, int parameterTwo) {
SqlParameterSource in = new MapSqlParameterSource().addValue("DB_FUNCTION_INCOMING_PARAMETER_ONE", parameterOne).addValue("DB_FUNCTION_INCOMING_PARAMETER_TWO", parameterTwo);
return functionGetServerErrors.executeFunction(String.class, in);
}
}
Calling function using NamedParameterJdbcTemplate:
final String query = "select MY_FUNCTION(:arg1, :arg2, :arg3) from dual";
Map<String, Object> argMap = new HashMap<>();
argMap.put("arg1", "value1");
argMap.put("arg2", 2);
argMap.put("arg3", "value3");
final String result = new NamedParameterJdbcTemplate(dataSource)
.queryForObject(query, argMap, String.class);
Calling procedure using JdbcTemplate:
final String query = "call MY_PROCEDURE(?, ?, ?)";
final Object[] args = {"arg1", "arg2", "arg3"};
new JdbcTemplate(dataSource).execute(query, args, String.class);
Calling function using SimpleJdbcCall:
Map<String, Object> inParameters = new HashMap<>();
inParameters.put("arg1", 55); // arg1 value
inParameters.put("arg2", 20); // arg2 value
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(inParameters);
BigDecimal result = new SimpleJdbcCall(dataSource)
.withCatalogName("MY_PACKAGE")
.withSchemaName("MY_SCHEMA")
.withFunctionName("MY_FUNCTION")
.executeFunction(BigDecimal.class, mapSqlParameterSource);
Calling procedure using SimpleJdbcCall:
new SimpleJdbcCall(dataSource)
.withCatalogName("MY_PACKAGE")
.withProcedureName("MY_PROCEDURE")
.execute("arg1", arg2);

Resources