当前位置:网站首页>Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
2022-07-03 14:03:00 【A ke】
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> home page </title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body onload="get()">
// Insert ( add to ) Page style of
<div>
full name <input type="text" name="name" id="name"/><br/>
height <input type="text" name="height" id="height"/><br/>
Age <input type="text" name="age" id="age"/><br/>
<input value=" Insert " type="button" onclick="insert()"/>
</div>
// Update Style
<div style="display:none;background-color:red" id="up_div">
ID<input type="text" name="id" id="up_id"/><br/>
full name <input type="text" name="name" id="up_name"/><br/>
height <input type="text" name="height" id="up_height"/><br/>
Age <input type="text" name="age" id="up_age"/><br/>
<input value=" change " type="button" onclick="update()"/>
</div>
</body>
//js part
<script type="text/javascript">
function get() {
$.ajax({
url:" Your display data servlet link ",
data:{},
type:"get",
success:function(value){
console.log(value.data);
ViewList(value.data);
}
})
}
// Data display table
function ViewList(data) {
console.log(data+"---");
var html= '<table style="border:1px solid #000; width:300px;height:20px;">';
for(var i=0;i<data.length;i++){
html+='<tr>';
html += '<td style="border:1px solid #000; width:30px;height:20px;" id="td_type">' + data[i].id + '</td>';
html += '<td style="border:1px solid #000; width:60px;height:20px;" id="td_type">' + data[i].name + '</td>';
html += '<td style="border:1px solid #000; width:30px;height:20px;" id="td_type">' + data[i].height + '</td>';
html += '<td style="border:1px solid #000; width:30px;height:20px;" id="td_type">' + data[i].age + '</td>';
html += '<td style="border:1px solid #000; width:135px;height:20px;">' ;
html += '<input style="border:1px solid #000; width:40px;height:20px;" id="td_type" type="submit" value=" modify " onclick="updateDiv('+data[i].id+')"/>';
html += '<input style="border:1px solid #000; width:40px;height:20px;" id="td_type"type="submit" value=" Delete " onclick="del('+data[i].id+')"/>';
html+='</td>';
html+='</tr>';
}
html +='</table>';
$("#inf").empty().append(html);
}
// Insert
function insert() {
var name=$("#name").val();
var height=$("#height").val();
var age=$("#age").val();
$.ajax({
url:" Your insertion servlet link ",
data:{"name":name,"height":height,"age":age},
success:function(value){
console.log(value);
}
})
}
// This paragraph is after clicking update , The updated page is displayed
function updateDiv(id) {
document.getElementById("up_div").style.display="block";
$("#up_id").val(id);
}
// update operation
function update() {
var id=$("#up_id").val();
var name=$("#up_name").val();
var height=$("#up_height").val();
var age=$("#up_age").val();
$.ajax({
url:" Your update servlet link ",
data:{"id":id,"name":name,"height":height,"age":age},
success:function(value){
console.log(value);
}
});
}
// Delete
function del(id) {
alert(id);
$.ajax({
url:" Your deletion servlet link ",
data:{"id":id},
type:"get",
success:function(value){
console.log(value.data);
ViewList(value.data);
}
})
}
function list(data) {
}
</script>
</html>servlet.java:
see .
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//servlet Successfully generated , View the back end of the function Java The language is as follows , front 3 OK, it can solve the problem of Chinese garbled code
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String sql = "select * from student";
String sqlcount = "select count(*) from student";
String[] colums = {"id","name","height","age"};
String data = MysqlUtil.getJsonBySql(sqlcount, sql, colums);// Pay attention to the introduction of mysqlutil file
System.out.println(data);
response.getWriter().append(data);
}Delete
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//servlet Successfully generated , Delete the back end of the function Java The language is as follows , front 3 OK, it can solve the problem of Chinese garbled code
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String id=request.getParameter("id");
System.out.println(id);
String sql="delete from student where id="+id;
int i=MysqlUtil.del(sql); Pay attention to the introduction of mysqlutil file
String json="";
if(i==0) {
json="{\"code\":\"200\",\"message\":\" Delete failed \"}";
}else {
json="{\"code\":\"200\",\"message\":\" Delete successful \"}";
}
response.getWriter().append(json);
}
Insert .
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//servlet Successfully generated , Insert the back end of the function Java The language is as follows , front 3 OK, it can solve the problem of Chinese garbled code
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String name=request.getParameter("name");
String height=request.getParameter("height");
String age=request.getParameter("age");
System.out.println(name+""+height+""+age);
String insert ="insert into student(name,height,age) values('"+name+"',"+height+","+age+")";
int i=MysqlUtil.add(insert);// Pay attention to the introduction of mysqlutil file
String json="";
if(i==0) {
json="{\"code\":\"200\",\"message\":\" Insert the failure \"}";
}else {
json="{\"code\":\"200\",\"message\":\" Insert the success \"}";
}
response.getWriter().append(json);
}
to update ( modify )
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//servlet Page generation , Back end of update function Java The language is as follows , front 3 OK, it can solve the problem of Chinese garbled code
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String id=request.getParameter("id");
String name=request.getParameter("name");
String height=request.getParameter("height");
String age=request.getParameter("age");
System.out.println(id+" "+name+" "+height+" "+age);
String update="update student set name = '"+name+" ',height = "+height+",age = " +age+ " where id= "+ id;
int i=MysqlUtil.update(update);// Pay attention to the introduction of mysqlutil file
String json="";
if(i==0) {
json="{\"code\":\"200\",\"message\":\" Modification failed \"}";
}else {
json="{\"code\":\"200\",\"message\":\" Modification successful \"}";
}
response.getWriter().append(json);
}//1. Be careful mysqlutil file ,( There are additions, deletions, modifications and checks in yours Java Code file ).
//2. Want to have jdbc Linked database java Code file
边栏推荐
- Leetcode-1175. Prime Arrangements
- Golang - command line tool Cobra
- Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
- Using registered classes to realize specific type matching function template
- QT learning 24 layout manager (III)
- 解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- JS general form submission 1-onsubmit
- Field problems in MySQL
- Qt学习21 Qt 中的标准对话框(下)
- Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
猜你喜欢

Qt学习17 对话框及其类型
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点

Vite project commissioning

RocksDB LRUCache

Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
[email protected])"/>金属有机骨架(MOFs)抗肿瘤药载体|PCN-223装载甲硝唑|UiO-66包载盐酸环丙沙星([email protected])
![[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes

Qt学习18 登录对话框实例分析

Multi person collaborative data annotation based on Baidu brain easydata from scratch

GoLand 2021.2 configure go (go1.17.6)
随机推荐
C language standard IO function sorting
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Common network state detection and analysis tools
GoLand 2021.2 configure go (go1.17.6)
PHP maze game
Function calling convention
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
QT learning 21 standard dialog box in QT (Part 2)
Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
QT learning 19 standard dialog box in QT (top)
全局事件总线
GoLand 2021.1: rename the go project
Qt学习17 对话框及其类型
Folic acid modified metal organic framework (zif-8) baicalin loaded metal organic framework composite magnetic material (AU- [email
Go language unit test 3: go language uses gocovey library to do unit test
项目协作的进度如何推进| 社区征文
叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
[556. Next larger element III]
Depth and breadth first traversal of tree (regardless of binary tree)
挡不住了,国产芯片再度突进,部分环节已进到4nm