2. 创建数据库环境
CREATE DATABASE test;
USE test;
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(32) UNIQUE NOT NULL,
password VARCHAR(32) NOT NULL,
address VARCHAR(32) NOT NULL,
phone VARCHAR(32) NOT NULL
/**
* JDBC工具类,使用Durid数据库连接池
*
* @author liwenlong
* @data 2020/3/20
*/
public class JDBCUtils {
//声明连接池对象
public static DataSource ds;
static {
//加载配置文件,初始化连接池
try {
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
/**
* 操作数据库中User表的类
*
* @author liwenlong
* @data 2020/3/19
*/
public class UserDao {
//声明JDBCTemplate对象供类中方法使用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDateSource());
public User login(User loginUser) { //形参loginUser为只有用户名和密码的User
//编写SQL语句
String sql = "select * from user where name=? and password =?";
//调用query方法,执行SQL语句
User user = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class),
loginUser.getName(), loginUser.getPassword());
//返回的为含有所有信息的user
return user;
}