资源简介
是自己暑假编的,可以简单的利用数据库实现对用户的注册及留言板的提交。
代码片段和文件信息
package com.control;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.model.BookBean;
public class BookQueryServlet extends HttpServlet {
Connection dbConnection;
public void init() {
String driver = “org.postgresql.Driver“;
String dburl = “jdbc:postgresql://127.0.0.1:5432/bookstore“;
String username = “bookstore“;
String password = “bookstore“;
try {
Class.forName(driver); // 加载数据库驱动程序
// 创建数据库连接对象
dbConnection = DriverManager.getConnection(dburl username
password);
} catch (ClassNotFoundException e1) {
} catch (SQLException e2) {
}
}
public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
String bookid = request.getParameter(“bookid“);
try {
String sql = “SELECT * FROM books WHERE bookid=?“;
PreparedStatement pstmt = dbConnection.prepareStatement(sql);
pstmt.setString(1 bookid);
ResultSet rst = pstmt.executeQuery();
if (rst.next()) {
BookBean book = new BookBean();
book.setBookid(rst.getString(“bookid“));
book.settitle(rst.getString(“title“));
book.setAuthor(rst.getString(“author“));
book.setPrice(rst.getDouble(“price“));
book.setPublisher(rst.getString(“publisher“));
request.getSession().setAttribute(“book“ book);
RequestDispatcher view = request
.getRequestDispatcher(“/showBook.jsp“);
view.forward(request response);
} else {
RequestDispatcher view = request
.getRequestDispatcher(“/error.jsp“);
view.forward(request response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void destroy() {
try {
dbConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 450 2012-11-20 13:03 helloapp\.classpath
文件 297 2012-11-19 14:20 helloapp\.myme
文件 1226 2012-11-19 14:37 helloapp\.project
文件 1870 2012-11-19 14:35 helloapp\src\com\control\BookQueryServlet.java
文件 1104 2012-11-19 14:30 helloapp\src\com\model\BookBean.java
文件 254 2012-11-19 14:22 helloapp\WebRoot\bookQuery.jsp
文件 158 2012-11-19 14:37 helloapp\WebRoot\error.jsp
文件 831 2012-11-19 14:20 helloapp\WebRoot\index.jsp
文件 39 2012-11-19 14:20 helloapp\WebRoot\me
文件 786 2012-11-19 14:36 helloapp\WebRoot\showBook.jsp
文件 3274 2012-11-20 13:03 helloapp\WebRoot\WEB-INF\classes\com\control\BookQueryServlet.class
文件 1655 2012-11-20 13:03 helloapp\WebRoot\WEB-INF\classes\com\model\BookBean.class
文件 524911 2012-11-19 14:47 helloapp\WebRoot\WEB-INF\lib\postgresql-9.2-1002.jdbc3.jar
文件 666 2012-11-19 14:33 helloapp\WebRoot\WEB-INF\web.xm
....... 432 2008-11-20 08:01 ly\.classpath
....... 280 2008-11-20 08:01 ly\.myme
....... 1220 2008-11-20 08:01 ly\.project
....... 2767 2008-11-20 08:01 ly\src\db\DB.java
....... 735 2008-11-20 08:01 ly\src\model\LyTable.java
....... 474 2008-11-20 08:01 ly\src\model\User.java
....... 1346 2008-11-20 08:01 ly\src\servlet\AddServlet.java
....... 1912 2008-11-20 08:01 ly\src\servlet\MainServlet.java
....... 870 2008-11-20 08:01 ly\src\servlet\RegisterServlet.java
....... 834 2008-11-20 08:01 ly\WebRoot\index.jsp
....... 595 2008-11-20 08:01 ly\WebRoot\liuyan.jsp
....... 594 2008-11-20 08:01 ly\WebRoot\login.jsp
....... 906 2008-11-20 08:01 ly\WebRoot\main.jsp
....... 39 2008-11-20 08:01 ly\WebRoot\me
....... 523 2008-11-20 08:01 ly\WebRoot\register.jsp
....... 222 2008-11-20 08:01 ly\WebRoot\success.jsp
............此处省略39个文件信息
评论
共有 条评论