我有连接到我的数据库正在运行。我可以毫无问题地执行以下操作:
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>