2

我有连接到我的数据库正在运行。我可以毫无问题地执行以下操作:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/people", "root", "r00t");

PreparedStatement statement = (PreparedStatement) conn.prepareStatement("select * from users");
ResultSet result = statement.executeQuery();

但是,在设置 JPA 和持久类之后,我总是收到“未选择数据库”错误。看起来我不需要调整我的数据库配置(MySQL 连接到 Glassfish 3.1),否则上面的代码将不起作用。

正在拨打的电话:

SELECT USERNAME, FIRSTNAME, LASTNAME, PASSWORD, PERMISSION FROM users

我已经直接在 MySQL Workbench 中尝试过这个调用,但它不起作用。

这个确实有效:

SELECT USERNAME, FIRSTNAME, LASTNAME, PASSWORD, PERMISSION FROM people.users

我一直在玩,似乎无法在任何地方添加数据库名称(“人”)。这是我到目前为止所拥有的:

使用 EclipseLink 2.0.x

JPA 实现:禁用库配置

连接:本地 MySQL(我的数据库已成功连接)

架构:人

从我的 servlet:

package com.lowe.samples;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @PersistenceContext
    private EntityManager em;

    public TestServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter(); 
        out.println("<h1>DataBase Test:<h1>");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/people", "root", "r00t");

            PreparedStatement statement = (PreparedStatement) conn.prepareStatement("select * from users");
            ResultSet result = statement.executeQuery();

            // prep the table
            out.print("<table border=\"5\">");
            out.print("<tr>");
            out.print("<td>UserName</td>");
            out.print("<td>FirstName</td>");
            out.print("<td>LastName</td>");
            out.print("<td>Password</td>");
            out.print("<td>Permission</td>");
            out.print("</tr>");

            while(result.next()) {
                out.print("<tr>");
                out.print("<td>" + result.getString(1) + "</td>");
                out.print("<td>" + result.getString(2) + "</td>");
                out.print("<td>" + result.getString(3) + "</td>");
                out.print("<td>" + result.getString(4) + "</td>");
                out.print("<td>" + result.getString(5) + "</td>");
                out.print("</tr>");
            }

            out.print("</table>");

            User u = (User)this.em.createNamedQuery("User.findAll").getResultList();
            out.print("User Name: " + u.getFirstName());

        } catch (ClassNotFoundException e) {
            out.print("<h4>" + e.getMessage() + "</h4>");
            e.printStackTrace();
        } catch (SQLException e) {
            out.print("<h4>" + e.getMessage() + "</h4>");
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

我的持久性课程:

package com.lowe.samples;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the users database table.
 */
@Entity
@Table(name="users")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String userName;

    private String firstName;

    private String permission;

    private String lastName;

    private String password;

    public User() {
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getPermission() {
        return this.permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return this.password;
    }

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

}

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="MyFriends">
        <jta-data-source>jdbc/MySQLDataSource</jta-data-source>
        <class>com.lowe.samples.User</class>
    </persistence-unit>
</persistence>
4

4 回答 4

4

第一段代码是使用JDBC加载数据库驱动程序,连接并执行查询。它工作的事实表明您的类路径中有 MySQL 驱动程序,并且您可以使用提供的 URL 和凭据连接到您的数据库。它与 JPA 无关。

您的persistent.xml具有名为“MyFriends”的持久性单元,并配置了 JTA 感知数据源:

<jta-data-source>jdbc/MySQLDataSource</jta-data-source>

此数据源绑定到JNDI

根据您收到的错误,连接池配置错误,可能在 URL 中缺少数据库名称。

于 2013-09-15T20:13:56.373 回答
2

如果您在指定 db url 时使用 jta 数据源或使用 hibernate 属性,请在 db ip-address 之后提及 db 名称,例如 jdbc:mysql://:3306/。如果在您的实体中以某种方式无法做到这一点,您可以指定表所在的@Table(name="abcd",catalog="dbname/schema name") 数据库 如果您使用来自一台服务器的多个数据库,也可以执行此操作。

于 2017-02-27T07:58:14.697 回答
0

此问题的另一种可能解决方案是确保在 URL 的 URL 中指定数据库jdbc-connection-pool

我遇到了这个问题并且有这个 URL:

<property name="URL" value="jdbc:mysql://my_host:3306"></property>

当我将数据库添加到最后时,问题解决了:

<property name="URL" value="jdbc:mysql://my_host:3306/my_db_name"></property>

希望这对将来的某人有所帮助。

于 2018-04-21T15:35:36.910 回答
0

如果您使用 hbm.xml,请尝试以下操作

<class name="com.javapapers.data.user"  table ="user"  catalog
="<DBNAME.Schema>">
于 2017-11-02T00:15:07.977 回答