1

我正在使用 Java 和 Jersey API 创建一个 REST Web 服务。基本的 REST 服务工作正常,但是当我添加数据库连接时,它给了我一个找不到类的异常和一个 SQL 异常 - 找不到驱动程序。我在 Eclipse 构建路径中包含了 ojdbc6.jar 文件。如果我创建一个 Java 应用程序,则使用相同的代码运行良好。我在下面添加了我的代码。有人可以提出一些建议吗?编辑:我将 jar 文件包含在 WEB-INF lib 目录中。但是当我尝试执行代码时,出现以下错误:HTTP Status 405 - Method Not Allowed

public class Note {

    private int noteId;
    private String content;
    private Date createdDate;

    public Note() {}

    public Note(int noteId, String content, Date createdDate) {
        this.noteId = noteId;
        this.content = content;
        this.createdDate = createdDate;
    }

    public int getNoteId() {
        return noteId;
    }

    public void setNoteId(int noteId) {
        this.noteId = noteId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    @Override
    public String toString() {
        return "Note [content=" + content + ", createdDate=" + createdDate
                + ", noteId=" + noteId + "]";
    }
}

public class NoteDAO {

    DatabaseAccess data;
    Connection connection;

    public NoteDAO()
    {
        try {
            data = new DatabaseAccess();
            connect();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void connect() throws SQLException
    {
        try
        {
            data.connect();
            connection = data.connection;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public Note getNoteById(int id) 
    {
        PreparedStatement prepStmt = null;
        try {
            String cSQL = "SELECT * FROM NOTE WHERE NOTEID = 12 ";
            prepStmt = connection.prepareStatement(cSQL);
            prepStmt.setInt(1, id); 
            ResultSet result = prepStmt.executeQuery();
            Note note = new Note();
            while (result.next())
            {
                note.setNoteId(result.getInt(1));
                note.setContent(result.getString(2));
                note.setCreatedDate( (Date) new java.util.Date(result.getDate(3).getTime()));
            }
            return note;
        } catch (SQLException e) {
            e.printStackTrace();
            prepStmt = null;
            return null;
        }
    }




}
@Path("/notes")
public class Notes {

    @Context
    UriInfo uriInfo;
    @Context
    Request request;

    NoteDAO dao = new NoteDAO();





    @Path("{note}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Note getNote(
            @PathParam("note") String idStr) {
        int id = Integer.parseInt(idStr);

        Note note = dao.getNoteById(id);
        if(note==null)
            throw new RuntimeException("Get: Note with " + id +  " not found");
        return note;
    }


public class DatabaseAccess {

    Connection connection = null;

    public void connect() throws SQLException
    {
        String DRIVER = "oracle.jdbc.driver.OracleDriver";
        String URL = "jdbc:oracle:thin:@xx.xxx.xx.xxx:1521:XXXX";
        String UserName = "username";
        String Password = "password";
        try
        {
            Class.forName(DRIVER);
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        try
        {
            connection = DriverManager.getConnection(URL,UserName,Password);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    public void disconnect() throws SQLException
    {
        connection.close();
    }

}
4

1 回答 1

0

如果您使用由应用程序服务器管理的数据源,则需要将ojdbc6.jar库放在lib应用程序服务器的文件夹中。

例如,在 JBoss 上,它将是$JBOSS_HOME/server/default/lib.

这是必需的,因为在这种情况下,数据源是在服务器启动时构建的,并且独立于您的应用程序,这意味着服务器不能使用您的应用程序 JAR。

但是,如果您自己汇集连接,则需要确保连接ojdbc6.jar位于lib应用程序WAR存档的文件夹中。

于 2012-06-21T14:17:41.800 回答