title: Using NamedParameterJdbcTemplate to send data to DataBase
body: <pre><code>package com.techm.template;
  import java.sql.Types;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.sql.DataSource;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  import org.springframework.jdbc.core.namedparam.SqlParameterSource;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  
  public class InsertRecordInDatabaseWithJdbcTemplate {
  
      private static final String driverClassName = "oracle.jdbc.driver.OracleDriver";
  
      private static final String url = "jdbc:oracle:thin:@localhost:1521:xe";
  
      private static final String dbUsername = "SYSTEM";
  
      private static final String dbPassword = "9848451415";
  
      private static final String sql = "INSERT INTO employee(firstname,lastname,username,password) VALUES(?,?,?,?)";
  
  
      private static DataSource dataSource;
  
  
  
      public static void main(String[] args) throws Exception {
  
          dataSource = getDataSource();
  
          Employee e = new Employee();
          e.setFirstname("Surya Teja");
          e.setLastname("Tammana");
          e.setUsername("surya_kvm");
          e.setPassword("9848451415");
  
          int result = saveRecord(e);
          if(result != 0){
              System.out.println("Success");
          }
          else{
              System.out.println("Failure");
          }
  
      }
  
  
  
     public static int saveRecord(Employee e) {
  
         NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);
  
         SqlParameterSource params = new MapSqlParameterSource();
  
         Map&lt;String, Object&gt; parameters = new HashMap&lt;String, Object&gt;();
          parameters.put("firstnames", e.getFirstname());
          parameters.put("lastname", e.getLastname());
          parameters.put("username", e.getUsername());
          parameters.put("password", e.getPassword());
  
          int result = template.update(sql, parameters);
          return result;
     }
  
  
  
   public static DriverManagerDataSource getDataSource() {
  
   DriverManagerDataSource dataSource = new DriverManagerDataSource();
   dataSource.setDriverClassName(driverClassName);
   dataSource.setUrl(url);
   dataSource.setUsername(dbUsername);
   dataSource.setPassword(dbPassword);
   return dataSource;
  
      }
  }   
  </code></pre>
  
  <p>Employee.java</p>
  
  <pre><code>package com.techm.template;
  
  public class Employee {
  
  private String firstname;
  private String lastname;
  private String username;
  private String password;
  public String getFirstname() {
      return firstname;
  }
  public void setFirstname(String firstname) {
      this.firstname = firstname;
  }
  public String getLastname() {
      return lastname;
  }
  public void setLastname(String lastname) {
      this.lastname = lastname;
  }
  public String getUsername() {
      return username;
  }
  public void setUsername(String username) {
      this.username = username;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }
  
  
  
  
  }
  </code></pre>
  
  <p>Console</p>
  
  <p>Jan 22, 2017 12:25:24 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
  INFO: Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
  Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: SQL [INSERT INTO employee(firstname,lastname,username,password) VALUES(?,?,?,?)]: given 4 parameters but expected 0
      at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.(PreparedStatementCreatorFactory.java:221)
      at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.(PreparedStatementCreatorFactory.java:199)
      at org.springframework.jdbc.core.PreparedStatementCreatorFactory.newPreparedStatementCreator(PreparedStatementCreatorFactory.java:173)
      at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator(NamedParameterJdbcTemplate.java:351)
      at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:287)
      at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:292)
      at com.techm.template.InsertRecordInDatabaseWithJdbcTemplate.saveRecord(InsertRecordInDatabaseWithJdbcTemplate.java:64)
      at com.techm.template.InsertRecordInDatabaseWithJdbcTemplate.main(InsertRecordInDatabaseWithJdbcTemplate.java:40)</p>