我正在尝试学习Spring MVC。我有一个注册表单,我试图坚持。我有MySQL作为后端。我有一个测试java应用程序,它能够连接到MySQL并插入到表中。但同样的事情对Web应用程序不起作用。我使用Eclipse作为IDE,Maven用于构建,tomcat用作webserver。
数据源文件
<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-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/chorechart" />
<property name="username" value="username" />
<property name="password" value="password*" />
</bean>
DAO
package com.chorechart.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.sql.DataSource;
import org.springframework.stereotype.Service;
import com.chorechart.dao.RegisterDAO;
import com.chorechart.model.Register;
@Service("registerDAO")
public class JdbcRegisterDAO implements RegisterDAO{
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void insertUser(Register registerUser) {
String sql = "Insert into register"+"
(FirstName,LastName,Email_username,password)"
+ "values(?,?,?,?)";
);
try {
System.out.println("DataSourrce"+dataSource);
Connection conn = dataSource.getConnection();
System.out.println("coonection"+conn);
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1,registerUser.getFirstName());
ps.setString(2,registerUser.getLastName());
ps.setString(3, registerUser.getEmail_username());
ps.setString(4, registerUser.getPassword());
int x = ps.executeUpdate();
}catch(Exception ex) {
System.out.print("JDBCRegisterDao");
ex.printStackTrace();
}
}
}
@Override
public Register findUser(String userName, String userPwd) {
// TODO Auto-generated method stub
return null;
}
public DataSource getDataSource() {
return dataSource;
}