title: How to insert data from bean to the database in Spring MVC?
body: <p>I'm having user class for which contains user data like</p>
  
  <p>user.java</p>
  
  <pre><code>@Table(name="user");
  @Column(name="userid")
  public String getUserId() {
      return userId;
  }
  
  public void setUserId(String userId) {
      this.userId = userId;
  }//setter and getter for username,password and mobile no.
  </code></pre>
  
  <p>I'm having signup page where I have to enter some of above details,after successful login whatever the response i m getting that again storing to this user object but now I want to insert it into the database.this is my</p>
  
  <p>userdao.java</p>
  
  <pre><code>package com.xyz.dao;
  
  import com.xyz.model.User;
  
  public class UserDao {
      public interface UserDAO {
          public void insert(User user);
      }
  } 
  </code></pre>
  
  <p>and this is my </p>
  
  <pre><code>import com.xyz.dao.UserDao.UserDAO;
  import com.xyz.model.User;
  
  public class UserJdbcImpl implements UserDAO {
      private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
      private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/user";
      private static final String DB_USER = "root";
      private static final String DB_PASSWORD = "root";
      private DataSource dataSource;
  
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      public void insert(User user) {
          Connection conn = null;
          PreparedStatement preparedStatement = null;
          String sql = "INSERT INTO user " +
                  "(userMobile,userEmail ) VALUES (?, ?)";
  
          try {
              conn = getDBConnection();
              preparedStatement = conn.prepareStatement(sql);
              //conn = dataSource.getConnection();
              System.out.println("connecting..");
              PreparedStatement ps = conn.prepareStatement(sql);
  
              ps.setString(1, user.getUserMobile());
              ps.setString(2, user.getUserEmail());
  
              ps.executeUpdate();
              ps.close();
  
          } catch (SQLException e) {
              throw new RuntimeException(e);
  
          } finally {
              if (conn != null) {
                  try {
                      conn.close();
                  } catch (SQLException e) {
                      System.out.println("connection failed");
                  }
              }
          }
      }
  
      private static Connection getDBConnection() {
  
          Connection conn = null;
  
          try {
              Class.forName(DB_DRIVER);
          } catch (ClassNotFoundException e) {
              System.out.println(e.getMessage());
          }
  
          try {
              conn = DriverManager.getConnection(DB_CONNECTION, DB_USER,DB_PASSWORD);
              return conn;
          } catch (SQLException e) {
              System.out.println(e.getMessage());
          }
  
          return conn;
      }
  }
  </code></pre>
  
  <p>As I'm going for authentication in the login page I'm using spring-security.xml and this is my spring-db.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/user" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="root" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>After running, no data is inserting into the database. Any help where I'm doing wrong?</p>