tariq tariq - 1 month ago 6
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

dispatch.xml
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" />
</bean>


Here is the DAO implementation-

@Component
public class PerformDaoImplmnt implements PerformDao {
@Autowired
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 = ?";

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

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

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

}

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

}


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

@Controller
public class Register {

@Autowired
PerformDao userDao;
@RequestMapping(value="/processRegistration", method=RequestMethod.POST)
public ModelAndView processRegistrationForm(@Valid @ModelAttribute("user") UserData user, BindingResult result,
RedirectAttributes redirect){
if(result.hasErrors()){
ModelAndView backToRegisterPage = new ModelAndView("register");
return backToRegisterPage;
}
ModelAndView profilePage = new ModelAndView();
profilePage.setViewName("redirect:profile");
String something = userDao.insert(user);
redirect.addFlashAttribute("user",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

Here you will find complete solution spring JDBC.