JavaWeb教程(六)——数据库登录、查询完善

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>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值