编写单一Servlet程序,仅用Servlet和jdbc技术不用spring。根据表名参数利用Connection.getMetaData()获取表的元数据字段结构,通过Response.getWriter()对象输出网页表单,实现数据列表、添加和编辑功能。
时间: 2023-12-10 07:42:59 浏览: 81
servlet实现的一个简单web程序
以下是一个简单的示例代码,实现了数据列表、添加和编辑功能:
```java
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
private Connection conn;
public void init() throws ServletException {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tablename = request.getParameter("tablename");
if (tablename == null) {
tablename = "mytable";
}
try {
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getColumns(null, null, tablename, null);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<html><head><title>" + tablename + "</title></head><body>");
response.getWriter().println("<h1>" + tablename + "</h1>");
response.getWriter().println("<table border=\"1\"><tr>");
while (rs.next()) {
response.getWriter().println("<th>" + rs.getString("COLUMN_NAME") + "</th>");
}
response.getWriter().println("<th>操作</th></tr>");
rs = conn.createStatement().executeQuery("SELECT * FROM " + tablename);
while (rs.next()) {
response.getWriter().println("<tr>");
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
response.getWriter().println("<td>" + rs.getString(i) + "</td>");
}
response.getWriter().println("<td><a href=\"?action=edit&tablename=" + tablename + "&id=" + rs.getString(1) + "\">编辑</a></td>");
response.getWriter().println("</tr>");
}
response.getWriter().println("</table>");
response.getWriter().println("<br><a href=\"?action=add&tablename=" + tablename + "\">添加</a>");
response.getWriter().println("</body></html>");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
String tablename = request.getParameter("tablename");
if (tablename == null) {
tablename = "mytable";
}
if (action != null && action.equals("add")) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + tablename + " VALUES (null, ?, ?, ?)");
ps.setString(1, request.getParameter("field1"));
ps.setString(2, request.getParameter("field2"));
ps.setString(3, request.getParameter("field3"));
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} else if (action != null && action.equals("edit")) {
try {
PreparedStatement ps = conn.prepareStatement("UPDATE " + tablename + " SET field1=?, field2=?, field3=? WHERE id=?");
ps.setString(1, request.getParameter("field1"));
ps.setString(2, request.getParameter("field2"));
ps.setString(3, request.getParameter("field3"));
ps.setString(4, request.getParameter("id"));
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
response.sendRedirect("?tablename=" + tablename);
}
public void destroy() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,我们通过 `Connection.getMetaData()` 方法获取了指定表的元数据信息,然后通过 `Response.getWriter()` 对象输出了一个简单的网页表单,实现了数据列表、添加和编辑功能。具体实现细节请参考代码注释。需要注意的是,这里我们使用了 MySQL 数据库,如果你使用的是其他数据库,请根据实际情况修改代码。
阅读全文