JdbcUtils.java
package common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcUtils {
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/test3?serverTimezone=UTC&characterEncoding=UTF-8";
private static String username="root";
private static String pwd="123456";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection con=null;
try {
con=DriverManager.getConnection(url,username,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
return con;
}
public static void close(Connection con, PreparedStatement pstmt, ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
PersonDao.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import common.JdbcUtils;
import pojo.Person;
public class PersonDao {
public void savePerson(Person per) {
Connection con=null;
PreparedStatement pstmt=null;
try {
con=JdbcUtils.getConnection();
String sql="insert into person(name,pwd,sex,jiguan,description) values(?,?,?,?,?)";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, per.getName());
pstmt.setString(2, per.getPwd());
pstmt.setString(3, per.getSex());
pstmt.setString(4, per.getJiguan());
pstmt.setString(5, per.getDesc());
pstmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}finally {
JdbcUtils.close(con, pstmt,null);
}
}
public boolean login(String name,String pwd) {
//数据库校验
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=JdbcUtils.getConnection();
String sql="select * from person where name=? and pwd=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, pwd);
rs=pstmt.executeQuery();
if(rs.next()) {
return true;
}
else {
return false;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}finally {
JdbcUtils.close(con, pstmt, rs);
}
}
public List<Person> list(){
//数据库校验
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<Person> list=new ArrayList<Person>();
try {
con=JdbcUtils.getConnection();
String sql="select * from person";
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()) {
Person per=new Person();
per.setId(rs.getInt("id"));
per.setName(rs.getString("name"));
per.setPwd(rs.getString("pwd"));
per.setSex(rs.getString("sex"));
per.setJiguan(rs.getString("jiguan"));
per.setDesc(rs.getString("description"));
list.add(per);
}
return list;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}finally {
JdbcUtils.close(con, pstmt, rs);
}
}
}
ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getParameter("name") %>,登陆成功!<br>
<a href="list">查看数据</a>
</body>
</html>