gpt4 book ai didi

java - Spring MVC中如何将bean中的数据插入数据库?

转载 作者:行者123 更新时间:2023-11-29 23:48:04 25 4
gpt4 key购买 nike

我有一个用户类,其中包含用户数据,例如

用户.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>

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

最佳答案

据我所知,该交易并未提交或打开。您可能习惯默认情况下 mysql 的自动提交处于打开状态。要么在代码中处理事务启动/提交/回滚,要么打开自动提交。我不建议自动提交,除非您有充分的理由并理解其含义。

关于java - Spring MVC中如何将bean中的数据插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25807418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com