首先新建java的动态web项目
然后点击next
先看一下默认创建的文件夹然后再对default output folder做修改
最后点击finish,在这里我的项目名字叫做PP
下面的url根据自己的数据库的名字填写(这个文件是写与数据库进行连接的信息的)
下面这个图是我后来改的只看db.properties文件即可
在webapp下建一个views文件夹存放网页显示有关的.jsp文件
建一个login的.jsp文件
然后建好包
在数据库建立一个user表
这样环境就算搭好了
然后根据user表在domain中建立一个user的类写上getter and setter和toString
根据需要在创建的interface文件中定义方法然后实现
先将jdbcutil工具类写好
代码如下:
package util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class JdbcUtil {
private JdbcUtil() {}
private static Properties p = new Properties();
private static DataSource dataSource=null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
dataSource = DruidDataSourceFactory.createDataSource(p);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void close(Connection conn,Statement st,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(st!=null) {
st.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}finally {
try {
if(conn!=null) {
conn.close();
}
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
}
}
在template包中建一个IResultSetHandler的interface这个接口在dao中的impl中会按需实现的
在template中建一个jdbcTemplate该类是一个jdbc模板方便以后在dao的impl中的文件里调用实现
代码如下:
package template;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import util.JdbcUtil;
public class JdbcTemplate {
private JdbcTemplate() {}
public static int update(String sql, Object... params) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection("jdbc:mysql:///dpb","mysql用户名","密码");
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i+1, params[i]);
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtil.close(conn, ps, null);
}
return 0;
}
public static
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConn();
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i+1, params[i]);
}
rs = ps.executeQuery();
return rsh.handle(rs);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtil.close(conn, ps, rs);
}
return null;
}
}
将之前建立的IUserDAO实现
代码如下:
package dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dao.IUserDAO;
import domain.User;
import template.IResultSetHandler;
import template.JdbcTemplate;
public class Userdaoimpl implements IUserDAO {
@Override
public User get(Long id) {
String sql = "select * from User where id = ?";
List
return list.size()==1?list.get(0):null;
}
@Override
public User get(String username) {
String sql = "select * from User where username = ?";
List
return list.size()==1?list.get(0):null;
}
class UserResultSetHandler implements IResultSetHandler>{
public List
List
while(rs.next()) {
User user = new User();
list.add(user);
user.setId(rs.getInt("id"));
user.setIdentity(rs.getInt("identity"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setUsername(rs.getString("username"));
}
return list;
}
}
}
然后写一个test来测试一下
代码如下:
package test;
import dao.IUserDAO;
import dao.impl.Userdaoimpl;
public class Test {
IUserDAO dao = new Userdaoimpl();
@org.junit.Test
public void getTest() {
System.out.println(dao.get("root"));
}
}
进行测试
这就说明jdbc测试通了
接下来写页面
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
最后一步写servlet
代码如下:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.impl.Userdaoimpl;
import domain.User;
@WebServlet("/login")
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8"); //设置接受的编码格式
resp.setHeader("Access-Control-Allow-Origin", "*"); //用来和前端交互时需要加的
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = new Userdaoimpl().get(username);
if(user!=null) {
if(user.getPassword().equals(password)) {
System.out.println("登陆成功!");
} else {
System.out.println("登录失败!");
}
} else {
System.out.println("登录失败!");
}
}
}
测试结果如下:
接下里输入正确的账号密码:
好了简单的登录实现完成!!