Oracle如何建立一个网上书店,请给出代码
时间: 2023-06-27 18:03:43 浏览: 160
建立一个网上书店需要很多的工作,涉及到前端、后端、数据库等多个方面。这里只提供一个Java Web应用程序的代码示例,基于Oracle数据库,用于展示商品信息和处理订单。
前端代码(HTML、CSS、JavaScript)省略:
后端代码(Java Servlet):
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
@WebServlet("/bookstore")
public class BookStoreServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
switch (action) {
case "list":
listBooks(request, response);
break;
case "details":
showDetails(request, response);
break;
default:
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
private void listBooks(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM books");
ResultSet rs = stmt.executeQuery();
List<JSONObject> books = new ArrayList<>();
while (rs.next()) {
JSONObject book = new JSONObject();
book.put("id", rs.getInt("id"));
book.put("title", rs.getString("title"));
book.put("author", rs.getString("author"));
book.put("price", rs.getDouble("price"));
books.add(book);
}
rs.close();
stmt.close();
conn.close();
request.setAttribute("books", books);
request.getRequestDispatcher("/bookstore.jsp").forward(request, response);
} catch (SQLException e) {
throw new ServletException(e);
}
}
private void showDetails(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
try {
Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM books WHERE id = ?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
JSONObject book = new JSONObject();
book.put("id", rs.getInt("id"));
book.put("title", rs.getString("title"));
book.put("author", rs.getString("author"));
book.put("price", rs.getDouble("price"));
book.put("description", rs.getString("description"));
request.setAttribute("book", book);
request.getRequestDispatcher("/bookdetails.jsp").forward(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
throw new ServletException(e);
}
}
private Connection getConnection() throws SQLException {
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "bookstore";
String password = "bookstore";
return DriverManager.getConnection(url, user, password);
}
}
```
数据库表结构:
```sql
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title VARCHAR2(100) NOT NULL,
author VARCHAR2(50) NOT NULL,
price NUMBER(6,2) NOT NULL,
description VARCHAR2(2000) NOT NULL
);
```
以上代码仅仅提供了一个简单的示例,实际上建立一个完整的网上书店需要更多的代码和细节处理。
阅读全文