JSP operation MySQL Implementation query/INSERT/delete function sample _jsp programming

Source: Internet
Author: User
Tags stmt visibility
Put the code directly:
First, the code for the Index_test.jsp page is as follows:
Copy Code code 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 ">
<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: #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 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";//Set the distance from the top edge of the pop-up layer
ObjDiv.style.left = "200px";//Set the distance of the pop-up layer from the left edge
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) {//Shutdown 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>
<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>
Sex: <input type= "text" name= "Sex" value= ""/></br>
Ages: <input type= "text" name= "Age" value= "/></br>
</p>
<input type= "Submit" value= "submitted"/>
<input type= "reset" value= "reset"/>
<input type= "reset" value= "Cancel" onclick= "Hidepopup (' Add ')"/>
</form>
</div>
</div>
<div id= "Popdivdelete" ><!--remove a person's div-->
<div id= "Poptitle" ><!--title div-->
<span class= "Title_left" > Delete personnel </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= "submitted"/>
<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" >
/*-------------------------Drag the left mouse button---------------------* *
/*--------You can delete this part of the code when you do not need to implement this feature------------* *
var objdiv = document.getElementById ("Popdiv");
var Isie = document.all? true:false;//to determine 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 is the left mouse button status flag, True to indicate that the left button is pressed
}
}

Objdiv.onmousedown = function (evnt) {
Objdrag = This;//objdrag for 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 = 0.5;//The transparency changes when the mouse is pressed
}
}
}
Document.onmouseup = function () {
moused = false;//Left button loosened
Objdrag = "";
if (Isie) {
Objdiv.releasecapture ();
ObjDiv.filters.alpha.opacity = 100;//The transparency changes when the mouse Zuo open
} else {
Window.releaseevents (objdiv.mousemove);//release Mouse drag Event
ObjDiv.style.opacity = 1;//The transparency changes when the mouse Zuo open
}
}

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>

The query_test.jsp page code is as follows:
Copy Code code 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 ");
%>
<body>
<%
Connection Con=null;
String url= "jdbc:mysql://localhost/html_db?user=root&password=123456";
html_db for Database name
Class.forName ("Org.gjt.mm.mysql.Driver"). Newinstance ()//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>
Sex: <%=rs.getstring ("Sex")%></br>
Ages: <%=rs.getstring ("Age")%></br></br>
<%}%>
<%out.print ("Congratulations!) the database operation is successful! ");%>
<%
Rs.close ();
Stmt.close ();
Conn.close ();
%>
</br>
<input name= "" type= "button" onclick= "location.href = ' index_test.jsp '" value= "return"/>
</body>

The insert_test.jsp page code is as follows:

Copy Code code 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 ");
%>
<title> insert data into the database </title>
<body>
<%
String id=request.getparameter ("id"); Get from Form
String name=request.getparameter ("name"); Get from Form
String sex=request.getparameter ("sex"); Get from Form
String age=request.getparameter ("Age"); Get from 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 + "', ' +" + "+") ";
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>
Sex: <%=rs.getstring ("Sex")%> </br>
Ages: <%=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>

The code for the Delete_test.jsp page is as follows:
Copy Code code 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 ");
%>
<title> Delete People </title>
<body>
<%
String id=request.getparameter ("id"); Get from Form
String name=request.getparameter ("name"); Get from Form
Try
{
/** 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{
if (ID!= null && ID!= "") {
Out.println ("deleted id= + ID +"). </br></br> ");
Delete_sql = "Delete from PERSON_TB where id=" + ID;
}
else if (name!= null && name!= "") {
Out.println ("deleted name= + name +"). </br></br> ");
Delete_sql = "Delete from person_tb where name= '" + name + "'";
}else{
Out.println ("Please enter at least one of the IDs, names!") </br></br> ");
}
}catch (Exception e) {
E.printstacktrace ();
}
try {
Stmt.execute (Delete_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> Sex: <%=rs.getstring ("Sex")%>
</br> Ages: <%=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>

In the browser direct access to the INDEX_TEST.JSP page, click the Delete button, fill in the ID or any one of the names and submit can be deleted:

The contents of the database before the delete (bottom left) and post (bottom right) are displayed as follows:


In general, the query, insert and DELETE functions are implemented.

Now found that the problem is: the original is to achieve the pop-up level can be moved, but now can't move, the specific reason is the mouse drag part of the first code: var objdiv = document.getElementById ("Popdiv"); Not specified well. are looking for ways to solve. Who has a better way, please help me, thank you!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.