1

Java と Jersey API を使用して REST Web サービスを作成しています。基本的な REST サービスは正常に動作しますが、DB 接続を追加すると、Class Not Found Exception と SQL Exception - No driver found が発生します。Eclipse ビルド パスに ojdbc6.jar ファイルを含めました。Javaアプリケーションを作成する場合、同じコードを使用して正常に動作します。以下にコードを追加しました。誰かが何かを提案できますか。編集: jar ファイルを WEB-INF lib ディレクトリに含めました。しかし、コードを実行しようとすると、次のエラーが発生します: HTTP ステータス 405 - メソッドが許可されていません

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内にあることを確認する必要があります。libWAR

于 2012-06-21T14:17:41.800 に答える