package chap03;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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 javax.servlet.http.HttpSession;
import javax.sql.DataSource;
/**
* Servlet implementation class GetPersonData
*/
@WebServlet("/GetPersonData")
public class GetPersonData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetPersonData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
String personName, personPwd;
request.setCharacterEncoding("gb2312");
personName = request.getParameter("PersonName").trim();
personPwd = request.getParameter("PersonPwd").trim();
// GetResult rp = new GetResult();
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<head>");
out.println("<title>用户请求信息</title>");
out.println("</head>");
out.println("<BODY>");
out.println("<h3>Request Headers</h3>");
out.println("<table border=1 cellpadding=4 cellspacing=0>");
Enumeration<String> enames = request.getHeaderNames();
while(enames.hasMoreElements()) {
String name = (String)enames.nextElement();
String value = request.getHeader(name);
if((value != null) && (!value.equals(""))){
out.println("<tr>");
out.println("<td>" + name + "</td>");
out.println("<td>" + value + "</td>");
out.println("</tr>");
}
}
out.println("</table>");
out.println("<BR>");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
// Class.forName("oracle.jdbc.driver.OracleDriver");
// conn = DriverManager.getConnection(rp.getOracle_url(), rp.getOracle_user(), rp.getOracle_pwd());
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoraclepool");
conn = ds.getConnection();
if(!conn.isClosed()){
System.out.println("�ѳɹ�����ora12c");
}
String sql = "select * from user_list where user_name = ? and user_password = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, personName);
stmt.setString(2, personPwd);
rs = stmt.executeQuery();
if(rs.next()){
out.println("*******************************************************");
out.println("<BR>");
out.println("UserId:" + rs.getString(1) + " UserName:" + rs.getString(2) + " UserPwd:" + rs.getString(3));
out.println("<BR>");
out.println("*******************************************************");
out.println("<BR>");
HttpSession session = request.getSession();
Integer I = new Integer(1);
session.setAttribute("userGrade", I);
session.setAttribute("userName", personName);
// response.sendRedirect("HelloUser.jsp");
stmt.close();
rs.close();
String sqlAll = "select * from user_list";
stmt = conn.prepareStatement(sqlAll);
// stmt.setString(1, personPwd);
rs = stmt.executeQuery();
out.println("<table>");
out.println("<tr>");
out.println("<th>");
out.println("用户ID ");
out.println("</th>");
out.println("<th>");
out.println("用户名 ");
out.println("</th>");
out.println("<th>");
out.println("密 码 ");
out.println("</th>");
out.println("</tr>");
while(rs.next()){
out.println("<tr>");
out.println("<td>");
out.println(rs.getString(1));
out.println("</td>");
out.println("<td>");
out.println(rs.getString(2));
out.println("</td>");
out.println("<td>");
out.println(rs.getString(3));
out.println("</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("<a href=HelloUser.jsp>分页显示</a>");
out.println("<BR>");
out.println("<a href=contactus.html>联系我们</a>");
out.println("<BR>");
}
else
response.sendRedirect("loginfail.jsp");
}catch(NamingException | SQLException e){
e.printStackTrace();
}finally{
try{
if(stmt != null)
stmt.close();
if(conn != null){
conn.close();
System.out.println("已连接ora12c");
}
if(rs != null)
rs.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
out.println("</BODY>");
out.println("</HTML>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
文章来源:https://www.toymoban.com/news/detail-711564.html
文章来源地址https://www.toymoban.com/news/detail-711564.html
到了这里,关于java serverlets使用数据源连接oracle数据库,并执行查询操作代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!