JSP operation MySQL query / insert / delete function example

Time:2021-1-30

Paste the code directly:
First, index_ test.jsp The code of the page is as follows:

Copy codeThe code is as follows:
<%@ page language=”java” pageEncoding=”utf-8″%>
<%@ page contentType=”text/html;charset=utf-8″%>&nbsp;
<%&nbsp;
&nbsp; &nbsp;request.setCharacterEncoding(“UTF-8”);&nbsp;
&nbsp; &nbsp;response.setCharacterEncoding(“UTF-8”);&nbsp;
&nbsp; &nbsp;response.setContentType(“text/html;charset=utf-8”);&nbsp;
%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
< title > personnel operation
<META http-equiv=Content-Type content=”text/html; charset=utf-8″>
<style>
#popDivAdd,#popDivDelete {
position: absolute;
visibility: hidden;
overflow: hidden;
border: 2px solid #AEBBCA;
background-color: #EEF1F8;
cursor: move;
padding: 1px;
}

#popTitle {
background: #9DACBF;
height: 20px;
line-height: 20px;
padding: 1px;
}

#popForm {
padding: 2px;
}

.title_left {
font-weight: bold;
padding-left: 5px;
float: left;
}

.title_right {
float: right;
}

#popTitle .title_right a {
color: #000;
text-decoration: none;
}

#popTitle .title_right a:hover {
text-decoration: underline;
color: #FF0000;
}
</style>
<script>
Function showpop (type) {/ / pop up layer
//Var objdiv = null; / / objdiv is the target Div
if(type == ‘add’){
objDiv = document.getElementById(“popDivAdd”);
}
else
if(type = ‘delete‘){
objDiv = document.getElementById(“popDivDelete”);
}
objDiv.style.top =”50px”; / / set the distance between the pop-up layer and the upper boundary
objDiv.style.left =”200px”; / / set the distance between the pop-up layer and the left boundary
objDiv.style.width =”300px”; / / set the width of the pop-up layer
objDiv.style.height =”220px”; / / set the height of the pop-up layer
//objDiv.style.display = “block”;
objDiv.style.visibility = “visible”;
}
Function hidepopup (type) {/ / close layer
//Var objdiv = null; / / objdiv is the target Div
if(type == ‘add’){
objDiv = document.getElementById(“popDivAdd”);
}
else&nbsp;
if(type = ‘delete’){
objDiv = document.getElementById(“popDivDelete”);
}
objDiv.style.visibility = “hidden”;
}
</script>
</head>
<body>
<! — form div — >
<form action=”insert_test.jsp” method=”post”>
<p>
&nbsp;ID :<input type=”text” name=”id” value=”0″ /></br>&nbsp;
Name:</br>
Gender: < input type = text “name = sex” value = / ></br>
Age: < input type = text “name = age” value = / ></br>
</p>&nbsp;
&Nbsp; & nbsp; & nbsp; < input type = “submit” value = “submit” / > & nbsp; & nbsp;
&Nbsp; & nbsp; < input type = “reset” value = “reset” / > & nbsp; & nbsp;
&Nbsp; & nbsp; < input type = reset “value = cancel” onclick = hidepopup (‘add ‘) “/ >
</form>
</div>
</div>
< div > <! — delete personnel’s div — >
< div > <! — Title div — >
<span>Delete personnel
< a (?) “onclick =” hidepopup (‘delete ‘); > Close < / a ></span>
</div>
< div > <! — form div — >
<form action=”delete_test.jsp” method=”post”>
<p>
&nbsp;ID :<input type=”text” name=”id” value=”” /> </br>&nbsp;
Name:</br>
</p>&nbsp;
&Nbsp; & nbsp; & nbsp; < input type = “submit” value = “submit” / > & nbsp; & nbsp;
&Nbsp; & nbsp; < input type = “reset” value = “reset” / > & nbsp; & nbsp;
&Nbsp; & nbsp; < input type = reset “value = cancel” onclick = hidepopup (‘delete ‘) “/ >
</form>
</div>
</div>
<p>
<input name=”” type=”button” onclick=” location.href = ‘query_ test.jsp ‘”value =” query’ / > & nbsp; & nbsp;
< input name = “type =” button “onclick =” showpop (‘add ‘) “value =” / > & nbsp; & nbsp;
< input name = “type =” button “onclick =” showpop (‘delete ‘) “value =” delete “/ >
</p>

