jsp mysql增删改查代码_java+jsp+mysql实现对数据库增删改查(无servlet)

无servelt就是连接和操作都写在jsp里面

代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

学生管理系统

学生管理系统

添加学生信息

学号 姓名 性别 生日 地址 操作

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentmanagement?useUnicode=true&characterEncoding=utf-8", "root","root");//使用Statement对象

Statement stmt =con.createStatement();

ResultSet rs= stmt.executeQuery("select * from student");while(rs.next()) {int sno = rs.getInt(1);

out.println("

" + rs.getString(1) + "" + rs.getString(2) + ""

+ rs.getString(3) + "

" + rs.getString(4) + "" + rs.getString(5)+ " 修改  删除");

}

rs.close();

stmt.close();

con.close();

}catch(Exception e) {

out.println("Exception:" +e.getMessage());

}%>


style="text-align: center; width: 100%; font-size: 12px; color: #333;">

©版权所有:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一些指导。 首先,你需要创建一个数据库来存储水果信息,包括水果名称、价格、库存等信息。然后,你需要创建一个JavaBean类来表示水果对象,包括属性和方法。接着,你需要编写servlet来处理用户的请求,包括增加、删除、修改、查询水果的请求。最后,你需要利用jsp来展示水果管理系统的界面,包括添加、删除、修改、查询水果的功能。 以下是一个简单的示例: 首先,创建一个名为"fruit"的数据库,并创建一个名为"fruit_info"的表来存储水果信息。表结构如下: ```sql CREATE TABLE `fruit_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `price` double NOT NULL, `stock` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 然后,创建一个JavaBean类"Fruit"来表示水果对象。代码如下: ```java public class Fruit { private int id; private String name; private double price; private int stock; 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } } ``` 接着,编写一个servlet"FruitServlet"来处理用户的请求。代码如下: ```java public class FruitServlet extends HttpServlet { private FruitDao dao = new FruitDao(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if ("add".equals(action)) { addFruit(request, response); } else if ("delete".equals(action)) { deleteFruit(request, response); } else if ("update".equals(action)) { updateFruit(request, response); } else if ("search".equals(action)) { searchFruit(request, response); } else { response.sendRedirect("index.jsp"); } } private void addFruit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); double price = Double.parseDouble(request.getParameter("price")); int stock = Integer.parseInt(request.getParameter("stock")); Fruit fruit = new Fruit(); fruit.setName(name); fruit.setPrice(price); fruit.setStock(stock); dao.addFruit(fruit); response.sendRedirect("index.jsp"); } private void deleteFruit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); dao.deleteFruit(id); response.sendRedirect("index.jsp"); } private void updateFruit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); double price = Double.parseDouble(request.getParameter("price")); int stock = Integer.parseInt(request.getParameter("stock")); Fruit fruit = new Fruit(); fruit.setId(id); fruit.setName(name); fruit.setPrice(price); fruit.setStock(stock); dao.updateFruit(fruit); response.sendRedirect("index.jsp"); } private void searchFruit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); List<Fruit> fruits = dao.searchFruit(name); request.setAttribute("fruits", fruits); request.getRequestDispatcher("search.jsp").forward(request, response); } } ``` 在servlet中,我们通过判断"action"参数来确定用户的请求,然后调用相应的方法来处理请求。例如,如果用户想要添加水果,我们就调用"addFruit"方法。 在"addFruit"方法中,我们首先获取用户提交的水果信息,然后创建一个Fruit对象,并将水果信息设置到对象中。最后,调用FruitDao中的"addFruit"方法,将水果信息保存到数据库中。 类似的,在其他方法中,我们可以调用FruitDao中的方法来删除、更新、查询水果的信息。 最后,我们需要编写jsp页面来展示水果管理系统的界面。例如,我们可以创建一个名为"index.jsp"的页面来展示水果列表,代码如下: ```html <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>水果管理系统</title> </head> <body> <h1>水果列表</h1> <table> <tr> <th>ID</th> <th>名称</th> <th>价格</th> <th>库存</th> <th>操作</th> </tr> <c:forEach var="fruit" items="${fruits}"> <tr> <td>${fruit.id}</td> <td>${fruit.name}</td> <td>${fruit.price}</td> <td>${fruit.stock}</td> <td> <a href="update.jsp?id=${fruit.id}&name=${fruit.name}&price=${fruit.price}&stock=${fruit.stock}">修改</a> <a href="FruitServlet?action=delete&id=${fruit.id}">删除</a> </td> </tr> </c:forEach> </table> <h1>添加水果</h1> <form action="FruitServlet" method="post"> <input type="hidden" name="action" value="add"> <label>名称:</label><input type="text" name="name"><br> <label>价格:</label><input type="text" name="price"><br> <label>库存:</label><input type="text" name="stock"><br> <input type="submit" value="添加"> </form> <h1>搜索水果</h1> <form action="FruitServlet" method="post"> <input type="hidden" name="action" value="search"> <label>名称:</label><input type="text" name="name"><br> <input type="submit" value="搜索"> </form> </body> </html> ``` 在页面中,我们使用了JSTL标签库来循环展示水果列表。同时,我们也提供了添加、删除、修改、搜索水果的功能。例如,用户可以通过点击"修改"链接来修改水果信息。 以上就是一个简单的水果管理系统的示例。当然,实际开发中还有很多细节需要注意,例如输入参数的验证、异常处理等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值