私はかなり単純なJavaサーブレットで作業しています。私がしているポイントは、jdbcリソースのために、主キー列の最大値を返そうとしています。このJava Servletでは、わかりやすいSQL文から結果セットに0が返されるのはなぜですか?
他のサーブレットで頻繁に使用しているので、私はリソースの動作を知っています。私はMySQL Workbenchでsql文をテストしており、SQL jstlを使用してJSP内で同じリソースを使用して同じ文をテストしています。どちらの場合も、フィールド名 "Last_Record"の下に510867925が返されます。これを私のサーブレット内で試してみると、デバッグのために0
が得られます。カウンタ、値、フィールド名、sqlデータ型をページに出力しました。 while ... next()が正しく実行されることを確認するために、getLongメソッドの前後にカウンタを配置しました。ここ
あなたは...ここ
// Save as "binedit_entries\WEB-INF\src\mypkg\BinEdits.java"
package mypkg;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.*;
public class BinEdits extends HttpServlet {
DataSource pool_sql; // Database connection pool
DataSource pool_oracle; // Database connection pool
@Override
public void init() throws ServletException {
try {
// Create a JNDI Initial context to be able to lookup the DataSource
InitialContext ctx = new InitialContext();
// Lookup the DataSource, which will be backed by a pool
// that the application server provides.
pool_sql = (DataSource)ctx.lookup("java:comp/env/jdbc/TrainingRequestKiosk");
if (pool_sql == null)
throw new ServletException("Unknown DataSource 'jdbc/TrainingRequestKiosk'");
pool_oracle = (DataSource)ctx.lookup("java:comp/env/jdbc/dcphl5");
if (pool_oracle == null)
throw new ServletException("Unknown DataSource 'jdbc/dcphl5'");
} catch (NamingException ex) {
ex.printStackTrace();
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Set the response message's MIME type
response.setContentType("text/html;charset=UTF-8");
// Allocate a output writer to write the response message into the network socket
PrintWriter out = response.getWriter();
Connection conn_sql = null;
Connection conn_oracle = null;
Statement stmt_lastLocalRecord = null;
PreparedStatement stmt_getNewRecords = null;
String uri = request.getRequestURI();
String pageName = uri.substring(uri.lastIndexOf("/")+1);
String rootURI = uri.substring(uri.indexOf("/"),uri.lastIndexOf("/")+1);
try {
String contextPath = "http://phl5-ops-dev.ant.amazon.com:8080" + rootURI;
String SiteID = "PHL5";
int eCount = 0;
// Get a connection from the pool
conn_sql = pool_sql.getConnection();
conn_oracle = pool_oracle.getConnection();
String qry_lastLocalRecord = "select max(binedit_entry_id) AS Last_Record FROM bin_edits.BINEDIT_ENTRIES;";
stmt_lastLocalRecord = conn_sql.createStatement();
ResultSet rs_lastLocalRecord = stmt_lastLocalRecord.executeQuery(qry_lastLocalRecord);
ResultSetMetaData rsmd = rs_lastLocalRecord.getMetaData();
long last_Record = 0;
while (rs_lastLocalRecord.next()) {
out.println(++eCount);
rs_lastLocalRecord.getLong("Last_Record");
out.println(++eCount);
}
//
//Begin Web Page
out.println("<!DOCTYPE html><html><head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
out.println("<title>PHL5 Bin Edits</title></head>");
out.println(last_Record);
out.println("<br>");
out.println(rsmd.getColumnTypeName(1));
out.println("<br>");
out.println(rsmd.getColumnName(1));
out.println("</body></html>");
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
out.close();
try {
if (conn_sql != null) conn_sql.close(); // return to pool
if (conn_oracle != null) conn_oracle.close(); // return to pool
if (stmt_lastLocalRecord != null) stmt_lastLocalRecord.close(); // return to pool
if (stmt_getNewRecords != null) stmt_getNewRecords.close(); // return to pool
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
は私が得る応答である...サーブレットである私が見ることを期待する
1 2 0
BIGINT
Last_Record
...
1 2 510867925
BIGINT
Last_Record
の
をしたいと仮定したいです。 'rs_lastLocalRecord.getLong(" Last_Record ");' – ManoDestra
からリターンを取得する必要があります。まず、サーブレットで(概念のテストを除いて)これを行うべきではないことを伝えましょう。 – ACV
サーブレットでこれをしたくないのはなぜですか? – gdove