<script type=”text/javascript”>
/*————————-Left mouse button drag———————*/
/*——–When you do not need to implement this function, you can delete this part of the code————*/
var objDiv = document.getElementById(“popDiv”);
var isIE = document.all ? true: false; / / judge browser type
document.onmousedown =Function (evnt) {/ / this function is executed when the left mouse button is pressed
var evnt = evnt ? evnt : event;
if (evnt.button == (document.all ? 1 : 0)) {
Moused = true; / / moused is the status flag of the left mouse button. If it is true, it means that the left mouse button is pressed
}
}

objDiv.onmousedown = function(evnt) {
Objdrag = this; / / objdrag is the dragged object
var evnt = evnt ? evnt : event;
if (evnt.button == (document.all ? 1 : 0)) {
mx = evnt.clientX;
my = evnt.clientY;
objDiv.style.left = objDiv.offsetLeft + “px”;
objDiv.style.top = objDiv.offsetTop + “px”;
if (isIE) {
objDiv.setCapture();
// objDiv.filters.alpha . opacity = 50; / / the transparency changes when the mouse is pressed
} else {
window.captureEvents ( Event.MOUSEMOVE ); / / capture mouse drag events
// objDiv.style.opacity =5; / / when the mouse is pressed, the transparency changes
}
}
}
document.onmouseup = function() {
Moused = false; / / left release
objDrag = “”;
if (isIE) {
objDiv.releaseCapture();
// objDiv.filters.alpha . opacity = 100; / / the transparency changes when the left mouse button is released
} else {
window.releaseEvents ( objDiv.MOUSEMOVE ); / / release the mouse drag event
// objDiv.style.opacity =1; / / when the left mouse button is released, the transparency changes
}
}

document.onmousemove = function(evnt) {
var evnt = evnt ? evnt : event;
if (mouseD == true && objDrag) {
var mrx = evnt.clientX – mx;
var mry = evnt.clientY – my;
objDiv.style.left = parseInt(objDiv.style.left) + mrx + “px”;
objDiv.style.top = parseInt(objDiv.style.top) + mry + “px”;
mx = evnt.clientX;
my = evnt.clientY;
}
}
</script>
</body>
</html>

query_ test.jsp The page code is as follows:

Copy codeThe code is as follows:
<%@ page language=”java” import=”java.util.*,java.sql.*” pageEncoding=”utf-8″%>
<%@ page contentType=”text/html;charset=utf-8″%>
<%
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html; charset=utf-8”);
%>
<html>
<body>
<%
Connection con=null;
String url=”jdbc:mysql://localhost/html_db?user=root&password=123456″;
//html_ DB is the database name
Class.forName (” org.gjt.mm . mysql.Driver “). Newinstance(); / / create a new instance
Connection conn= DriverManager.getConnection (URL); / / establish a connection
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql=”select * from person_tb”;
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
ID:<%=rs.getString(“id”)%> </br>
Name: <%= rs.getString (“name”)%></br>
Gender: <%= rs.getString (“sex”)%></br>
Age: <%= rs.getString (“age”)%></br></br>
<%}%>
<% out.print “Congratulations! Database operation is successful! “);%>
<%
rs.close();
stmt.close();
conn.close();
%>
</br>
<input name=”” type=”button” onclick=” location.href = ‘index_ test.jsp ‘”value =” return’ / >
</body>
</html>

insert_ test.jsp The page code is as follows:

Copy codeThe code is as follows:
<%@ page language=”java” import=”java.util.*,java.sql.*” pageEncoding=”utf-8″%>
<%@ page contentType=”text/html;charset=utf-8″%>
<%
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html; charset=utf-8”);
%>
<htnl>
<head>
< title > insert data into database
</head>
<body>
<%
String id= request.getParameter (“Id”); / / obtained from the form
String name= request.getParameter (“name”); / / obtained from the form
String sex= request.getParameter (“sex”); / / obtained from the form
String age= request.getParameter (“age”); / / obtained from the form

