1.创建数据库表
create table m_student(
stu_id int primary key auto_increment,
stu_name varchar(20),
stu_pass varchar(20),
stu_age int,
stu_address varchar(30)
);
2.创建javaweb项目StudentManger2,[修改Content Root为student2],修改工程访问名称
3.导入第三方依赖【druid-1.1.10.jar mysql-connector-java-5.1.38-bin.jar】
4.在src下创建数据库连接配置文件【xxxxxxxx.properties】
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
initialSize=100
maxActive=300
maxWait=60000
5.创建javabean
package com.wangxing.student.bean;
/**
* 保存学生信息的类
* @author 14336
*
*/
public class StudentBean {
private int stuid;
private String stuname;
private String stupass;
private int stuage;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStupass() {
return stupass;
}
public void setStupass(String stupass) {
this.stupass = stupass;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
6.创建数据访问接口以及实现类
package com.wangxing.student.dao;
import java.util.List;
import com.wangxing.student.bean.StudentBean;
/**
* 定义增删改查方法接口类
* 学生信息的数据访问接口
* @author 14336
*
*/
public interface StudentDao {
/**
* 添加学生信息
* @param student
* @throws Exception
*/
public void insertStudent(StudentBean student)throws Exception;
/**
* 修改学生信息
* @param student
* @throws Exception
*/
public void updateStudent(StudentBean student)throws Exception;
/**
* 删除学生信息
* @param stuid
* @throws Exception
*/
public void deleteStudent(int stuid)throws Exception;
/**
* 根据ID查询学生信息
* @param stuid
* @return
* @throws Exception
*/
public StudentBean selectStudentById(int stuid)throws Exception;
/**
* 查询所有学生信息
* @return
* @throws Exception
*/
public List<StudentBean> selectStudent()throws Exception;
/**
* 根据用户名和密码验证登录
* @param studentBean
* @return
* @throws Exception
*/
public boolean selectStudentByNameAndPass(StudentBean studentBean) throws Exception;
}
package com.wangxing.student.dao.impl;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.wangxing.student.bean.StudentBean;
import com.wangxing.student.dao.StudentDao;
/**
* 学生信息的访问方法具体实现类
* @author 14336
*
*/
public class StudentDaoImpl implements StudentDao{
private static StudentDaoImpl studentDao = null;
//1.定义保存druid数据源的对象
private DataSource dataSource = null;
//创建数据源,建立数据库连接
//一个私有的构造方法--单例模式
private StudentDaoImpl() {
if (dataSource == null) {
try {
//如果数据源为空,创建数据源
Properties properties = new Properties();
InputStream inStream = this.getClass().getResourceAsStream("/mysqldata.properties");
properties.load(inStream);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//提供一个获得本类对象的方法
public static StudentDaoImpl getStudentDaoImpl(){
if (studentDao==null) {
studentDao = new StudentDaoImpl();
}
return studentDao;
}
//添加学生信息
@Override
public void insertStudent(StudentBean student) throws Exception {
//1.获得数据库连接对象
Connection connection = dataSource.getConnection();
//2.创建sql语句
String insert = "insert into m_student values(null,?,?,?,?)";
//3.预编译sql语句
PreparedStatement preparedStatement = connection.prepareStatement(insert);
//传递数据
preparedStatement.setString(1, student.getStuname());
preparedStatement.setString(2, student.getStupass());
preparedStatement.setInt(3, student.getStuage());
preparedStatement.setString(4, student.getStuaddress());
//将sql语句传递给数据库,并接收返回的数据
int temp = preparedStatement.executeUpdate();
//关闭数据库操作对象,和数据库连接对象
preparedStatement.close();
connection.close();
}
//修改学生信息的方法
@Override
public void updateStudent(StudentBean student) throws Exception {
//获取连接对象
Connection connection = dataSource.getConnection();
//创建预编译的sql语句
String update = "update m_student set stu_name=?,stu_pass=?,stu_age=?,stu_address=? where stu_id=?";
PreparedStatement preparedStatement = connection.prepareStatement(update);
preparedStatement.setString(1, student.getStuname());
preparedStatement.setString(2, student.getStupass());
preparedStatement.setInt(3, student.getStuage());
preparedStatement.setString(4, student.getStuaddress());
preparedStatement.setInt(5, student.getStuid());
//将预编译好的sql语句传递给数据库,并接收返回信息
int temp = preparedStatement.executeUpdate();
//关闭数据库操作对象
preparedStatement.clos