Questions tagged [simplejdbcinsert]

SimpleJdbcInsert is a Spring component. It defines a multi-threaded, reusable object providing easy insert capabilities for a table. It provides meta data processing to simplify the code needed to construct a basic insert statement.

A SimpleJdbcInsert is a multi-threaded, reusable object providing easy insert capabilities for a table. It provides meta data processing to simplify the code needed to construct a basic insert statement. All you need to provide is the name of the table and a Map containing the column names and the column values.

The meta data processing is based on the DatabaseMetaData provided by the JDBC driver. As long as the JBDC driver can provide the names of the columns for a specified table than we can rely on this auto-detection feature. If that is not the case then the column names must be specified explicitly.

The actual insert is being handled using Spring's JdbcTemplate.

Many of the configuration methods return the current instance of the SimpleJdbcInsert to provide the ability to string multiple ones together in a "fluid" interface style.

Example Usage

public class JdbcActorDao implements ActorDao {
    private SimpleJdbcTemplate simpleJdbcTemplate;
    private SimpleJdbcInsert insertActor;

    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
        this.insertActor =
                new SimpleJdbcInsert(dataSource)
                        .withTableName("t_actor")
                        .usingGeneratedKeyColumns("id");
    }

    public void add(Actor actor) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("first_name", actor.getFirstName());
        parameters.put("last_name", actor.getLastName());
        Number newId = insertActor.executeAndReturnKey(parameters);
        actor.setId(newId.longValue());
    }

    //  ... additional methods
}

See the reference documentation for more examples.

13 questions
7
votes
5 answers

SimpleJdbcInsert equivalent for update

I am using Spring's SimpleJdbcInsert class to create entities - eg: final SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("abc"); insert.execute(new BeanPropertySqlParameterSource(abc)); Is there some equivalent of this…
James Hargreaves
  • 415
  • 1
  • 3
  • 15
6
votes
1 answer

Spring SimpleJdbcInsert vs JdbcTemplate

I have a requirement where I have to insert a row into the Database and get the Key (Identity) back. I thought of using SimpleJdbcInsert for this. I am passing the Object of JdbcTemplate to my SimpleJdbcInsert and executing method…
user2004685
  • 9,548
  • 5
  • 37
  • 54
5
votes
2 answers

Specify Oracle Sequence in SimpleJdbcInsert object to generate key from Oracle Sequence

I am using SimpleJdbcInsert as, SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName(TABLE_NAME).withSchemaName(SCHEMA_NAME); Map namedParameterMap = new HashMap
3
votes
2 answers

replace on duplicate with simpleJdbcInsert

I use tomcat for my server side with Mysql Server 5.5. i use Spring framework for the database connectivity. I would like to be able to insert a row to a table using simpleJdbcInsert. if the insert fails because of a duplicate i want it to replace…
ufk
  • 30,912
  • 70
  • 235
  • 386
1
vote
0 answers

SimpleJdbcInsert to table with dot in name

I am trying to use SimpleJdbcInsert to a table that happens to have a dot character in its name with dbo as default schema. I pass in a map of key values to be inserted. insertMessage = new…
David CL
  • 11
  • 2
0
votes
4 answers

update automatically after insert is successful in java

i need to update a table automatically after the insert of another table is successful. i am using prepared statement to do this, and tried couple of ways to do but dint work out. can some one please help me out on this. the code is given following …
Dennis
  • 1
0
votes
0 answers

simpleJdbcInsert not inserting the default constraint value mentioned on SQL table

Below is the table creation script CREATE TABLE [dbo].[Employee](     [empId] [int] IDENTITY(9,1) NOT NULL,     [empName] VARCHAR(255) NOT NULL,     [empOrgId] [int] NOT NULL, ) ALTER TABLE [dbo].[Employee] ADD  CONSTRAINT…
0
votes
0 answers

42884 error when calling DB2 from Java through SimpleJDBCall

trying to call a DB2 procedure from java through spring: SimpleJdbcCall call = new SimpleJdbcCall(dataSource) .withSchemaName("schemaName") .withProcedureName("proc1") .declareParameters( new SqlParameter("param2", Types.CHAR), new…
Parameswar
  • 1,951
  • 9
  • 34
  • 57
0
votes
2 answers

Is it possible to use Oracle's sysdate with jdbcTemplate

So far I tried jdbcTemplate.update("INSERT INTO INFO (id, my_date) "VALUES(?, ?)", 1, "sysdate"); Also tried with namedParameterJdbcTemplate by just using a map. Map namedParameters = new HashMap(); namedParameters.put("id",…
user2342259
  • 345
  • 2
  • 9
  • 27
0
votes
0 answers

SimpleJdbcInsert issue with Boolean field

I am trying to insert a record to the database using spring API SimpleJdbcInsert... But, its creating issue when model object carrying Boolean variable...If I set the Boolean variable as true, its actually inserting NULL value to the database... My…
rahul shalgar
  • 1,198
  • 5
  • 24
  • 39
0
votes
1 answer

How to insert a row to DB in spring, using only auto-generated values, and return an ID?

I use spring framework, and would like to insert a row to DB in order to have its ID, which is needed for further processing. Inserting query is given below, it's a simple query, without parameters. But it's even worse, cause how to construct…
Szymon Roziewski
  • 956
  • 2
  • 20
  • 36
0
votes
1 answer

columns are getting repeated when using simplejdbcinsert statement

I am using SimpleJdbcInsert to store records, when I run the following code it gives me a exception. When I see the logs I found that spring is generating an insert query with doubling all the parameters. Please say where the problem…
0
votes
2 answers

SimpleJdbcInsert - Not inserting data in tables in default schema

I am struggling with a code that uses Spring's SimpleJdbcInsert. SimpleJdbcInsert jdbcInsert = (new SimpleJdbcInsert(transactionManager.getDataSource()) …
partha
  • 2,286
  • 5
  • 27
  • 37