java Spring Controller失败了我的CRUD操作,对此可以做些什么?

kg7wmglp  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(161)

这是浏览器在尝试提交用户后返回的错误图片:
x1c 0d1x的数据
下面是我为用户创建的方法,我也使用Repository来保存这些方法。

  1. private EntityManager entityManager = EntityManagerUtil.getEntityManager();
  2. public String createUser(String account, String password) {
  3. Users user = new UsersEntity(account, password);
  4. entityManager.getTransaction().begin();
  5. Query queryAccount = entityManager.createNativeQuery("Select * From Users Where Account = '" + account + "' ;",
  6. UsersEntity.class);
  7. List<Users> accountVerification = queryAccount.getResultList();
  8. Query queryPassword = entityManager
  9. .createNativeQuery("Select * From Users Where Password = '" + password + "' ;", UsersEntity.class);
  10. List<Users> passwordVerification = queryPassword.getResultList();
  11. if (accountVerification.size() != 0) {
  12. return "This username is already used! Try with another one!";
  13. } else if (passwordVerification.size() != 0) {
  14. return "This password is already used! Try with another one!";
  15. } else {
  16. entityManager.persist(user);
  17. entityManager.getTransaction().commit();
  18. }
  19. entityManager.close();
  20. return "Sucessfull reccord!";
  21. }

字符串
这是我的控制器:

  1. @Controller
  2. public class LibraryHelloWorld {
  3. @RequestMapping(value = "/login", method = RequestMethod.GET)
  4. public ModelAndView loginGet() {
  5. return new ModelAndView("LogInForm");
  6. }
  7. @RequestMapping(value = "/login", method = RequestMethod.POST)
  8. public ModelAndView loginPost(HttpServletRequest request, @RequestParam(value = "firstname") String account,
  9. @RequestParam(value = "lastname") String password) {
  10. JPAUsersRepository userrep = new JPAUsersRepository();
  11. userrep.createUser(account.toString(), password.toString());
  12. return new ModelAndView("Sucessfull");
  13. }


在普通类中,创建用户的工作正常,但在post方法中的@Controller servlet中,它会抛出异常:

  1. INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
  2. Initial SessionFactory creation failed.javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory
  3. Apr 29, 2017 12:10:09 AM org.apache.catalina.core.StandardWrapperValve invoke
  4. SEVERE: Servlet.service() for servlet [library] in context with path [/Library] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
  5. java.lang.ClassNotFoundException: Could not load requested class : com.microsoft.sqlserver.jdbc.SQLServerDriver
  6. at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:319)
  7. at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  8. at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  9. at java.lang.Class.forName0(Native Method)
  10. at java.lang.Class.forName(Class.java:348)
  11. at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149)
  12. at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
  13. at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
  14. at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
  15. at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
  16. at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
  17. at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
  18. at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
  19. at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
  20. at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
  21. at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
  22. at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
  23. at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
  24. at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
  25. at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
  26. at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
  27. at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
  28. at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
  29. at com.infoleven.utility.EntityManagerUtil.<clinit>(EntityManagerUtil.java:11)
  30. at com.infoleven.controller.LibraryHelloWorld.loginPost(LibraryHelloWorld.java:41)
  31. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  32. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  33. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  34. at java.lang.reflect.Method.invoke(Method.java:498)
  35. at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177)
  36. at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
  37. at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
  38. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
  39. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
  40. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
  41. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
  42. at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
  43. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
  44. at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  45. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
  46. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
  47. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  48. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
  49. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
  50. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
  51. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
  52. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
  53. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
  54. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  55. at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
  56. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
  57. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
  58. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
  59. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
  60. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
  61. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
  62. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  63. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  64. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  65. at java.lang.Thread.run(Thread.java:745)
  66. Apr 29, 2017 12:10:24 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop


下面是我的persistence.xml文件:

  1. <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  3. version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  4. <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
  5. <properties>
  6. <property name="javax.persistence.jdbc.driver"
  7. value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
  8. <property name="javax.persistence.jdbc.url"
  9. value="jdbc:sqlserver://localhost:1433;databaseName=Library" />
  10. <property name="javax.persistence.jdbc.user" value="sa" />
  11. <property name="javax.persistence.jdbc.password" value="notgame2" />
  12. <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
  13. <property name="show_sql" value="true" />
  14. <property name="hibernate.temp.use_jdbc_metadata_defaults"
  15. value="false" />
  16. </properties>
  17. </persistence-unit>
  18. </persistence>


对于DB,我使用本地SQL Server。Create方法工作正常,但在当前@Controller服务器中不工作

siotufzp

siotufzp1#

请确保在CLASSPATH中为com.microsoft.sqlserver.jdbc.SQLServerDriver提供了正确的SQL文件
如果你的CLASSPATH中没有这个文件,那么你必须添加它。Hibernate不会为JDBC驱动程序提供第三方代理文件。

相关问题