Guest User

Untitled

a guest
Aug 31st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. public class Pool {
  2. private static DataSource ds;
  3.  
  4. static {
  5. DriverAdapterCPDS cpds = new DriverAdapterCPDS();
  6. try {
  7. cpds.setDriver("com.mysql.jdbc.Driver");
  8. } catch (ClassNotFoundException e) {
  9. e.printStackTrace();
  10. }
  11. cpds.setUrl("jdbc:mysql://localhost:3306/collage");
  12. cpds.setUser("root");
  13. cpds.setPassword("r00t");
  14.  
  15. SharedPoolDataSource tds = new SharedPoolDataSource();
  16. tds.setConnectionPoolDataSource(cpds);
  17. tds.setMaxTotal(10);
  18. /*tds.setMaxWait(50);
  19. tds.setMaxTotal();*/
  20.  
  21. tds.setMaxTotal(50);
  22.  
  23. ds = tds;
  24. }
  25.  
  26. public static Connection getConnection() throws SQLException {
  27. return ds.getConnection();
  28. }
  29.  
  30. public class MainClass {
  31. public static void main(String[] args) {
  32. Connection connection = null;
  33. Statement statement = null;
  34. ResultSet resultSet = null;
  35.  
  36. try {
  37. connection = Pool.getConnection();
  38. // Do work with connection
  39. statement = connection.createStatement();
  40. String selectEmployeesSQL = "select * from students";
  41. resultSet = statement.executeQuery(selectEmployeesSQL);
  42.  
  43. while (resultSet.next()) {
  44. printTestTable(resultSet);
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. } finnaly {
  49. ...
  50. }
  51. }
  52.  
  53. @WebServlet("/Demo.do")
  54. public class DemoController extends HttpServlet {
  55. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  56. ResultSet rsObj = null;
  57. Connection connObj = null;
  58. PreparedStatement pstmtObj = null;
  59.  
  60. try {
  61. connObj = Pool.getConnection();
  62. ...
  63.  
  64. HTTP Status 500 – Internal Server Error
  65.  
  66. Type Exception Report
  67.  
  68. Message Servlet execution threw an exception
  69.  
  70. Description The server encountered an unexpected condition that prevented it from fulfilling the request.
  71.  
  72. Exception
  73.  
  74. javax.servlet.ServletException: Servlet execution threw an exception
  75. org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  76.  
  77. Root Cause
  78.  
  79. java.lang.NoClassDefFoundError: Could not initialize class am.ak.dao.jdbc.connections.Pool
  80. test.DemoController.doGet(DemoController.java:27)
  81. javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
  82. javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  83. org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  84.  
  85. Note The full stack trace of the root cause is available in the server logs.
  86.  
  87. <dependency>
  88. <groupId>org.apache.commons</groupId>
  89. <artifactId>commons-dbcp2</artifactId>
  90. <version>2.5.0</version>
  91. </dependency>
Add Comment
Please, Sign In to add comment