我正在研究一個相當簡單的Java Servlet。我所處的點正在嘗試返回jdbc資源的主鍵列的最大值。在這個Java Servlet中,爲什麼我從已知的好sql語句中獲得結果集中的0
我知道資源的工作原理,因爲我在其他servlet中大量使用它。我已經在MySql Workbench中測試了sql語句,並且還測試了相同的語句,在使用sql jstl的JSP內使用相同的資源。在這兩種情況下,它都以字段名稱「Last_Record」返回510867925。當我嘗試在我的servlet中,我得到0.
用於調試,我打印了一個計數器,值,字段名稱和SQL數據類型的頁面。我在「getLong」方法之前和之後放置了計數器,以確認while ... next()正確執行。
這裏是servlet ...
// 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
你不改變last_Record的價值。你需要從'rs_lastLocalRecord.getLong(「Last_Record」)返回;' – ManoDestra
首先讓我告訴你,你不應該在servlet中做這件事(除了測試這個概念)。 – ACV
爲什麼我不想在servlet中這樣做? – gdove