About executing SQL statements on a page

Source: Internet
Author: User

Executes the SQL statement on the page, starting with a text field at the top of the page, allowing the user to enter the SQL statement that needs to be executed.

The HTML code is as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title> New Document </title>
<body>
<div id = "Defaultbuttonbar" align= "right" style= "margin-right:20px" >
<button id= "Add" value= "execute" onclick= "Find ()" style= "width:100px;height:30px;" > Execution </button>
</div>
<div>
Input Sql:<br><textarea id= "SQL" rows= "6" style= "width:98%" ></textarea>
</div>

<div id= "Divsql" style= "color:red;height:400" > <text id= "str" ></text></div>
</body>

Then the most important thing is how to get the selected text and have it execute SQL in the background and return information to the foreground.

The JS code is as follows:

/**
* Execute the mouse selected text (i.e. SQL statement)
*/
function Find () {
var selectvalue=null;
var sqlval=null;
if (document.selection) {
Selectvalue=document.selection.createrange (). text;//Verify IE browser
} else if (window.getselection) {
Selectvalue=window.getselection ();//Other browsers
var obj = document.getElementById ("SQL");
var selstart = Obj.selectionstart; Start position of text buoy selection
var selend = obj.selectionend; The end position of the text buoy selection
Selectvalue= obj.value.substring (selstart,selend);
}
if (selectvalue==null| | selectvalue== "") {
var Sqlval=document.getelementbyid ("SQL"). Value.tolocalelowercase ();
}else{
Sqlval=selectvalue;
}
if (Sqlval.indexof ("select")!=-1&&sqlval.indexof ("create") ==-1) {
Sqlval = encodeURI (sqlval);
$.ajax ({
Type: ' POST ',
URL: ' Findcolumn ',
Data: "Sqlval=" +sqlval,
DataType: ' Text ',
ContentType: ' application/x-www-form-urlencoded; Charset=utf-8 ',
Success:function (data) {
var datajson=eval (' (' +data+ ') ');
$ ("#str"). Text ("");
$ (' #mytable '). Omgrid ({
height:400,
limit:0,
Colmodel:d Atajson,
ContentType: ' application/x-www-form-urlencoded; Charset=utf-8 ',
DataSource: ' finddata.action?sqlval= ' +sqlval
});
}
, Error:function (xmlresponse) {
document.getElementById ("tab"). innerhtml= "";
document.getElementById ("tab"). innerhtml= ' <table id= "mytable" ></table> ";
var Str=xmlresponse.responsetext;
var str1=str.substring (Str.indexof ("Java"), Str.lastindexof ("</pre>"));
$ ("#str"). Text ("");//Clear data
$ ("#str"). Append (STR1);
}
});
}else{
$.ajax ({
Type: ' POST ',
URL: ' Updateanddelete ',
Data: "Sqlval=" +sqlval,
DataType: ' Text ',
limit:0,
ContentType: ' application/x-www-form-urlencoded; Charset=utf-8 ',
Success:function (data) {
var datajson=eval (' (' +data+ ') ');
$ ("#str"). Text ("");
if (datajson.update== ' 0002 ') {
Alert ("Update succeeded");
}else if (datajson.del== ' 0001 ') {
Alert ("Delete succeeded");
}else if (datajson.insert== ' 0003 ') {
Alert ("Insert data Success");
}else if (datajson.drop== ' 0004 ') {
Alert ("Delete table succeeded");
else if (datajson.create== ' 0005 ') {
Alert ("CREATE table succeeded");
}else{
Alert ("System error");
}
},error:function (xmlresponse) {
var Str=xmlresponse.responsetext;
var str1=str.substring (Str.indexof ("Java"), Str.lastindexof ("</pre>"));
$ ("#str"). Text ("");//Clear data
$ ("#str"). Append (STR1);
}
});
}
}

About the background code is not shown here, nothing more than to execute the foreground of the SQL statement and return some corresponding information

About executing SQL statements on a page

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.