eclipse增删查改
时间: 2023-11-17 10:01:25 浏览: 282
Eclipse是一款常用的Java集成开发环境(IDE),可以用来进行Java程序的开发、调试和部署。在Eclipse中进行增删查改操作,需要使用Java Web开发中的相关技术,如Servlet、JSP、JDBC等。其中,JSP可以用来实现页面的展示和数据的传递,Servlet可以用来处理请求和响应,JDBC可以用来连接数据库并进行数据的增删查改操作。下面是一个简单的示例代码,演示如何在Eclipse中使用JSP和Servlet实现数据的增删查改操作。
<<引用>>
1. 增加数据:
在JSP页面中,使用表单提交数据到Servlet,Servlet中使用JDBC连接数据库并执行插入语句,将数据插入到数据库中。
```jsp
<form action="AddServlet" method="post">
<label>姓名:</label><input type="text" name="name"><br>
<label>年龄:</label><input type="text" name="age"><br>
<label>性别:</label><input type="text" name="gender"><br>
<input type="submit" value="提交">
</form>
```
```java
public class AddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "改成你的!");
Statement stmt = con.createStatement();
String sql = "insert into student(name, age, gender) values('" + name + "', " + age + ", '" + gender + "')";
int i = stmt.executeUpdate(sql);
if (i == 1) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("error.jsp");
}
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
2. 删除数据:
在JSP页面中,使用超链接或表单提交数据到Servlet,Servlet中使用JDBC连接数据库并执行删除语句,将数据从数据库中删除。
```jsp
<a href="DeleteServlet?id=<%=rs.getInt("id")%>">删除</a>
```
```java
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
int id = Integer.parseInt(request.getParameter("id"));
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "改成你的!");
Statement stmt = con.createStatement();
String sql = "delete from student where id=" + id;
int i = stmt.executeUpdate(sql);
if (i == 1) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("error.jsp");
}
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
3. 修改数据:
在JSP页面中,使用超链接或表单提交数据到Servlet,Servlet中使用JDBC连接数据库并执行更新语句,将数据更新到数据库中。
```jsp
<a href="UpdateServlet?id=<%=rs.getInt("id")%>&name=<%=rs.getString("name")%>&age=<%=rs.getInt("age")%>&gender=<%=rs.getString("gender")%>">修改</a>
```
```java
public class UpdateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "改成你的!");
Statement stmt = con.createStatement();
String sql = "update student set name='" + name + "', age=" + age + ", gender='" + gender + "' where id=" + id;
int i = stmt.executeUpdate(sql);
if (i == 1) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("error.jsp");
}
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
4. 查询数据:
在JSP页面中,使用JDBC连接数据库并执行查询语句,将数据从数据库中查询出来并展示在页面上。
```jsp
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "改成你的!");
Statement stmt = con.createStatement();
String sql = "select * from student";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getInt("age")%></td>
<td><%=rs.getString("gender")%></td>
<td><a href="UpdateServlet?id=<%=rs.getInt("id")%>&name=<%=rs.getString("name")%>&age=<%=rs.getInt("age")%>&gender=<%=rs.getString("gender")%>">修改</a></td>
<td><a href="DeleteServlet?id=<%=rs.getInt("id")%>">删除</a></td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
```
阅读全文