public String chaBookname(int id) throws Exception{ Connection con=DbUtil.getConnection(); Statement stm=con.createStatement(); String sql="select * from book where id='"+id+"'"; ResultSet rs=DataB.query(sql, stm); if(rs.next()) { return rs.getString("book_name"); } return null; } public void initJTable() { String []colNames= {"编号", "教室编号","状态","借阅时间","归还时间"}; String [][]tableValues={}; tableModel=new DefaultTableModel(tableValues,colNames); table=new JTable(tableModel); table.setSelectionBackground(Color.YELLOW); table.setSelectionForeground(Color.RED); table.setRowHeight(30); } }
时间: 2024-03-28 11:40:22 浏览: 13
这段代码中包含了两个方法,第一个方法名为`chaBookname`,接收一个参数`id`,返回一个字符串类型的书籍名称。这个方法首先获取数据库连接,然后执行一条SQL语句查询`id`等于传入参数`id`的书籍记录。如果查询结果不为空,就返回该书籍记录中的`book_name`字段值;否则返回`null`。第二个方法名为`initJTable`,没有参数,也没有返回值。这个方法主要是初始化一个`JTable`控件,设置表头和表格数据模型,并设置表格的一些样式属性。
相关问题
showALLProduct.jsp里面进去展示所有商品信息,表格有四列,最前面为cherkbox复选框,product数据库表有pid,pname,categpry表有cid和cname,第二列为商品id,即pid,第三列为商品名称,即pname,第四列为类别名称,即cname,最后一列为操作,即删除按钮,有productDao类,ProductService类,BaseServlet类代码怎么写
以下是示例代码,可能需要根据具体情况进行修改:
productDao类:
```java
public class ProductDao {
// 获取所有商品信息
public List<Product> getAllProducts() throws SQLException {
List<Product> products = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
stmt = conn.createStatement();
String sql = "SELECT p.pid, p.pname, c.cname FROM product p, category c WHERE p.cid = c.cid";
rs = stmt.executeQuery(sql);
while (rs.next()) {
Product product = new Product();
product.setPid(rs.getInt("pid"));
product.setPname(rs.getString("pname"));
product.setCname(rs.getString("cname"));
products.add(product);
}
} finally {
DBUtil.close(conn, stmt, rs);
}
return products;
}
// 根据商品id删除商品
public void deleteProduct(int pid) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM product WHERE pid = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, pid);
pstmt.executeUpdate();
} finally {
DBUtil.close(conn, pstmt, null);
}
}
}
```
ProductService类:
```java
public class ProductService {
private ProductDao productDao = new ProductDao();
// 获取所有商品信息
public List<Product> getAllProducts() throws SQLException {
return productDao.getAllProducts();
}
// 根据商品id删除商品
public void deleteProduct(int pid) throws SQLException {
productDao.deleteProduct(pid);
}
}
```
BaseServlet类:
```java
public class BaseServlet extends HttpServlet {
// 获取商品服务对象
protected ProductService productService = new ProductService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置请求编码和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String method = request.getParameter("method");
if ("showAllProducts".equals(method)) {
// 展示所有商品信息
showAllProducts(request, response);
} else if ("deleteProduct".equals(method)) {
// 根据商品id删除商品
deleteProduct(request, response);
} else {
// 默认显示所有商品信息
showAllProducts(request, response);
}
}
// 展示所有商品信息
private void showAllProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productService.getAllProducts();
request.setAttribute("products", products);
request.getRequestDispatcher("showAllProduct.jsp").forward(request, response);
}
// 根据商品id删除商品
private void deleteProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int pid = Integer.parseInt(request.getParameter("pid"));
try {
productService.deleteProduct(pid);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect(request.getContextPath() + "/BaseServlet?method=showAllProducts");
}
}
```
showAllProduct.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>所有商品信息</title>
</head>
<body>
<h1>所有商品信息</h1>
<table border="1">
<tr>
<th>选择</th>
<th>商品ID</th>
<th>商品名称</th>
<th>类别名称</th>
<th>操作</th>
</tr>
<c:forEach items="${products}" var="product">
<tr>
<td><input type="checkbox" name="pid" value="${product.pid}"></td>
<td>${product.pid}</td>
<td>${product.pname}</td>
<td>${product.cname}</td>
<td><a href="<%=request.getContextPath()%>/BaseServlet?method=deleteProduct&pid=${product.pid}">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
注意事项:
1. 需要在web.xml文件中配置BaseServlet的映射关系;
2. 数据库连接和关闭可以使用DBUtil类,需要根据具体情况进行修改;
3. 在删除商品时,应该先提示用户确认是否删除,可以使用JavaScript或弹出框实现。
阅读全文