Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add and delete mysql database data by JavaWeb

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "JavaWeb how to achieve the addition and deletion of mysql database data", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "JavaWeb how to achieve the addition and deletion of mysql database data" this article.

Prepare for operation 1. Configure the mysql environment

Connect to mysql database

two。 Create Servlet Project

3. Create EMS Package

4. Create Servlet separately

UserListServlet: used to access the database and display the data on the page.

UserAddServlet: used to get request data and add the data to the ems database user table.

UserDeleteServlet: used to delete data corresponding to id in the user table in the ems database through the employee id

5. Create addUser.html

6. Configure the web.xml document UserListServlet EMS.UserListServlet UserDeleteServlet EMS.UserDeleteServlet UserAddServlet EMS.UserAddServlet UserListServlet / list UserDeleteServlet / delete UserAddServlet / addUser 7. Open the server and redeploy the server

8. Effect display

Display database data

Delete database data

Add database data

Add employee data interface

AddUser.html

Add employee add employee Information Job number: name: salary: age: display employee data

UserListServlet

Package EMS;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse / / Servletpublic class UserListServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException of employee information query, IOException {/ / use JDBC to connect to the mysql database, and process the output query in the user table to ResultSet result=null; Connection con=null Try {/ / (1) register the load driver Class.forName ("com.mysql.jdbc.Driver"); / / (2) get the link to the database / / (1). Connect to the url address of mysql String url= "jdbc:mysql://localhost:3306/ems"; / / (2). The user name String username= "root" to connect to mysql; / / (3). Password for connecting to mysql String pwd= "123456"; con=DriverManager.getConnection (url, username, pwd); / / (3) precompiled sql statement System.out.println ("MySQL connected successfully!" + con); / / 3. Precompile the SQL statement String sql= "select * from user"; PreparedStatement prep=con.prepareStatement (sql); / / (4) execute the sql statement result=prep.executeQuery () } catch (Exception e) {e.printStackTrace () } / / use response to obtain the character output stream PrintWriter, and output the query results to the browser side / / set format encoding response.setContentType ("text/html;charset=utf-8") / / output a table PrintWriter pw=response.getWriter (); pw.println ("); pw.println (" employee Information Table "); pw.println ("); pw.println ("Job ID name, salary Age") to the browser Pw.println ("") Try {while (result.next ()) {pw.println ("" + result.getInt ("id") + "+ result.getString (" name ") +"+ result.getDouble (" salary ") +" + result.getInt ("age") + "delete") System.out.println (result.getInt ("id") + "-" + result.getString ("name") + "-" + result.getDouble ("salary") + "-" + result.getInt ("age")) }} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / the last line of the table pw.println ("add employee information") Pw.println (""); / / close try {con.close ();} catch (SQLException e) {e.printStackTrace ();} add employee data

UserAddServlet

Package EMS;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.PreparedStatement;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import DBUtil.DBUtil / / add Servletpublic class UserAddServlet extends HttpServlet {public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String strId=request.getParameter ("id"); String strName=request.getParameter ("name"); String strSal=request.getParameter ("salary"); String strAge=request.getParameter ("age") to the employee information System.out.print (strId+strName+strSal+strAge); try {/ / connects to the database Connection con=DBUtil.getCon using jdbc ("ems") / / precompile sql sentences String sql= "insert into user values (?)"; PreparedStatement prep=con.prepareStatement (sql); prep.setInt (1, Integer.parseInt (strId)); prep.setString (2, strName) Prep.setDouble (3, Double.parseDouble (strSal)); prep.setInt (4, Integer.parseInt (strAge)); / / execute the sql statement prep.executeUpdate (); / / close the connection to the database con.close () } catch (Exception e) {e.printStackTrace ();} / / return to the list home page / / redirect response.sendRedirect ("list");}} delete employee data

UserDeleteServlet

Package EMS;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import DBUtil.DBUtil / / Servletpublic class UserDeleteServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException deleted by employee information, IOException {/ / receive the parameters in the request / / http://localhost:8080/Servlet/delete?id=2 String strId=request.getParameter ("id"); / / System.out.println ("job number:" + strId) Try {/ / 2room3, connect to the mysql database using JDBC to complete the deletion operation Connection con=DBUtil.getCon ("ems"); / / System.out.println (con) / / com.mysql.jdbc.JDBC4Connection@64dfeb / / precompile the sql statement String sql= "delete from user where id=?"; PreparedStatement prep=con.prepareStatement (sql) / / set the question mark parameter 1 in the sql statement: which indicates the hello parameter 2: the content set for the question mark prep.setInt (1) Integer.parseInt (strId)) / / execute sql statement / / executeUpdate () is suitable for deleting delete, modifying update, inserting insert executeQuery () is suitable for querying select prep.executeUpdate (); / / closing database connection con.close () } catch (Exception e) {e.printStackTrace () } / / 4, after the deletion is successful, go back to the http://localhost:8080/ems-servlet/list homepage address / / forwarding technology: leave the unfinished work to the next component to complete, the address of the browser address bar will not change, it is a request / / redirect technology: the work has been completed Just to jump to the next address, the address of the browser's address bar will change. It will request / / write a url-pattern address corresponding to Servlet twice, and will be redirected to the corresponding Servlet to execute response.sendRedirect ("list"). }} above are all the contents of the article "how to add and delete data from mysql database by JavaWeb". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report