给出一个完整的项目代码,用servlet和jsp实现在网页中输入数据库,账户密码,库名,表名列名,可以在网页中显示相关数据,点击更新可以对数据库中的数据进行相应的修改,可以在网页和数据库中都看到。
时间: 2024-05-14 21:19:34 浏览: 74
很抱歉,我并不是一个可以执行代码的程序,我只能给你提供一些思路和参考代码。
首先,你需要在 web.xml 中配置一个 servlet,用于处理用户提交的请求。具体配置如下:
```xml
<servlet>
<servlet-name>DatabaseServlet</servlet-name>
<servlet-class>com.example.DatabaseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DatabaseServlet</servlet-name>
<url-pattern>/database</url-pattern>
</servlet-mapping>
```
然后,你需要编写一个 DatabaseServlet 类,处理用户提交的请求。具体代码如下:
```java
package com.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DatabaseServlet extends HttpServlet {
// 数据库连接信息
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "password";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 获取用户请求参数
String dbName = req.getParameter("dbName");
String tableName = req.getParameter("tableName");
String columnName = req.getParameter("columnName");
// 连接数据库
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DB_DRIVER);
conn = DriverManager.getConnection(DB_URL + dbName, DB_USERNAME, DB_PASSWORD);
stmt = conn.createStatement();
// 查询数据库
rs = stmt.executeQuery("SELECT " + columnName + " FROM " + tableName);
// 构造 HTML 响应
StringBuilder sb = new StringBuilder();
sb.append("<html><body>");
sb.append("<table>");
while (rs.next()) {
sb.append("<tr>");
sb.append("<td>" + rs.getString(columnName) + "</td>");
sb.append("</tr>");
}
sb.append("</table>");
sb.append("</body></html>");
// 发送响应
resp.getWriter().write(sb.toString());
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 获取用户请求参数
String dbName = req.getParameter("dbName");
String tableName = req.getParameter("tableName");
String columnName = req.getParameter("columnName");
String id = req.getParameter("id");
String newValue = req.getParameter("newValue");
// 更新数据库
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DB_DRIVER);
conn = DriverManager.getConnection(DB_URL + dbName, DB_USERNAME, DB_PASSWORD);
stmt = conn.createStatement();
stmt.executeUpdate(
"UPDATE " + tableName + " SET " + columnName + "='" + newValue + "' WHERE id=" + id);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 重定向到 GET 请求
resp.sendRedirect(req.getRequestURI() + "?dbName=" + dbName + "&tableName=" + tableName
+ "&columnName=" + columnName);
}
}
```
接下来,你需要编写一个 JSP 页面,用于展示用户查询的结果和更新数据库。具体代码如下:
```html
<html>
<body>
<form method="get" action="/database">
<label>数据库:</label>
<input type="text" name="dbName">
<br>
<label>表名:</label>
<input type="text" name="tableName">
<br>
<label>列名:</label>
<input type="text" name="columnName">
<br>
<input type="submit" value="查询">
</form>
<hr>
<% if (request.getParameter("dbName") != null && request.getParameter("tableName") != null
&& request.getParameter("columnName") != null) { %>
<table>
<thead>
<tr>
<th>ID</th>
<th><%= request.getParameter("columnName") %></th>
<th>操作</th>
</tr>
</thead>
<tbody>
<%
// 连接数据库
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DB_DRIVER);
conn = DriverManager.getConnection(DB_URL + request.getParameter("dbName"),
DB_USERNAME, DB_PASSWORD);
stmt = conn.createStatement();
// 查询数据库
rs = stmt.executeQuery(
"SELECT id, " + request.getParameter("columnName") + " FROM "
+ request.getParameter("tableName"));
// 构造 HTML 响应
while (rs.next()) { %>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString(request.getParameter("columnName")) %></td>
<td>
<form method="post" action="/database">
<input type="hidden" name="dbName" value="<%= request.getParameter("dbName") %>">
<input type="hidden" name="tableName" value="<%= request.getParameter("tableName") %>">
<input type="hidden" name="columnName" value="<%= request.getParameter("columnName") %>">
<input type="hidden" name="id" value="<%= rs.getInt("id") %>">
<input type="text" name="newValue" value="<%= rs.getString(request.getParameter("columnName")) %>">
<input type="submit" value="更新">
</form>
</td>
</tr>
<% }
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} %>
</tbody>
</table>
<% } %>
</body>
</html>
```
最后,你需要将上述代码放到一个 Web 项目中,然后运行该项目,访问 http://localhost:8080/your-project-name/database 即可使用该功能。注意,你需要将代码中的数据库连接信息替换为你自己的连接信息,同时需要在你的数据库中创建相应的表格和数据。
阅读全文