Paste the Code directly:
First, the code of the index_test.jsp page is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" 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 ");
%>
<! 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 Operations </title>
<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: #9 DACBF;
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 {
Color: #000;
Text-decoration: none;
}
# PopTitle. title_right a: hover {
Text-decoration: underline;
Color: # FF0000;
}
</Style>
<Script>
Function showPopup (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"; // sets the distance between the pop-up layer and the upper boundary.
ObjDiv. style. left = "200px"; // you can specify the distance between the pop-up layer and the left boundary.
ObjDiv. style. width = "300px"; // you can specify the width of the pop-up layer.
ObjDiv. style. height = "220px"; // you can specify the height of the pop-up layer.
// ObjDiv. style. display = "block ";
ObjDiv. style. visibility = "visible ";
}
Function hidePopup (type) {// close the 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. visibility = "hidden ";
}
</Script>
</Head>
<Body>
<! -- Form div -->
<Form action = "insert_test.jsp" method = "post">
<P>
ID: <input type = "text" name = "id" value = "0"/> </br>
Name: <input type = "text" name = "name" value = "/> </br>
Gender: <input type = "text" name = "sex" value = "/> </br>
Age: <input type = "text" name = "age" value = "/> </br>
</P>
<Input type = "submit" value = "submit"/>
<Input type = "reset" value = "reset"/>
<Input type = "reset" value = "cancel" onclick = "hidePopup ('add')"/>
</Form>
</Div>
</Div>
<Div id = "popDivDelete"> <! -- Delete the div of a person -->
<Div id = "popTitle"> <! -- Title div -->
<Span class = "title_left"> delete a person </span> <span class = "title_right">
<A href = "#" onclick = "hidePopup ('delete');"> close </a> </span>
</Div>
<Div id = "popForm"> <! -- Form div -->
<Form action = "delete_test.jsp" method = "post">
<P>
ID: <input type = "text" name = "id" value = "/> </br>
Name: <input type = "text" name = "name" value = "/> </br>
</P>
<Input type = "submit" value = "submit"/>
<Input type = "reset" value = "reset"/>
<Input type = "reset" value = "cancel" onclick = "hidePopup ('delete')"/>
</Form>
</Div>
</Div>
<P>
<Input name = "" type = "button" onclick = "location. href = 'query _ test. jsp '" value = "query"/>
<Input name = "" type = "button" onclick = "showPopup ('add')" value = "add"/>
<Input name = "" type = "button" onclick = "showPopup ('delete')" value = "delete"/>
</P>
<Script type = "text/javascript">
/* ------------------------- Move the left mouse button ---------------------*/
/* -------- If you do not need to implement this function, you can delete this part of code ------------*/
Var objDiv = document. getElementById ("popDiv ");
Var isIE = document. all? True: false; // determines the browser type.
Document. onmousedown = function (evnt) {// execute this function when the left mouse button is pressed.
Var evnt = evnt? Evnt: event;
If (evnt. button = (document. all? 1: 0 )){
MouseD = true; // mouseD indicates the left mouse button status flag. If it is true, 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 the mouse drag Event
// ObjDiv. style. opacity = 0.5; // The transparency changes when the mouse is pressed.
}
}
}
Document. onmouseup = function (){
MouseD = false; // release with the left button
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; // The transparency changes when the left mouse button is released.
}
}
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>
The query_test.jsp 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 indicates the Database Name
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance (); // create an 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 rs1_stmt.exe cuteQuery (SQL );
While (rs. next () {%>
ID: <% = rs. getString ("id") %> </br>
Name: <% = rs. getString ("name") %> </br>
Gender: <% = rs. getString ("sex") %> </br>
Age: <% = rs. getString ("age") %> </br>
<% }%>
<% Out. print ("Congratulations! Database Operation successful! "); %>
<%
Rs. close ();
Stmt. close ();
Conn. close ();
%>
</Br>
<Input name = "" type = "button" onclick = "location. href = 'index _ test. jsp '" value = "return"/>
</Body>
</Html>
The insert_test.jsp 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 to the database </title>
</Head>
<Body>
<%
String id = request. getParameter ("id"); // obtain from the form
String name = request. getParameter ("name"); // obtain from the form
String sex = request. getParameter ("sex"); // obtain from the form
String age = request. getParameter ("age"); // obtain from the form
Try
{
/** Database connection 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.exe cuteQuery ("set names UTF8 ");
String insert_ SQL = "insert into person_tb values ('" + id + "', '" + name + "', '" + sex + "', '"+ age + "')";
String query_ SQL = "select * from person_tb ";
Try {
Stmt.exe cute (insert_ SQL );
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
ResultSet rs = stmt.exe cuteQuery (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>
<%
}
} 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"/>
</Body>
</Html>
The code for the delete_test.jsp 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" %>
<%
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
%>
<Htnl>
<Head>
<Title> delete a person </title>
</Head>
<Body>
<%
String id = request. getParameter ("id"); // obtain from the form
String name = request. getParameter ("name"); // obtain from the form
Try
{
/** Database connection 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 {
If (id! = Null & id! = ""){
Out. println ("deleted id =" + id +. </Br> ");
Delete_ SQL = "delete from person_tb where id =" + id;
}
Else if (name! = Null & name! = ""){
Out. println ("deleted name =" + name +. </Br> ");
Delete_ SQL = "delete from person_tb where name = '" + name + "'";
} Else {
Out. println ("Please enter at least one of the IDS and names! </Br> ");
}
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
Stmt.exe cute (delete_ SQL );
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
ResultSet rs = stmt.exe cuteQuery (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"/>
</Body>
</Html>
Access the index_test.jsp page in the browser, click the delete button, enter any one of the IDS or names, and submit them to delete them:
Before the deletion (bottom left) and after (bottom right), the database content is displayed as follows:
In general, the query, insert, and delete functions are all implemented.
The problem found now is that the position of the pop-up layer can be moved, but it cannot be moved now. The specific reason is that the first code of the mouse dragging part is as follows: var objDiv = document. getElementById ("popDiv"); that is, it is not specified when the object is dragged. Looking for a solution. Who has a better method? Please help me. Thank you!