tariq tariq - 2 years ago 81
Java Question

How to access a specific table in mysql using spring-mvc

I am new to spring-mvc. I am using it with maven dependencies and JDBC Template.

I can access to my database but can't seem to find any answer about how I can set up my connection so that spring-mvc would put data to a specific table in that database. So in short I am trying to access a specific table through spring-mvc in same database. I tried researching but could not find any answer.

This is database access in

looks like:

<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="connectionProperties" value="useUnicode=yes;characterEncoding=utf8;" -->
<property name="username" value="root" />
<property name="password" value="root" />

Here is the DAO implementation-

public class PerformDaoImplmnt implements PerformDao {
DataSource dataSource;

private static final String SQL_INSERT = "insert into user (name, password, email, area, phone) values (?, ?, ?, ?, ?)";
private static final String SQL_UPDATE = "update user set password=?, email=?, area=?, phone=? where name=?";
private static final String SQL_FIND_ONE = "select * from user where name = ?";
private static final String SQL_FIND_ALL = "select * from user order by phone";
private static final String SQL_DELETE = "delete from user where phone = ?";

public UserData findOne(long id) {
// TODO Auto-generated method stub
// JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return null;
public List<UserData> findAll() {
// TODO Auto-generated method stub
return null;

public String insert(UserData user) {
// TODO Auto-generated method stub
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
int row = jdbc.update(SQL_INSERT, new Object[] { user.getFullName(), user.getPass(), user.getEmail(),
user.getArea(), user.getNumber()});

if(row == 1){
return "success";
return null;

public void update(UserData user) {
// TODO Auto-generated method stub


public void delete(UserData user) {
// TODO Auto-generated method stub


After this,
This is how i am inserting data in my database-

public class Register {

PerformDao userDao;
@RequestMapping(value="/processRegistration", method=RequestMethod.POST)
public ModelAndView processRegistrationForm(@Valid @ModelAttribute("user") UserData user, BindingResult result,
RedirectAttributes redirect){
ModelAndView backToRegisterPage = new ModelAndView("register");
return backToRegisterPage;
ModelAndView profilePage = new ModelAndView();
String something = userDao.insert(user);
return profilePage;

I have two table in this "user" database. For example-

  1. admin

  2. normal

How do i access each individually. Please help. Thank you.

I apologize. I know what to do. Just change table name in query. Its a dumb question after i understood what to do. Sorry guys

Answer Source

Here you will find complete solution spring JDBC.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download