日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

web项目实现mysql增删改查并从前端页面操作

發(fā)布時間:2024/4/14 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 web项目实现mysql增删改查并从前端页面操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.看下各個包下面的文件,我上一篇文章已經(jīng)說過了,這里對上一章有一部分重復(fù)的

2.User.java是數(shù)據(jù)庫元素寫的一個類,代碼如下

package com.hqyj.wj.model; //用戶信息表 public class User {private int id;private String name;private String birthday; // public User(int id,String name,String birthday){ // this.id=id; // this.name=name; // this.birthday=birthday; // }public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;} }

 3.UserDaoInf是一個接口實(shí)現(xiàn)數(shù)據(jù)庫的增刪查改方法,代碼如下

package com.hqyj.wj.dao.inf; import java.util.List; import com.hqyj.wj.model.User; /*** 數(shù)據(jù)訪問層的接口定義數(shù)據(jù)接口的方法* */ public interface UserDaoInf {//定義一個查詢方法List<User> search();
//定義數(shù)據(jù)庫的插入int insert(User user);
//定義跟新數(shù)據(jù)庫int update(User user);
//通過名字刪除數(shù)據(jù)元素int delete(String name);}

4.UserDao實(shí)現(xiàn)UserDaoInf接口,并 連接數(shù)據(jù)庫

package com.hqyj.wj.dao;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List;import com.hqyj.wj.dao.inf.UserDaoInf; import com.hqyj.wj.model.User;/*** * @author wl 數(shù)據(jù)訪問接口*/ public class UserDao implements UserDaoInf {// 數(shù)據(jù)訪問數(shù)據(jù)庫的連接對象protected Connection con = null;// 預(yù)編譯你寫的sql語句protected PreparedStatement ps = null;// 查詢預(yù)編譯的sql語句protected ResultSet rs = null;// 獲取數(shù)據(jù)庫鏈接@SuppressWarnings("finally")public Connection getCon() {try {// 加載mysql驅(qū)動Class.forName("com.mysql.jdbc.Driver");// 獲取數(shù)據(jù)庫鏈接con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/image?characterEncoding=utf8","root", "root");System.out.println("鏈接成功");return con;} catch (Exception e) {System.out.println("鏈接失敗" + e.getMessage());e.printStackTrace();return null;}}/*** 查詢方法*/public List<User> search() {
//定義一個列表接收數(shù)據(jù)庫數(shù)據(jù)List<User> list = new ArrayList<User>();try {// 定義一個sql語句// String// sql="SELECT a.id as 序號,a.salary as '薪水',b.`name` as '姓名' from salary a LEFT JOIN `user` b on a.u_id=b.id";String sql = "SELECT * from user";// 獲取數(shù)據(jù)庫連接con = getCon();// 預(yù)編譯sql語句ps = con.prepareStatement(sql);// 把編譯出來的結(jié)果集裝載到ResultSet對象里面rs=ps.executeQuery();// 取出ResultSet里的結(jié)果集裝載到數(shù)據(jù)模型里while (rs.next()) {User user = new User();user.setName(rs.getString("name"));user.setBirthday(rs.getString("birthday"));user.setId(Integer.parseInt(rs.getString("id")));list.add(user);}} catch (Exception e) {System.out.println("查詢錯誤" + e.getMessage());} finally {try {rs.close();ps.close();con.close();} catch (Exception e2) {e2.printStackTrace();}}return list;}public int insert(User user) {int i=0;try {// 定義一個sql語句// String// sql="SELECT a.id as 序號,a.salary as '薪水',b.`name` as '姓名' from salary a LEFT JOIN `user` b on a.u_id=b.id";String sql = "insert into user(name,birthday) values(?,?)";// 獲取數(shù)據(jù)庫連接con = getCon();// 預(yù)編譯sql語句ps = con.prepareStatement(sql);ps.setString(1, user.getName());ps.setString(2, user.getBirthday());i=ps.executeUpdate();} catch (Exception e) {System.out.println("查詢錯誤" + e.getMessage());} finally {try {ps.close();con.close();} catch (Exception e2) {e2.printStackTrace();}}return i;}//跟新信息public int update(User user) {int i=0;try {// 定義一個sql語句// String// sql="SELECT a.id as 序號,a.salary as '薪水',b.`name` as '姓名' from salary a LEFT JOIN `user` b on a.u_id=b.id";String sql = "update user set name='" + user.getName() +"' where id='" + user.getId() + "'";// 獲取數(shù)據(jù)庫連接con = getCon();// 預(yù)編譯sql語句ps = con.prepareStatement(sql);i=ps.executeUpdate();} catch (Exception e) {System.out.println("查詢錯誤" + e.getMessage());} finally {try {ps.close();con.close();} catch (Exception e2) {e2.printStackTrace();}}return i;}public int delete(String name) {int i=0;try {// 定義一個sql語句// String// sql="SELECT a.id as 序號,a.salary as '薪水',b.`name` as '姓名' from salary a LEFT JOIN `user` b on a.u_id=b.id";String sql = "delete from user where name='"+name+"'";// 獲取數(shù)據(jù)庫連接con = getCon();// 預(yù)編譯sql語句ps = con.prepareStatement(sql);i=ps.executeUpdate();} catch (Exception e) {System.out.println("查詢錯誤" + e.getMessage());} finally {try {ps.close();con.close();} catch (Exception e2) {e2.printStackTrace();}}return i;}}

5.UserServiceInf也是對數(shù)據(jù)庫的增刪改查方法和獲取前端數(shù)據(jù)對數(shù)據(jù)庫的增刪改查操作方法

package com.hqyj.wj.service.inf;import java.io.PrintWriter; import java.util.List;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.hqyj.wj.model.User;public interface UserServiceInf {List<User> search();int insert(User user);int update(User user);int delete(String name);
//里面用ajax獲取前端來的數(shù)據(jù),并操作數(shù)據(jù)庫的方法public void ss(HttpServletRequest request,HttpServletResponse response,PrintWriter out); }

6.UserService實(shí)現(xiàn)UserService接口

 

package com.hqyj.wj.service;import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.List;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import com.hqyj.wj.dao.UserDao; import com.hqyj.wj.dao.inf.UserDaoInf; import com.hqyj.wj.model.User; import com.hqyj.wj.service.inf.UserServiceInf;/*** 邏輯服務(wù)層實(shí)現(xiàn)類*/ public class UserService implements UserServiceInf {UserDaoInf us = new UserDao();public List<User> search() {//返回的是 UserDao里面的search()方法return us.search();}public int insert(User user) {
//調(diào)用了UserDao()里的insert()方法int i = us.insert(user);return i;}public int update(User user) {// TODO Auto-generated method stubint i = us.update(user);return i;}public int delete(String name) {int i = us.delete(name);return i;}public void ss(HttpServletRequest request, HttpServletResponse response,PrintWriter out) {// serverInder是從前端獲取的數(shù),目的是為了區(qū)分是增刪改查的那個操作
//request.getParameter()是獲取拼在url地址欄下的值int serverInder = Integer.parseInt(request.getParameter("serverIndex"));
//實(shí)現(xiàn)查詢-----------------------if (serverInder == 1) {List<User> list = search();// 把list數(shù)據(jù)解析成前端頁面能讀取的數(shù)據(jù)JSONArray json = JSONArray.fromObject(list);out.print(json.toString());}// 現(xiàn)實(shí)插入---------------------------if (serverInder == 2) {String name = request.getParameter("name");String birthday = request.getParameter("birthday");User user = new User();try {
//解決從前端傳到服務(wù)器(數(shù)據(jù)庫)亂碼問題name = new String(name.getBytes("ISO-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {birthday = new String(birthday.getBytes("ISO-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}user.setName(name);user.setBirthday(birthday);int i = insert(user);if (i == 1) {out.print("插入數(shù)據(jù)成功");System.out.println("插入數(shù)據(jù)成功");} else {out.print("插入數(shù)據(jù)失敗");System.out.println("插入數(shù)據(jù)失敗");}}// 實(shí)現(xiàn)刪除if (serverInder == 3) {String name = request.getParameter("name");try {name = new String(name.getBytes("ISO-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}int i = delete(name);if (i == 1) {out.print("刪除數(shù)據(jù)成功");System.out.println("刪除數(shù)據(jù)成功");} else {out.print("刪除數(shù)據(jù)失敗" + i);System.out.println("刪除數(shù)據(jù)失敗" + i);}}// 實(shí)現(xiàn)更新if (serverInder == 4) {User user = new User();int id = Integer.parseInt(request.getParameter("id"));String name = request.getParameter("name");String birthday = request.getParameter("birthday");try {birthday = new String(birthday.getBytes("ISO-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {name = new String(name.getBytes("ISO-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}user.setId(id);user.setName(name);user.setBirthday(birthday);int i = update(user);if (i == 1) {out.print("更改數(shù)據(jù)成功");System.out.println("更改數(shù)據(jù)成功");} else {out.print("更改數(shù)據(jù)失敗" + i);System.out.println("更改數(shù)據(jù)失敗" + i);}}}}

7.controller控制器是調(diào)用UserService里面的方法,實(shí)現(xiàn)對前端頁面操作數(shù)據(jù)庫

package com.hqyj.wj.controller;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import com.hqyj.wj.service.*; import com.hqyj.wj.service.inf.*; import com.hqyj.wj.model.*;import java.util.List; import java.util.ArrayList;public class OneServlet extends HttpServlet {public OneServlet() {super();}public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}//doget對應(yīng)的是ajax的$.get()方法//request是裝載請求數(shù)據(jù)//response響應(yīng)數(shù)據(jù)到前端對象public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//解決中文亂碼的問題request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");//在服務(wù)器端設(shè)置允許在其他域名下訪問,及響應(yīng)類型、響應(yīng)頭設(shè)置//這三句解決的是跨域問題response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods","POST");response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");//響應(yīng)的文本格式response.setContentType("text/html");//獲取響應(yīng)的輸出對象PrintWriter out = response.getWriter();UserServiceInf service=new UserService();String num = request.getParameter("serverIndex");service.ss(request,response,out);//這里全部拿到UserService的ss()方法里了//實(shí)現(xiàn)查詢----------------------/*int serverInder=Integer.parseInt(request.getParameter("serverIndex"));if(serverInder==1){List<User> list=service.search();//把list數(shù)據(jù)解析成前端頁面能讀取的數(shù)據(jù)JSONArray json=JSONArray.fromObject(list);out.print(json.toString());}//現(xiàn)實(shí)插入---------------------------if(serverInder==2){String name=request.getParameter("name");String birthday=request.getParameter("birthday");User user=new User();name=new String(name.getBytes("ISO-8859-1"),"UTF-8");birthday=new String(birthday.getBytes("ISO-8859-1"),"UTF-8");user.setName(name);user.setBirthday(birthday);int i=service.insert(user);if(i==1){out.print("插入數(shù)據(jù)成功");System.out.println("插入數(shù)據(jù)成功");}else{out.print("插入數(shù)據(jù)失敗");System.out.println("插入數(shù)據(jù)失敗");}}*/out.flush();out.close();}//doget對應(yīng)的是ajax的$.post()方法public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.flush();out.close();}public void init() throws ServletException {// Put your code here}}

8.前端實(shí)現(xiàn)html代碼 

?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>ajax獲取集合</title> <script src="../js/jquery-3.1.1.min.js"></script> </head> <body><table><thead><tr><th>id</th><th>姓名</th><th>生日</th></tr></thead><tbody></tbody></table>名字:<input type="text" placeholder="請輸入姓名" id="name">生日:年<select><option>1992</option><option>1993</option><option>1994</option><option>1995</option></select><select><option>1</option><option>2</option><option>3</option><option>4</option></select><select><option>2</option><option>12</option><option>22</option><option>23</option></select><button>點(diǎn)擊獲取數(shù)據(jù)</button><button>點(diǎn)擊插入數(shù)據(jù)</button><br>請輸入刪除的名字:<input type="text" id="removename"><button>點(diǎn)擊刪除數(shù)據(jù)</button><br>請輸入需要修改的id號:<input type="text" id="updateid"><br>請輸入需要修改后的名字:名字:<input type="text" placeholder="請輸入姓名" id="updatename"><br>請輸入需要修改的后生日:生日:年<select><option>1992</option><option>1993</option><option>1994</option><option>1995</option></select><select><option>1</option><option>2</option><option>3</option><option>4</option></select><select><option>2</option><option>12</option><option>22</option><option>23</option></select><br><button>點(diǎn)擊跟新數(shù)據(jù)</button><script>$(function(){var str;
//serverIndex區(qū)分增刪改查的變量
var serverIndex;//點(diǎn)擊查看數(shù)據(jù) $("button").eq(0).click(function(){var str;serverIndex=1;
//{serverIndex:serverIndex}是拼在地址欄上的,從后端獲取他的值$.get(
"http://localhost:8080/jquery/servlet/OneServlet",{serverIndex:serverIndex},function(data){console.log(data);var num=eval(data);for(var i=0;i<num.length;i++){str+=" <tr> <td>"+num[i].id+"</td> <td>"+num[i].name+"</td> <td>"+num[i].birthday+"</td></tr>";}$("tbody").html(str); })})//點(diǎn)擊插入數(shù)據(jù) $("button").eq(1).click(function(){//獲取輸入名字的值 serverIndex=2;var name=$("#name").val();var year=$("select:eq(0) option:selected").val();var mouth=$("select:eq(1) option:selected").val();var day=$("select:eq(2) option:selected").val();var birthday=year+"/"+mouth+"/"+day;console.log(birthday);$.get("http://localhost:8080/jquery/servlet/OneServlet",{serverIndex:serverIndex,name:name,birthday:birthday},function(data){console.log(data);})})//點(diǎn)擊刪除數(shù)據(jù) $("button").eq(2).click(function(){//獲取輸入名字的值 serverIndex=3;var name=$("#removename").val();$.get("http://localhost:8080/jquery/servlet/OneServlet",{serverIndex:serverIndex,name:name},function(data){console.log(data);})})//點(diǎn)擊跟新數(shù)據(jù) $("button").eq(3).click(function(){//獲取輸入名字的值 serverIndex=4;var id=$("#updateid").val();var name=$("#updatename").val();var year=$("select:eq(3) option:selected").val();var mouth=$("select:eq(4) option:selected").val();var day=$("select:eq(5) option:selected").val();var birthday=year+"/"+mouth+"/"+day;$.get("http://localhost:8080/jquery/servlet/OneServlet",{serverIndex:serverIndex,name:name,id:id,birthday:birthday},function(data){console.log(data);})})})</script> </body> </html>

?

 

轉(zhuǎn)載于:https://www.cnblogs.com/wlhappy92/p/web_sql.html

總結(jié)

以上是生活随笔為你收集整理的web项目实现mysql增删改查并从前端页面操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 午夜精品av | 老司机福利精品 | 夜夜草导航 | 男男gay羞辱feet贱奴vk | 欧美专区视频 | 极品美女开粉嫩精品 | 国产视频一二 | 久久免费精品 | 亚洲av成人片色在线观看高潮 | 国产精品久久久久久三级 | 91九色丨porny丨国产jk | 午夜黄视频 | 黑森林福利视频导航 | 在线观看污视频网站 | 久久免费视频一区二区 | 日中文字幕 | 国产人人插| 污污网站在线 | 美女免费视频网站 | www视频在线观看免费 | 亚洲 国产 日韩 欧美 | 欧洲精品视频在线观看 | 九九九九久久久久 | 中文字幕一区二区在线观看 | 久久h| 天天操bb | 在线91视频| 午夜簧片 | 欧美日韩一区二区三区在线观看 | 一级黄色性生活视频 | 九月激情网 | 超碰av在线免费观看 | 成人观看视频 | 99精品久久久久久 | 操www| 成人激情五月天 | 精品国产乱码久久久久久1区二区 | 在线免费观看黄色小视频 | 国产日本欧美一区二区 | 免费草逼视频 | 九九精品在线观看视频 | 亚洲欧美经典 | 精品无码一区二区三区蜜臀 | 国模人体私拍xvideos | 一级特黄aaa | 亚洲综合福利 | 日韩三级免费看 | 欧美激情精品久久久久 | 亚洲精品男人天堂 | 国产精品无码在线 | 日韩视频一二三 | 黄色高潮 | 欧美啪啪一区 | 国产伦精品一区二区三区视频网站 | 国产精选第一页 | 黄色三级小视频 | 91久久久久一区二区 | 色婷婷狠狠操 | 亚洲无限av | 久久99精品久久久久婷婷 | 伊人亚洲| 免费黄视频在线观看 | 影音先锋亚洲成aⅴ人在 | 日本大尺度电影免费观看全集中文版 | 成人日皮视频 | japansexxxxhd医生 夜夜操导航 | aa一级黄色片 | 国产 欧美 精品 | 影音先锋啪啪资源 | 欧美激情综合色综合啪啪五月 | 色窝窝无码一区二区三区 | 污片网站| 国产内射一区二区 | 牛牛精品一区 | 久青草免费视频 | 免费看裸体视频网站 | 17c在线视频 | 国产三级一区 | 欧美激情视频一区二区三区在线播放 | 男生和女生靠逼视频 | 欧美裸体xxxx| 国产一区二区三区毛片 | 蜜桃av鲁一鲁一鲁一鲁俄罗斯的 | 久久高清| 五月天小说网 | а√天堂资源在线 | 久久人人爽人人爽人人av | 高清一区二区三区四区 | 久热网 | 男女网站视频 | jizz教师| 日韩系列在线 | 在线视频成人 | 亚洲一区二区三区视频在线 | 国产欧美精品区一区二区三区 | 波多野吉衣一二三区乱码 | 久久久久国色av免费观看性色 | a视频在线观看 | 成人性生生活性生交全黄 |