postgreSQL 连接池04

前端之家收集整理的这篇文章主要介绍了postgreSQL 连接池04前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1,需要修改

<a href=图片" src="http://b99.photo.store.qq.com/psb?/10c0ddec-acfa-423e-bfa8-a420ed0f2099/EaL4MgG.Meor4o1FJCAqD.QMaPGoorH2DccdWkKxYpw!/b/YY*4Dzt6LQAAYsTUCTt6LgAA">

中的context.xml文件

添加

 
 
  1. <ResourcedriverClassName="org.postgresql.Driver"maxActive="4"maxIdle="2"maxWait="50"
  2. auth="Container"
  3. name="jdbc/postg"password="root"type="javax.sql.DataSource"
  4. url="jdbc:postgresql://192.168.2.150:5432/postgres"username="postgres"/>

工具类:ConnUtil

 
 
  1. packagecom.hw.util;
  2. importjava.sql.Connection;
  3. importjava.sql.DriverManager;
  4. importjava.sql.sqlException;
  5. importjavax.naming.Context;
  6. importjavax.naming.InitialContext;
  7. importjavax.naming.NamingException;
  8. importjavax.sql.DataSource;
  9. publicclassConnUtil
  10. {
  11. publicstaticConnectiongetConn()
  12. {
  13. Connectionconn=null;
  14. try
  15. {
  16. Class.forName("org.postgresql.Driver");
  17. Stringurl="jdbc:postgresql://192.168.2.150:5432/postgres";
  18. try
  19. {
  20. conn=DriverManager.getConnection(url,"postgres","postgres");
  21. }
  22. catch(sqlExceptione)
  23. {
  24. e.printStackTrace();
  25. }
  26. }
  27. catch(ClassNotFoundExceptione)
  28. {
  29. e.printStackTrace();
  30. }
  31. returnconn;
  32. }
  33. publicstaticConnectiongetJNDIConn(){
  34. Contextctx=null;
  35. try
  36. {
  37. ctx=newInitialContext();
  38. }
  39. catch(NamingExceptione1)
  40. {
  41. e1.printStackTrace();
  42. }
  43. Connectionconn=null;
  44. DataSourceds=null;;
  45. try
  46. {
  47. ds=(DataSource)ctx.lookup("java:comp/env/jdbc/postg");
  48. }
  49. catch(NamingExceptione)
  50. {
  51. e.printStackTrace();
  52. }
  53. try
  54. {
  55. conn=ds.getConnection();
  56. }
  57. catch(sqlExceptione)
  58. {
  59. e.printStackTrace();
  60. }
  61. returnconn;
  62. }
  63. }

servlet:

 
 
  1. packagecom.hw.servlet;
  2. importjava.io.IOException;
  3. importjava.io.PrintWriter;
  4. importjava.sql.Connection;
  5. importjava.sql.ResultSet;
  6. importjava.sql.sqlException;
  7. importjava.sql.Statement;
  8. importjavax.servlet.ServletException;
  9. importjavax.servlet.http.HttpServlet;
  10. importjavax.servlet.http.HttpServletRequest;
  11. importjavax.servlet.http.HttpServletResponse;
  12. importcom.hw.util.ConnUtil;
  13. /**
  14. *ServletimplementationclassPostgreServlet
  15. */
  16. publicclassPostgreServletextendsHttpServlet{
  17. privatestaticfinallongserialVersionUID=1L;
  18. /**
  19. *@seeHttpServlet#HttpServlet()
  20. */
  21. publicPostgreServlet(){
  22. super();
  23. }
  24. /**
  25. *@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)
  26. */
  27. protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
  28. Connectionconn=ConnUtil.getJNDIConn();
  29. Stringsql="select*fromstudent";
  30. Statementstmt=null;
  31. ResultSetrs=null;
  32. PrintWriterout=response.getWriter();
  33. try
  34. {
  35. stmt=conn.createStatement();
  36. rs=stmt.executeQuery(sql);
  37. while(rs.next()){
  38. out.println(rs.getInt(1));
  39. }
  40. }
  41. catch(sqlExceptione)
  42. {
  43. e.printStackTrace();
  44. }
  45. }
  46. /**
  47. *@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)
  48. */
  49. protectedvoiddoPost(HttpServletRequestrequest,IOException{
  50. doGet(request,response);
  51. }
  52. }

项目结构如下:

原文链接:https://www.f2er.com/postgresql/196679.html

猜你在找的Postgre SQL相关文章