JSP+Servlet+jQuery实现简单的MySQL数据库的增、删、改、查这个⼩项⽬通过使⽤ jsp、servlet、jQuery 在⽹页中对 MySQL 数据库中的数据进⾏增删改查。
软件:Eclipse
数据库:MySQL
请注意:关于数据库的⼀些的地⽅需要进⾏适当修改
jQuery中使⽤ Ajax 的语法格式:
$.ajax({
type:" ",//提交的格式:GET 或 POST
url:" ",//要提交的“地⽅”
data:{key:value, key:value, `````},//待提交的数据
success:function(data){
//处理数据的操作
},//处理成功返回的数据
});
项⽬的⽂件
代码
Book.java
杭电oj没做出来的题怎么办
package com.lyq.bean;
public class Book {
private int id;
private String name;
private int bookCount;
public int getId() {return id;}
public String getName() {return name;}
public int getBookCount() {return bookCount;}
public void setId(int id) {this.id = id;}
public void setName(String name) {this.name = name;}
public void setBookCount(int bookCount) {this.bookCount = bookCount;} }
AddServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class AddServlet
*/
@WebServlet("/AddServlet")
html在线查看public class AddServlet extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  // TODO Auto-generated method stub
//Writer().append("Served at: ").ContextPath());
response.setCharacterEncoding("utf-8");
int id = Integer.Parameter("id"));
String name = Parameter("bookName");
int bookCount = Integer.Parameter("bookCount"));
try{
Class.forName("sql.jdbc.Driver");
Connection con = Connection("jdbc:mysql://localhost/tuShu","root","111111");
String sql = "insert into xinXi(id, name, bookCount) values(?, ?, ?)";
PreparedStatement pr = con.prepareStatement(sql);
pr.setInt(1, id);
pr.setString(2, name);
pr.setInt(3, bookCount);
int row = pr.executeUpdate();
String str =  "成功添加" + row + "条数据!";
if(row > 0){
}
字符串压缩else {
}
pr.close();
con.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
DeleteServlet.java
import java.sql.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DeleteServlet
*/
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  // TODO Auto-generated method stub
//Writer().append("Served at: ").ContextPath());
response.setCharacterEncoding("UTF-8");
int id = Integer.Parameter("id"));
try{
Class.forName("sql.jdbc.Driver");
Connection con = Connection("jdbc:mysql://localhost/tuShu","root","111111");
String sql = "delete from xinXi where id=?";
PreparedStatement pr = con.prepareStatement(sql);
pr.setInt(1,id);
mysql面试题 增删改查int row = pr.executeUpdate();
String str =  "成功删除" + row + "条数据!";
video html
if(row > 0){
}
else {
}
pr.close();
con.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
FindOneServlet.java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FindOneServlet
*/
@WebServlet("/FindOneServlet")
public class FindOneServlet extends HttpServlet {
/
**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  // TODO Auto-generated method stub
//Writer().append("Served at: ").ContextPath());
response.setCharacterEncoding("UTF-8");
int id = Integer.Parameter("id"));
try {
Class.forName("sql.jdbc.Driver");
Connection con = Connection("jdbc:mysql://localhost/tuShu","root","111111");
Statement sta = ateStatement();
String sql = "select name, bookCount from xinXi where id=" + id;
ResultSet rs = uteQuery(sql);
List<String> list = new ArrayList<String>();
list.add(String.valueOf(id));
()) {
list.String(1));
list.add(String.Int(2)));
}
String str = "<table align=\"center\" border=\"1\"><tr><td>";
str = str + (0) + "</td><td>" + (1)  + "</td><td>" + (2) + "</td></table>";
rs.close();
sta.close();
con.close();
getpid函数用法}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
UpdateServlet.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。