用代码实现一下 公司正在将servlet、html和jdbc封装为jstl标签框架,可高效率开发应用程序。现需考察面试人员对Servlet、jdbc、html技术掌握情况。 要求编写单一的Servlet程序完成以下功能: 根据tn表名参数,使用request.getParameter('tn') 方法得到表名,根据表名使用connection.getMetaData()动态获取数据表字段结构,依字段名命名表单项,使用response.getWriter()方法输出网页表单,实现数据列表、添加和编辑功能。 表单提交后通过之前以字段名命名的网页表单项,使用Servlet提供的request.getParameter() 方法 接收客户端提交的数据,再使用jdbc技术实现增、删、改、查等功能。
时间: 2024-03-20 09:39:29 浏览: 215
网络软件开发实验六-数据库访问及jstl
好的,以下是我提供的代码实现:
```java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
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 JstlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tableName = request.getParameter("tn");
if (tableName == null || tableName.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Table name is required");
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>" + tableName + "</title></head>");
out.println("<body>");
// Table header
out.println("<table>");
out.println("<tr>");
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
out.println("<th>" + columnName + "</th>");
}
out.println("</tr>");
// Table rows
while (rs.next()) {
out.println("<tr>");
for (int i = 1; i <= columnCount; i++) {
Object value = rs.getObject(i);
out.println("<td>" + value + "</td>");
}
out.println("</tr>");
}
out.println("</table>");
// Add and Update form
out.println("<form method=\"post\">");
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
out.println(columnName + ": <input type=\"text\" name=\"" + columnName + "\"><br>");
}
out.println("<input type=\"submit\" value=\"Add\">");
out.println("</form>");
out.println("</body>");
out.println("</html>");
stmt.close();
conn.close();
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tableName = request.getParameter("tn");
if (tableName == null || tableName.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Table name is required");
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String id = request.getParameter("id");
String sql = "";
if (id == null || id.isEmpty()) {
// Add a new record
sql = "INSERT INTO " + tableName + " (";
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
if (i == columnCount) {
sql += columnName + ") VALUES (";
} else {
sql += columnName + ", ";
}
}
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
String value = request.getParameter(columnName);
if (i == columnCount) {
sql += "'" + value + "')";
} else {
sql += "'" + value + "', ";
}
}
} else {
// Update an existing record
sql = "UPDATE " + tableName + " SET ";
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
String value = request.getParameter(columnName);
if (i == columnCount) {
sql += columnName + "='" + value + "' WHERE id=" + id;
} else {
sql += columnName + "='" + value + "', ";
}
}
}
stmt.executeUpdate(sql);
response.sendRedirect(request.getContextPath() + "/JstlServlet?tn=" + tableName);
stmt.close();
conn.close();
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
e.printStackTrace();
}
}
}
```
这个Servlet程序可以处理GET和POST请求。GET请求会返回表格数据和添加/更新表单,POST请求会根据表单数据添加或更新记录,并重定向到GET请求页面。
需要注意的是,这段代码使用了MySQL数据库,需要在本地部署MySQL数据库才能运行。另外,为了简化代码,这里省略了一些错误处理和输入验证的代码。在实际开发中,需要加入这些代码以提高程序的健壮性和安全性。
阅读全文