tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > JDBC > Insert Record

Insert Record 

Spring JDBC Framework simplifies the use of JDBC and helps to avoid common errors. The following example shows inserting a new record using JdbcTemplate class.

File Name  :  
/SpringJDBC001/conf/basic/applicationContext.xml 

File Name  :  
com/bethecoder/tutorials/spring3/tests/InsertTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class InsertTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    /**
     * Initialize context and get the JdbcTemplate
     */
    ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = (DataSourceappContext.getBean("dataSource");
    JdbcTemplate template = new JdbcTemplate(dataSource);

    int userId = 6;
    template.update("insert into USER values (?, ?, ?, ?)",
        userId, "Steve"66666666);
    
    Map<String, Object> row = template.queryForMap(
        "select * from USER where ID = ? ", userId);
    System.out.println(row);
  }

}
   

It gives the following output,
{ID=6, NAME=Steve, AGE=66, SALARY=666666}



 
  


  
bl  br