Servlet连接数据库进行数据的增删改查

1.连接数据库,以及关闭数据库连接

import java.sql.*;

public class JdbcUtils {

	public static Connection getConnection() {
		Connection con = null;
		try {
			Class.forName("org.postgresql.Driver");
			con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "postgres");
			System.out.println("数据库驱动加载成功,数据库连接成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	// 关闭结果集对象
	public static void close(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 关闭预编译对象
	public static void close(PreparedStatement pstmt) {
		try {
			if (pstmt != null) {
				pstmt.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 关闭结果集对象
	public static void close(Connection con) {
		try {
			if (con != null) {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


2.构造个类People.java

public class People {
	private int id;
	private String name;
	private String sex;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
}

3.查询数据

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		ResultSet res = null;
		PreparedStatement pstmt = null;
		Connection con = null;
		
		try {
		    con = JdbcUtils.getConnection();
		    pstmt = con.prepareStatement("select * from credit");
			ResultSet res = sql.executeQuery();
			while (res.next()) {
				int id = res.getInt("id");
				String name = res.getString("name");
				String sex = res.getString("sex");
				System.out.print("id:"+id);
				System.out.print("  姓名:"+name);
				System.out.println("  性别:"+sex);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(res);
			JdbcUtils.close(pstmt);
			JdbcUtils.close(con);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	public void destroy() {
	}
}

4.新增数据

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PreparedStatement pstmt = null;
		Connection con = null;
		   
		String name = "博客";
		String sex =  "男";
		
		try {
		    con = JdbcUtils.getConnection();
		    pstmt = con.prepareStatement("insert into credit(name,sex) values(?,?)");
			pstmt .setString(1, name);
			pstmt .setString(2, sex);
			pstmt .executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(pstmt);
			JdbcUtils.close(con);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	public void destroy() {
	}
}

5.修改数据

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	
		String updateid = "2";
		String name = "博客";
		String sex = "男";
		
		PreparedStatement pstmt = null;
		Connection con = null;

		try {
		    con = JdbcUtils.getConnection();
			pstmt  = con.prepareStatement("update credit set name=?,sex=? where id=?");
			sql.setString(1, name);
			sql.setString(3, sex);
			sql.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(pstmt);
			JdbcUtils.close(con);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	public void destroy() {
	}
}

6.删除数据

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String deleteid = request.getParameter("deleteid");
		
		PreparedStatement pstmt = null;
		Connection con = null;
		
		try {
		    con = JdbcUtils.getConnection();
			pstmt = con.prepareStatement("delete from credit where id=?");
			pstmt .setInt(1, Integer.parseInt(deleteid));
			pstmt .executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(pstmt);
			JdbcUtils.close(con);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	public void destroy() {
	}
}

本篇文章只是写出了方法和逻辑,至于数据如何处理和显示根据自己的要求写

发布了25 篇原创文章 · 获赞 14 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览