登录页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login">
姓名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录" >
</form>
</body>
</html>
创建Servlet
public class LoginServlet extends HttpServlet {
@Override //重写init方法 该方法是通过ServletContext上下文对象设置count值来完成登录人数的增加功能
public void init() throws ServletException {
int count = 0;
this.getServletContext().setAttribute("count", count); //设置域属性
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得请求中的属性值
String name = request.getParameter("name");
String password = request.getParameter("password");
//从数据库中获取用户名的值 这里用了C3P0连接池
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from user where name =? and password=?";
User user_bean = null;
try {
User user = queryRunner.query(sql, new BeanHandler<User>(User.class), name,password);
user_bean = user;
ServletContext servletContext = this.getServletContext();
if(user_bean!=null) {
//获得域属性
Integer count = (Integer) servletContext.getAttribute("count");
count++; //count值自增
servletContext.setAttribute("count", count); //设置域属性
response.getWriter().write("good "+user_bean+"<br>"+"是第"+count+"位");
}
else {
response.getWriter().write("bad");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
C3P0连接池的使用
使用前需要先导入三个jar包
之后将C3P0配置文件写入 注意要放入src目录下 这个是必须的 名称必须为c3p0-config.xml
//C3P0配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
<named-config name="oracle">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///</property>
<property name="user">root</property>
<property name="password">root</property>
</named-config>
</c3p0-config>
写完配置文件后 写C3P0Utils工具类
public class C3P0Utils {
public static ComboPooledDataSource pd = new ComboPooledDataSource();
public static DataSource getDataSource() {
return pd;
}
public static Connection getConnection() {
try {
return pd.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
查询操作需要封装一个实体类对象 在这里用的User
public class C3P0Utils {
public static ComboPooledDataSource pd = new ComboPooledDataSource();
public static DataSource getDataSource() {
return pd;
}
public static Connection getConnection() {
try {
return pd.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}