jsp实现增删改查功能
时间: 2023-12-26 22:03:41 浏览: 49
JSP(JavaServer Pages)是一种动态网页开发技术,可以使用Java代码嵌入到HTML页面中。实现增删改查功能需要借助Java Web开发框架,比如Spring MVC、Struts2等。下面是一个使用JSP和JDBC实现增删改查功能的示例。
1.建立数据库连接
在JSP页面中使用JDBC连接数据库,可以在页面上显示数据库中的数据。
```java
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from user");
while(rs.next()){
%>
<tr>
<td><%=rs.getString("id")%></td>
<td><%=rs.getString("username")%></td>
<td><%=rs.getString("password")%></td>
<td><%=rs.getString("email")%></td>
<td><%=rs.getString("phone")%></td>
<td><input type="button" value="修改"></td>
<td><input type="button" value="删除"></td>
</tr>
<%
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
%>
```
2.添加数据
在JSP页面中使用JDBC添加数据到数据库中。
```java
<%
Connection conn=null;
PreparedStatement pstmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
String sql="insert into user(username,password,email,phone) values(?,?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("username"));
pstmt.setString(2, request.getParameter("password"));
pstmt.setString(3, request.getParameter("email"));
pstmt.setString(4, request.getParameter("phone"));
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
%>
```
3.修改数据
在JSP页面中使用JDBC修改数据库中的数据。
```java
<%
Connection conn=null;
PreparedStatement pstmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
String sql="update user set username=?,password=?,email=?,phone=? where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("username"));
pstmt.setString(2, request.getParameter("password"));
pstmt.setString(3, request.getParameter("email"));
pstmt.setString(4, request.getParameter("phone"));
pstmt.setString(5, request.getParameter("id"));
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
%>
```
4.删除数据
在JSP页面中使用JDBC删除数据库中的数据。
```java
<%
Connection conn=null;
PreparedStatement pstmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
String sql="delete from user where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("id"));
pstmt.executeUpdate();
response.sendRedirect("index.jsp");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
%>
```
以上是一个简单的使用JSP和JDBC实现增删改查功能的示例,但这种方式并不是安全的,因为JSP代码容易受到SQL注入攻击,建议使用Spring MVC等Java Web框架来实现。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)