Reputation: 639
I'm new to Java EE; I have a good background in ASP .NET with WebForms and MVC. I find that nothing is as easy in Java EE as it is in ASP .NET. Nevertheless, all I am trying to do is connect to a local Oracle Express database using persistance on a JBoss runtime. I am absolutely going mad trying to get this to work. In ASP .NET, all I had to do was specify a connection string and walla. I would really appreciate it if someone could either show me what I'm doing wrong or point me to a good resource to use JPA in eclipse.
Here's my code:
persistence.xml
I am sure that the database properties are correct; eclipse was able to make a successful ping.
<?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="Testing" transaction-type="RESOURCE_LOCAL">
<class>model.Testq</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="javax.persistence.jdbc.user" value="system"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
Testq class
This class is just a simple entity in java for a very simple table in the oracle database.
package model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@NamedQuery(name="Testq.findAll", query="SELECT t FROM Testq t")
public class Testq implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="T_ID")
private long tId;
private String descr;
public Testq() {
tId = 0;
descr = "ONE";
}
public long getTId() {
return this.tId;
}
public void setTId(long tId) {
this.tId = tId;
}
public String getDescr() {
return this.descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
}
DB Servlet
I am using this to display the result of a query from the database, but it fails at line 28.
package com.db;
import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
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 model.Testq;
@WebServlet("/DB")
public class DB extends HttpServlet {
private static final long serialVersionUID = 1L;
public DB() {
super();
// TODO Auto-generated constructor stub
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Testq test = new Testq();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Testing");
EntityManager em = emf.createEntityManager();
test = em.createNamedQuery("Testq.findAll", Testq.class).getSingleResult();
em.close();
emf.close();
PrintWriter out = response.getWriter();
out.println(test.getDescr());
}
}
Upvotes: 0
Views: 1143
Reputation: 639
Thank you all so much. it turns out that the driver had to be in the lib folder of WEB-INF. I really appreciate all of your help, specifically Thorbjørn Ravn Andersen for identifying the error when I was sick of dealing with it.
Upvotes: 1