java - SQL语法错误异常 : Table/View 'USERS' does not exist

标签 java html jpa

我正在做一个简单的登录,为了测试它不会检查您是否有效,而只是打印所有用户(仅用于测试)。我不知道为什么,但它给了我这个错误:

Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'USERS' does not exist.
Error Code: 30000
Call: SELECT EMAIL, PASSWORD FROM USERS
Query: ReadAllQuery(name="Users.findAll" referenceClass=Users sql="SELECT EMAIL, PASSWORD FROM USERS")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
at com.mycompany.cinematographer.LoginService.findAll(LoginService.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
... 44 more

我已经清楚地创建了一个表用户:

enter image description here

真的不知道如何识别问题并解决它,所以欢迎任何帮助。我在下面发布了一些代码以供引用:

html 格式:

<div class="container-fluid">             
  <form class="form-signin" method="post" role="form" action="LoginServlet">
    <h2 class="form-signin-heading">Please log in</h2>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" name="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" name="inputPassword" class="form-control" placeholder="Password" required>
    <div class="checkbox">
      <label>
        <input type="checkbox" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Log in</button>
  </form>         
</div>

实体类:

@Entity
@Table(name = "USERS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
@NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password")})
public class Users implements Serializable
{
private static final long serialVersionUID = 1L;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "EMAIL")
private String email;
@Size(max = 100)
@Column(name = "PASSWORD")
private String password;

public Users()
{
}

public Users(String email)
{
    this.email = email;
}

public String getEmail()
{
    return email;
}

public void setEmail(String email)
{
    this.email = email;
}

public String getPassword()
{
    return password;
}

public void setPassword(String password)
{
    this.password = password;
}

@Override
public int hashCode()
{
    int hash = 0;
    hash += (email != null ? email.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object)
{
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Users)) {
        return false;
    }
    Users other = (Users) object;
    if ((this.email == null && other.email != null) || (this.email != null && !this.email.equals(other.email))) {
        return false;
    }
    return true;
}

@Override
public String toString()
{
    return email + " " + password;
}
}

登录服务:

@Stateless
public class LoginService
{
 @PersistenceContext
private EntityManager em;

public List<Users> findAll(){
    return  em.createNamedQuery("Users.findAll").getResultList();

}
}

小服务程序:

@Inject
private LoginService log;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
{

    StringBuilder str = new StringBuilder();
    List<Users> users = log.findAll();

    for(Users e: users){
        str.append(e.toString());
    }

    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet LoginServlet</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet LoginServlet at " + str + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
    }

持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="com.mycompany_Cinematographer_war_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>jdbc/users</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>

最佳答案

您是否尝试过完全限定名称? @Table(name = "APP.USERS")

从 APP.USERS 选择电子邮件、密码

关于java - SQL语法错误异常 : Table/View 'USERS' does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467802/

相关文章:

java - 使用多对一映射编写插入 HQL 查询

java - 如何在 JPA 2.0 中创建类似 'instance of' 的查询?

java - Java错误: The method setParent(IndexNode) is undefined for the type DocumentIndex and type IndexNode

java - 在访问值之前调用 "Optional#isPresent()"

javascript - 从 JavaScript/jQuery 中的 html 输入框中删除重复的条目

html - 试图从另一个网站永久链接中提取数据。如何?

java - Spring 数据 JPA 中 getReference 方法的替代方法

java - 基于 GPS 数据的全局栅格

java - 一个对象可以存储在栈上而不是java中的堆吗?

html - 在 iFrame 中跳转链接