通过jdbc对数据库一个表单实现查询、修改、增加和删除

查询操作

代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>




Insert title here

			
 
<%
String username=(String)application.getAttribute("sousuo"); 
String sn=" ";
String name=" ";
String gender=" ";
String class1=" ";
try {
	Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
	e.printStackTrace();
}
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8","root","123456");
PreparedStatement pStmt=con.prepareStatement("select * from t_student where c_sn= ? ");
pStmt.setString(1,username);
ResultSet rs=pStmt.executeQuery();
while(rs.next()){
	sn=rs.getString(2);
	name=rs.getString(3);
	gender=rs.getString(4);
	class1=rs.getString(5);

}
rs.close();
pStmt.close();
con.close();
%>





<%@ page language="java" contentType="textml; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
 String name=new String(request.getParameter("name").getBytes("utf-8"),"utf-8"); 
 String applicationMessage=name;
application.setAttribute("sousuo",applicationMessage); 
out.print("查询成功");
%>

实验结果
通过jdbc对数据库一个表单实现查询、修改、增加和删除_第1张图片

修改操作

代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>




Insert title here






<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.sql.*"%>
<%
 	String sn = new String(request.getParameter("sn").getBytes("UTF-8"), "UTF-8");
	String name = new String(request.getParameter("name").getBytes("UTF-8"), "UTF-8"); 
	String gender= new String(request.getParameter("gender").getBytes("UTF-8"), "UTF-8");
	String class1 = new String(request.getParameter("class1").getBytes("UTF-8"), "UTF-8"); 	

		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {		
			e.printStackTrace();
		}
	Connection conn = DriverManager.getConnection(
			"jdbc:mysql://127.0.0.1:3306/jwdb?autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
	PreparedStatement pt = conn.prepareStatement("update t_student set c_name=?,c_gender=?,c_class=? where c_sn=?");
	pt.setString(4, sn);	
	pt.setString(1,name);
	pt.setString(2, gender);
	pt.setString(3, class1);
	int rtn = pt.executeUpdate();
	pt.close();
	conn.close(); 
%>

通过jdbc对数据库一个表单实现查询、修改、增加和删除_第2张图片
修改结果
通过jdbc对数据库一个表单实现查询、修改、增加和删除_第3张图片

增加操作

代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here






<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.sql.*"%>
<%
	String sn = new String(request.getParameter("sn").getBytes("UTF-8"), "UTF-8");
	String name = new String(request.getParameter("name").getBytes("UTF-8"), "UTF-8"); 
	String gender= new String(request.getParameter("gender").getBytes("UTF-8"), "UTF-8");
	String class1 = new String(request.getParameter("class1").getBytes("UTF-8"), "UTF-8"); 	

 try {
		Class.forName("com.mysql.cj.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		
		e.printStackTrace();
	}


	Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8","root","123456");

	PreparedStatement stmt=con.prepareStatement("insert into t_student (c_sn,c_name,c_gender,c_class)values(?,?,?,?)");

	stmt.setString(1, sn);
	stmt.setString(2, name);
	stmt.setString(3, gender);
	stmt.setString(4, class1);
	int rtn=stmt.executeUpdate();
	stmt.close();
	con.close();
 
	
%>

通过jdbc对数据库一个表单实现查询、修改、增加和删除_第4张图片
添加结果
在这里插入图片描述

删除

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.sql.*"%>
<%
	String sn = new String(request.getParameter("sn").getBytes("UTF-8"), "UTF-8");
 try {
		Class.forName("com.mysql.cj.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		
		e.printStackTrace();
	}

	Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8","root","123456");
	PreparedStatement stmt=con.prepareStatement("delete from t_student where c_sn=?");
	stmt.setString(1, sn);
	int rtn=stmt.executeUpdate();
	stmt.close();
	con.close();	
%>

通过jdbc对数据库一个表单实现查询、修改、增加和删除_第5张图片

你可能感兴趣的:(数据库)