javaweb网上书店源代码
时间: 2023-12-29 17:24:34 浏览: 107
以下是一个基于JSP和MySQL的网上图书购物系统的源代码,供您参考:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书购物网站</title>
</head>
<body>
<h1>欢迎来到图书购物网站!</h1>
<%
// 连接数据库
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/bookstore";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement(); String sql = "SELECT * FROM books";
rs = stmt.executeQuery(sql);
// 显示图书列表
while (rs.next()) {
out.println("<p>" + rs.getString("name") + " - " + rs.getString("author") + " - " + rs.getDouble("price") + "</p>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
```
阅读全文