package com.lyq;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindServlet extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("oracle.jdbc.driver.OracleDriver");
// 数据库连接字符串
String url = "jdbc:oracle:thin:@localhost:1521/orcl";
// 数据库用户名
String username = "scott";
// 数据库密码
String password = "long";
// 创建Connection连接
Connection conn = DriverManager.getConnection(url,username,password);
// 获取Statement
Statement stmt = conn.createStatement();
// 添加图书信息的SQL语句
String sql = "select * from books";
// 执行查询
ResultSet rs = stmt.executeQuery(sql);
// 实例化List对象
List<Book> list = new ArrayList<Book>();
// 判断光标向后移动,并判断是否有效
while(rs.next()){
// 实例化Book对象
Book book = new Book();
// 对id属性赋值
book.setId(rs.getInt("id"));
// 对name属性赋值
book.setName(rs.getString("name"));
// 对price属性赋值
book.setPrice(rs.getDouble("price"));
// 对bookCount属性赋值
book.setBookCount(rs.getInt("bookCount"));
// 对author属性赋值
book.setAuthor(rs.getString("author"));
// 将图书对象添加到集合中
list.add(book);
}
// 将图书集合放置到request之中
request.setAttribute("list", list);
rs.close(); // 关闭ResultSet
stmt.close(); // 关闭Statement
conn.close(); // 关闭Connection
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 请求转发到book_list.jsp
request.getRequestDispatcher("book_list.jsp").forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}