编写单一Servlet程序,仅用Servlet和jdbc技术不用spring。根据表名参数利用Connection.getMetaData()获取表的元数据字段结构,通过Response.getWriter()对象输出网页表单,实现数据列表、添加和编辑功能。规定要适应任何数据表
时间: 2023-12-14 09:36:23 浏览: 107
servlet实现的一个简单web程序
下面是一个简单的示例代码,实现了根据表名获取表的元数据字段结构,并输出网页表单来实现数据列表、添加和编辑功能。需要注意的是,这只是一个简单的示例代码,实际应用中可能需要更完善的错误处理、安全性考虑等。
```java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/TableServlet")
public class TableServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tableName = request.getParameter("tableName");
if (tableName == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing parameter: tableName");
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// Get table metadata
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getColumns(null, null, tableName, null);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
// Generate HTML form
PrintWriter out = response.getWriter();
out.println("<html><body>");
// List data
String sql = "SELECT * FROM " + tableName;
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet data = stmt.executeQuery();
out.println("<h2>Data list</h2>");
out.println("<table>");
while (data.next()) {
out.println("<tr>");
for (int i = 1; i <= columnCount; i++) {
String columnName = rsmd.getColumnName(i);
String value = data.getString(columnName);
out.println("<td>" + value + "</td>");
}
out.println("</tr>");
}
out.println("</table>");
// Add form
out.println("<h2>Add new data</h2>");
out.println("<form method=\"post\" action=\"TableServlet\">");
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=\"hidden\" name=\"tableName\" value=\"" + tableName + "\">");
out.println("<input type=\"submit\" value=\"Add\" />");
out.println("</form>");
// Edit form
out.println("<h2>Edit data</h2>");
out.println("<form method=\"post\" action=\"TableServlet\">");
out.println("ID: <input type=\"text\" name=\"id\" /><br/>");
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=\"hidden\" name=\"tableName\" value=\"" + tableName + "\">");
out.println("<input type=\"submit\" value=\"Edit\" />");
out.println("</form>");
out.println("</body></html>");
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tableName = request.getParameter("tableName");
if (tableName == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing parameter: tableName");
return;
}
String id = request.getParameter("id");
if (id == null) {
// Add data
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ").append(tableName).append(" (");
String[] columnNames = request.getParameterValues("column");
for (String columnName : columnNames) {
sb.append(columnName).append(", ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append(") VALUES (");
for (String columnName : columnNames) {
sb.append("?, ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append(")");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement stmt = conn.prepareStatement(sb.toString());
for (int i = 1; i <= columnNames.length; i++) {
stmt.setString(i, request.getParameter(columnNames[i - 1]));
}
stmt.executeUpdate();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
// Edit data
StringBuilder sb = new StringBuilder();
sb.append("UPDATE ").append(tableName).append(" SET ");
String[] columnNames = request.getParameterValues("column");
for (String columnName : columnNames) {
sb.append(columnName).append("=?, ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append(" WHERE id=?");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement stmt = conn.prepareStatement(sb.toString());
for (int i = 1; i <= columnNames.length; i++) {
stmt.setString(i, request.getParameter(columnNames[i - 1]));
}
stmt.setString(columnNames.length + 1, id);
stmt.executeUpdate();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
doGet(request, response);
}
}
```
阅读全文