This Jsp Mysql Ajax Project will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE. using Mysql Database using Ajax. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.The project is very helpful for learn Ajax technology.
All the libraries i put in to one folder which name is components. i attached the link below you will be able to download.
https://drive.google.com/drive/folders/1Wn6kACTV24qFw6ztKUcfQ9CsQUF9PFF1
In order to do the project download the related jar files for connect with Ajax json simple.jar. In order to download the jar go to the respective website to do the task click link and download them Click here.
Index.php
we are going to implemented all crud operations to get the help of Ajax without reload the pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"> </head> <body> <nav class="navbar navbar-dark bg-primary"> <h3 style="color: white">Student Management System Crud Using Jsp Ajax</h3> </nav> </br> <div class="row"> <div class="col-sm-4"> <div class="container"> <form id="frmStudent" name="frmStudent"> <div class="form-group" align="left"> <label>Student Name</label> <input type="text" name="stname" id="stname" class="form-control" placeholder="StudentName" size="30px" required> </div> <div class="form-group"> <label>Course</label> <input type="text" name="course" id="course" class="form-control" placeholder="Course" size="30px" required> </div> <div class="form-group"> <label>Fee</label> <input type="text" name="fee" id="fee" class="form-control" placeholder="Fee" size="30px" required> </div> <div class="form-group" align="right"> <button type="button" class="btn btn-info" id="save" onclick="addStudent()">Add</button> <button type="button" class="btn btn-warning" id="reset" onclick="reSet()">Reset</button> </div> </form> </div> </div> <div class="col-sm-8"> <div class="panel-body"> <table id="tbl-student" class="table table-bordered" cellpadding="0" cellspacing="0" width="100%"> <thead> <tr> <th></th> <th></th> <th></th> <th></th> <th></th> </tr> </thead> </table> </div> </div> </div> <script src="component/jquery/jquery.js" type="text/javascript"></script> <script src="component/jquery/jquery.min.js" type="text/javascript"></script> <script src="component/jquery.validate.min.js" type="text/javascript"></script> <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js" type="text/javascript"></script> <script> getall(); var isNew = true; var studentid = null; function addStudent() { if($("#frmStudent").valid()) { var url=""; var data= ""; var method; if(isNew==true) { url = 'add.jsp'; data = $("#frmStudent").serialize(); method = 'POST' } else { url = 'update.jsp'; data = $("#frmStudent").serialize() + "&studentid=" + studentid; method = 'POST' } $.ajax({ type: method, url : url, dataType: 'JSON', data : data, success:function(data) { getall(); $('#stname').val(""); $('#course').val(""); $('#fee').val(""); if(isNew ==true) { alert("Record Adddeddd"); } else { alert("Record Updatee"); } } }); } } function getall() { $('#tbl-student').dataTable().fnDestroy(); $.ajax({ url: "all_student.jsp", type : "GET", dataType : "JSON", success:function(data) { $('#tbl-student').dataTable({ "aaData":data, "scrollX": true, "aoColumns": [ {"sTitle": "Student Name","mData": "name"}, {"sTitle": "Course","mData": "course"}, {"sTitle": "Fee","mData": "fee"}, { "sTitle": "Edit", "mData": "id", "render" : function(mData,type,row,meta) { return '<button class="btn btn-success" onclick="get_details('+ mData +')">Edit</button>'; } }, { "sTitle": "Delete", "mData": "id", "render" : function(mData,type,row,meta) { return '<button class="btn btn-danger" onclick="get_delete('+ mData +')">Delete</button>'; } }, ] }); } }); } function get_details(id) { $.ajax({ type: "POST", url : "edit_return.jsp", data : {"id": id}, success: function(data) { isNew = false var obj = JSON.parse(data); studentid = obj[0].id $('#stname').val(obj[0].stname); $('#course').val(obj[0].scourse); $('#fee').val(obj[0].sfee); } }); } function get_delete(id) { $.ajax({ type: 'POST', url: 'delete.jsp', dataType: 'JSON', data:{"id": id}, success:function(data) { alert("Record Deletedddd"); getall(); } }); } </script> </body> </html> |
Add records :- when you get add request from the Ajax call
add.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <%@page import="java.sql.DriverManager"%> <%@page import="org.json.simple.JSONObject"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="org.json.simple.JSONArray"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% JSONArray list = new JSONArray(); String studname = request.getParameter("stname"); String course = request.getParameter("course"); String fee = request.getParameter("fee"); Connection con; PreparedStatement pst; ResultSet rs; JSONObject obj = new JSONObject(); Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jspstudent","root",""); pst = con.prepareStatement("insert into records (stname,course,fee)values(?,?,?)"); pst.setString(1, studname); pst.setString(2, course); pst.setString(3, fee); pst.executeUpdate(); obj.put("name", "success"); list.add(obj); out.println(list.toJSONString()); out.flush(); %> |
all_students.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <%@page import="java.sql.Statement"%> <%@page import="java.sql.DriverManager"%> <%@page import="org.json.simple.JSONObject"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="org.json.simple.JSONArray"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% JSONArray list = new JSONArray(); Connection con; PreparedStatement pst; ResultSet rs; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jspstudent","root",""); String query = "select * from records"; Statement stmt = con.createStatement(); rs = stmt.executeQuery(query); while(rs.next()) { JSONObject obj = new JSONObject(); String id = rs.getString("id"); String name = rs.getString("stname"); String course = rs.getString("course"); String fee = rs.getString("fee"); obj.put("name", name); obj.put("course", course); obj.put("fee", fee); obj.put("id", id); list.add(obj); } out.print(list.toJSONString()); out.flush(); %> |
edit_return.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <%@page import="org.json.simple.JSONObject"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="org.json.simple.JSONArray"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% JSONArray list = new JSONArray(); Connection con; PreparedStatement pst; ResultSet rs; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jspstudent","root",""); String id = request.getParameter("id"); pst = con.prepareStatement("select id,stname,course,fee from records where id = ?"); pst.setString(1, id); rs = pst.executeQuery(); if(rs.next()==true) { String id1 = rs.getString(1); String stname = rs.getString(2); String scourse = rs.getString(3); String sfee = rs.getString(4); JSONObject obj = new JSONObject(); obj.put("id",id1); obj.put("stname",stname); obj.put("scourse",scourse); obj.put("sfee",sfee); list.add(obj); } out.print(list.toJSONString()); out.flush(); %> |
update.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <%@page import="java.sql.DriverManager"%> <%@page import="org.json.simple.JSONObject"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="org.json.simple.JSONArray"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% JSONArray list = new JSONArray(); String stid = request.getParameter("studentid"); String studname = request.getParameter("stname"); String course = request.getParameter("course"); String fee = request.getParameter("fee"); Connection con; PreparedStatement pst; ResultSet rs; JSONObject obj = new JSONObject(); Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jspstudent","root",""); pst = con.prepareStatement("update records set stname = ?, course= ? , fee= ? where id = ?"); pst.setString(1, studname); pst.setString(2, course); pst.setString(3, fee); pst.setString(4, stid); pst.executeUpdate(); obj.put("name", "success"); list.add(obj); out.println(list.toJSONString()); out.flush(); %> |
Delete.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <%@page import="java.sql.DriverManager"%> <%@page import="org.json.simple.JSONObject"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="org.json.simple.JSONArray"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% JSONArray list = new JSONArray(); String stid = request.getParameter("id"); Connection con; PreparedStatement pst; ResultSet rs; JSONObject obj = new JSONObject(); Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jspstudent","root",""); pst = con.prepareStatement("delete from records where id = ?"); pst.setString(1, stid); pst.executeUpdate(); obj.put("name", "success"); list.add(obj); out.println(list.toJSONString()); out.flush(); %> |
i have attached the video link below. which will do this tutorials step by step.