try
{
/**Connection database parameters**/
String driverName = ” com.mysql.jdbc . driver “; / / driver name
String dbuser = “root”; / / MySQL user name
String dbpasswd = “123456”; / / MySQL password
String DBName = “html_ DB “; / / database name

String connUrl = “jdbc:mysql://localhost/” + DBName + “?user=” + DBUser + “&password=” + DBPasswd;
Class.forName(driverName).newInstance();
Connection conn = DriverManager.getConnection(connUrl);
Statement stmt = conn.createStatement();
stmt.executeQuery(“SET NAMES UTF8”);
String insert_sql = “insert into person_tb values(‘” + id + “‘,'” + name + “‘,'” + sex + “‘,'” + age + “‘)”;
String query_sql = “select * from person_tb”;

try {
stmt.execute(insert_sql);
}catch(Exception e) {
e.printStackTrace();
}
try {
ResultSet rs = stmt.executeQuery(query_sql);
while(rs.next()) {
%>
ID:<%=rs.getString(“id”)%> </br>
Name: <%= rs.getString (“name”)%> </br>
Gender: <%= rs.getString (“sex”)%> </br>
Age: <%= rs.getString (“age”)%> </br> </br>
<%
}
}catch(Exception e) {
e.printStackTrace();
}
//rs.close();
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
%>
</br>
<input name=”” type=”button” onclick=” location.href = ‘index_ test.jsp ‘”value =” return’ / > & nbsp; & nbsp;
</body>
</html>

delete_ test.jsp The code of the page is as follows:

Copy codeThe code is as follows:
<%@ page language=”java” import=”java.util.*,java.sql.*” pageEncoding=”UTF-8″%>
<%@ page contentType=”text/html;charset=UTF-8″%>
<%&nbsp;
&nbsp; &nbsp;request.setCharacterEncoding(“UTF-8”);&nbsp;
&nbsp; &nbsp;response.setCharacterEncoding(“UTF-8”);&nbsp;
&nbsp; &nbsp;response.setContentType(“text/html; charset=UTF-8”);&nbsp;
%>
<htnl>
<head>
< title > delete personnel
</head>
<body>
<%
String id= request.getParameter (“Id”); / / get from the form & nbsp;
String name= request.getParameter (“name”); / / obtained from the form
try&nbsp;
{&nbsp;
/**Connection database parameters**/
String driverName = ” com.mysql.jdbc . driver “; / / driver name
String dbuser = “root”; / / MySQL user name
String dbpasswd = “hel610”; / / MySQL password
String DBName = “html_ DB “; / / database name
String connUrl = “jdbc:mysql://localhost/” + DBName + “?user=” + DBUser + “&password=” + DBPasswd;
String query_sql = “select id,name,sex,age from person_tb”;
String delete_sql = “”;
Class.forName(driverName).newInstance();
Connection conn = DriverManager.getConnection(connUrl);
Statement stmt = conn.createStatement();
try{&nbsp;
if(id != null && id != “”){
out.println (“deleted the person with id =” + ID + “. </br></br>”);
delete_sql = “delete from person_tb where”){
out.println (“deleted the person whose name =” + name + “. </br></br>”);
delete_sql = “delete from person_tb where name='” + name + “‘”;
}else{
out.println (please enter at least one of ID and name! </br></br>”);
}
}catch(Exception e){
e.printStackTrace();
} &nbsp;&nbsp;
try {&nbsp;
stmt.execute(delete_sql);&nbsp;
}catch(Exception e){
e.printStackTrace();
}
try {&nbsp;
ResultSet rs = stmt.executeQuery(query_sql); &nbsp;&nbsp;
while(rs.next()) {
%>
ID:<%=rs.getString(“id”)%>
< / BR > Name: <%= rs.getString (“name”)%>
< / BR > gender: <%= rs.getString (“sex”)%>
< / BR > age: <%= rs.getString (“age”)%>
</br>
</br>
<%
} &nbsp; &nbsp;
}catch(Exception e) {
e.printStackTrace();
}&nbsp;
//rs.close();
stmt.close();
conn.close();&nbsp;
}catch (Exception e) {
e.printStackTrace();
}&nbsp;
%>
</br>
<input name=”” type=”button” onclick=” location.href = ‘index_ test.jsp ‘”value =” return’ / >
</body>
</html>

Access index directly in browser_ test.jsp Page, click the delete button, fill in any one of ID or name and submit to delete:
JSP operation MySQL query / insert / delete function example 
The contents of the database before and after the deletion are shown as follows:
JSP operation MySQL query / insert / delete function example 
JSP operation MySQL query / insert / delete function example
In general, the functions of query, insert and delete have been realized.

Now the problem is: originally, the pop-up layer can be moved, but now it can’t be moved. The specific reason lies in the first sentence of the mouse drag part: VAR objdiv= document.getElementById (“popdiv”); that is, when you specify to drag an object, you do not specify it well. We’re looking for a solution. Who has a better way, please help, thank you!

Recommended Today

Analysis of super comprehensive MySQL statement locking (Part 1)

A series of articles: Analysis of super comprehensive MySQL statement locking (Part 1) Analysis of super comprehensive MySQL statement locking (Part 2) Analysis of super comprehensive MySQL statement locking (Part 2) Preparation in advance Build a system to store heroes of the Three KingdomsheroTable: CREATE TABLE hero ( number INT, name VARCHAR(100), country varchar(100), PRIMARY […]