Advertisement
Guest User

Exception while running the insert service to add country

a guest
Mar 16th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. //Databse.java for database connection with the mysql
  2. public class Database {
  3.  
  4.  
  5. public Connection Get_Connection() throws Exception
  6. {
  7. try
  8. {
  9. String connectionURL = "jdbc:mysql://localhost:3306/atm";
  10. Connection connection = null;
  11. Class.forName("com.mysql.jdbc.Driver").newInstance();
  12. connection = DriverManager.getConnection(connectionURL, "root", "admin");
  13. return connection;
  14. }
  15. catch (SQLException e)
  16. {
  17. throw e;
  18. }
  19. catch (Exception e)
  20. {
  21. throw e;
  22. }
  23. }
  24.  
  25.  
  26. }
  27. /***************************************************************************************************/
  28.  
  29.  
  30. //AlertDao.java to insert data into the country table having country_id and country_name as 2 fields
  31.  
  32. public class AlertDao {
  33. public static boolean setCountry(Connection connection, String countryName) {
  34.  
  35. PreparedStatement ps;
  36. boolean insertStatus = false;
  37.  
  38. try {
  39. ps = connection.prepareStatement("SELECT COUNTRY_ID FROM COUNTRY WHERE COUNTRY_NAME = ?");
  40. ps.setString(1, countryName);
  41. ResultSet rs = ps.executeQuery();
  42. if (!rs.next()) {
  43. rs.close();
  44. ps.close();
  45. ps = connection.prepareStatement("INSERT INTO COUNTRY (COUNTRY_NAME) VALUES (?) ");
  46. ps.setString(1, countryName);
  47. int records = ps.executeUpdate();
  48. if (records > 0) {
  49. insertStatus = true;
  50. }
  51.  
  52. }
  53.  
  54. ps.close();
  55. connection.commit();
  56. connection.close();
  57.  
  58. } catch (Exception e) {
  59. System.out.println(e);
  60. }
  61.  
  62. return insertStatus;
  63.  
  64. }
  65.  
  66. }
  67.  
  68. /***************************************************************************************************/
  69.  
  70. // AlertManager.java for caaling the method of AlertDao.java
  71.  
  72. public class AlertManager {
  73. public static boolean setCountry(String countryName){
  74.  
  75. boolean countryInsertStatus = false;
  76.  
  77. try {
  78. Database database = new Database();
  79. Connection connection = database.Get_Connection();
  80. AlertDao project = new AlertDao();
  81. countryInsertStatus = project.setCountry(connection, countryName);
  82. } catch (Exception e) {
  83. System.out.println(e);
  84. }
  85. return countryInsertStatus;
  86.  
  87. }
  88.  
  89. }
  90.  
  91. /**************************************************************************************************/
  92.  
  93. //AtmService.java for calling the service through URL
  94.  
  95. @Path("/WebService")
  96. public class AtmService {
  97. @GET
  98. @Path("/setCountry")
  99. @Produces("application/json")
  100. public String country(String countryName) {
  101. boolean countryInsert = false;
  102. String feeds = null;
  103.  
  104. try {
  105. AlertManager alertManager = new AlertManager();
  106. countryInsert = alertManager.setCountry(countryName);
  107. Gson gson = new Gson();
  108. System.out.println(gson.toJson(countryInsert));
  109. feeds = gson.toJson(countryInsert);
  110. }
  111.  
  112. catch (Exception e) {
  113. System.out.println("Exception Error"); // Console
  114. }
  115. return feeds;
  116. }
  117.  
  118. }
  119.  
  120.  
  121. /***********************************************************************************************/
  122.  
  123. // url which I am running to run this service is
  124. http://localhost:8080/DemoJavaAngular/REST/WebService/setCountry?countryName=nupur
  125.  
  126.  
  127. but I am getting exception as
  128.  
  129. type Exception report
  130.  
  131. message Servlet.init() for servlet ServletAdaptor threw exception
  132.  
  133. description The server encountered an internal error that prevented it from fulfilling this request.
  134.  
  135. exception
  136.  
  137. javax.servlet.ServletException: Servlet.init() for servlet ServletAdaptor threw exception
  138. org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  139. org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  140. org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
  141. org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
  142. org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
  143. org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
  144. org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
  145. org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
  146. java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  147. java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  148. org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  149. java.lang.Thread.run(Unknown Source)
  150. root cause
  151.  
  152. com.sun.jersey.api.container.ContainerException: Fatal issues found at class webService.AtmService. See logs for more details.
  153. com.sun.jersey.server.impl.application.WebApplicationImpl.newResourceClass(WebApplicationImpl.java:346)
  154. com.sun.jersey.server.impl.application.WebApplicationImpl.getResourceClass(WebApplicationImpl.java:321)
  155. com.sun.jersey.server.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:807)
  156. com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:590)
  157. com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:383)
  158. com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:377)
  159. com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:242)
  160. com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:449)
  161. com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:169)
  162. com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:281)
  163. com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:442)
  164. javax.servlet.GenericServlet.init(GenericServlet.java:158)
  165. org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  166. org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  167. org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
  168. org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
  169. org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
  170. org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
  171. org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
  172. org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
  173. java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  174. java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  175. org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  176. java.lang.Thread.run(Unknown Source)
  177. note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement