如何在Spring MVC中将数据从bean插入数据库?

时间:2014-09-12 11:37:07

标签: java mysql spring spring-mvc javabeans

我的用户类包含用户数据,如

user.java

@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.

我有注册页面,我必须输入上面的一些细节,成功登录后无论响应我再次存储到此用户对象但现在我想将其插入数据库。这是我的

userdao.java

package com.xyz.dao;

import com.xyz.model.User;

public class UserDao {
    public interface UserDAO {
        public void insert(User user);
    }
} 

这是我的

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;
    }
}

由于我要在登录页面进行身份验证,我使用的是spring-security.xml,这是我的spring-db.xml

<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">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/user" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
</beans>

运行后,没有数据插入数据库。我做错了什么帮助?

1 个答案:

答案 0 :(得分:0)

据我所知,交易并未就此事进行评审或开放。您可能默认使用mysql的自动提交功能。在代码中处理事务启动/提交/回滚或打开自动提交。除非你有充分的理由并理解其含义,否则我不建议自动提交。