TestUserDao
package com.qiku.app;
import com.qiku.dao.UserDao;
import com.qiku.entity.User;
import org.junit.Test;
import java.sql.SQLException;
public class TestUserDao {
private UserDao userDao = new UserDao();
@Test
public void testRegister() throws SQLException {
User user = new User();
user.setUid("006");
user.setUsername("鸡你太美");
user.setPassword("123456");
user.setTelephone("1234567899");
user.setBirthday("2000-12-21");
user.setSex("男");
int register = userDao.register(user);
if (register > 0) {
System.out.println("注册成功,欢迎您:"+user.getUsername());
} else {
System.out.println("注册失败");
}
}
}
UserDao
package com.qiku.dao;
import com.qiku.entity.User;
import com.qiku.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import java.sql.SQLException;
public class UserDao {
public int register(User user) throws SQLException {
QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
String sql = "insert into user values (?,?,?,?,?,?)";
int i = qr.update(sql,
user.getUid(), user.getUsername(),
user.getPassword(), user.getTelephone(),
user.getBirthday(), user.getSex());
return i;
}
}