编写单一Servlet程序,仅用Servlet和jdbc技术不用spring。根据表名参数利用Connition.getMetaData()获取表的元数据字段结构,通过Response.getWriter()对象输出网页表单,实现数据列表、添加和编辑功能。
时间: 2024-02-05 16:10:46 浏览: 127
以下是一个简单的Servlet程序,用于实现数据列表、添加和编辑功能:
```java
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tableName = request.getParameter("tableName");
if (tableName == null || tableName.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing table name parameter.");
return;
}
try {
// Connect to the database
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "username", "password");
// Get the metadata for the table
DatabaseMetaData metadata = conn.getMetaData();
ResultSet rs = metadata.getColumns(null, null, tableName, null);
// Output the HTML form
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>" + tableName + "</title></head><body>");
// Output the data table
out.println("<h1>" + tableName + "</h1>");
out.println("<table><tr>");
while (rs.next()) {
out.println("<th>" + rs.getString("COLUMN_NAME") + "</th>");
}
out.println("<th>Edit</th></tr>");
rs = conn.createStatement().executeQuery("SELECT * FROM " + tableName);
while (rs.next()) {
out.println("<tr>");
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
out.println("<td>" + rs.getString(i) + "</td>");
}
out.println("<td><a href=\"edit?tableName=" + tableName + "&id=" + rs.getInt(1) + "\">Edit</a></td>");
out.println("</tr>");
}
out.println("</table>");
// Output the add form
out.println("<h2>Add " + tableName + "</h2>");
out.println("<form action=\"add\" method=\"POST\">");
rs = metadata.getColumns(null, null, tableName, null);
while (rs.next()) {
out.println(rs.getString("COLUMN_NAME") + ": <input type=\"text\" name=\"" + rs.getString("COLUMN_NAME") + "\"><br>");
}
out.println("<input type=\"hidden\" name=\"tableName\" value=\"" + tableName + "\">");
out.println("<input type=\"submit\" value=\"Add\">");
out.println("</form>");
conn.close();
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tableName = request.getParameter("tableName");
if (tableName == null || tableName.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing table name parameter.");
return;
}
try {
// Connect to the database
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "username", "password");
// Insert the new record
String sql = "INSERT INTO " + tableName + " (";
String values = " VALUES (";
ResultSet rs = conn.getMetaData().getColumns(null, null, tableName, null);
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");
String value = request.getParameter(columnName);
sql += columnName + ",";
values += "'" + value + "',";
}
sql = sql.substring(0, sql.length() - 1) + ")";
values = values.substring(0, values.length() - 1) + ")";
conn.createStatement().executeUpdate(sql + values);
conn.close();
response.sendRedirect(request.getContextPath() + "/myServlet?tableName=" + tableName);
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
}
}
}
```
该程序会根据 `tableName` 参数获取数据库表的元数据字段结构,并且输出一个网页表单来实现数据列表、添加和编辑功能。其中,`doGet` 方法用于处理 HTTP GET 请求,`doPost` 方法用于处理 HTTP POST 请求。在 `doGet` 方法中,我们首先根据 `tableName` 参数获取数据库表的元数据字段结构,然后输出一个 HTML 表格来展示数据列表,并且输出一个 HTML 表单来实现添加功能。在 `doPost` 方法中,我们首先根据 `tableName` 参数获取数据库表的元数据字段结构,然后根据用户提交的表单数据插入一个新记录,并且重定向到数据列表页面。
注意,上述代码中的数据库连接 URL、用户名和密码需要根据实际情况进行修改。同时,该程序仅作为一个示例,实际使用中需要进行更多的安全性和性能优化。
阅读全文