私は、JDBC ODBC 接続を使用して Ms SQL サーバーに接続する Stock Inventory System ソフトウェアに取り組んでいます。結果セット カーソルを次の行と後方に移動したいと考えています。接続は機能し、プログラムはデータベースからフィールドを取得できるため、問題はありません。
ここにあるコードは、「次へ」というラベルの付いたボタンにあります。このボタンをクリックすると、データベースの次の行に移動し、その行からデータを取得する必要があります。取得したデータは「テキストフィールド」に表示する必要があります。問題は、[次へ] をクリックしても何も起こらないことです。
private void NextBtnActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbc");
Connection con;
con = DriverManager.getConnection("jdbc:odbc:StockInventory","sa","123");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query = "select * from Stocktbl";
ResultSet rs;
rs = stmt.executeQuery(query);
if(!rs.isLast()) {
rs.next();
TxtStockid.setText(rs.getString("StockId"));
TxtItem.setText(rs.getString("ItemName"));
TxtQuantity.setText(rs.getString("Quantity"));
TxtUnitprice.setText(rs.getString("UnitPrice"));
TxtNetprice.setText(rs.getString("NetPrice"));
TxtUnitweight.setText(rs.getString("UnitWeight"));
TxtNetweight.setText(rs.getString("Netweight"));
TxtDescription.setText(rs.getString("Description"));
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException ex)
{
Logger.getLogger(StockScr.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ClassNotFoundException ex)
{
Logger.getLogger(StockScr.class.getName()).log(Level.SEVERE, null, ex);
}
}
プログラムの残りのコーディングは次のとおりです。このパネルに「次へ」ボタンが追加されます。
public class StockScr extends javax.swing.JPanel {
ResultSet rs;
Connection con = null;
public StockScr() {
initComponents();
ShowTable();
}
void ShowTable(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbc");
Connection con;
con = DriverManager.getConnection("jdbc:odbc:StockInventory","sa","123");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String query = "select * from Stocktbl";
ResultSet rs;
rs = stmt.executeQuery(query);
rs.first();
TxtStockid.setText(rs.getString("StockId"));
TxtItem.setText(rs.getString("ItemName"));
TxtQuantity.setText(rs.getString("Quantity"));
TxtUnitprice.setText(rs.getString("UnitPrice"));
TxtNetprice.setText(rs.getString("NetPrice"));
TxtUnitweight.setText(rs.getString("UnitWeight"));
TxtNetweight.setText(rs.getString("Netweight"));
TxtDescription.setText(rs.getString("Description"));
rs.close();
stmt.close();
con.close();
}
catch (SQLException ex)
{
Logger.getLogger(StockScr.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ClassNotFoundException ex)
{
Logger.getLogger(StockScr.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void fetchResultSet()
{
try {
if(con==null || con.isClosed())
{
Class.forName("sun.jdbc.odbc.JdbcOdbc");
con = DriverManager.getConnection("jdbc:odbc:StockInventory","sa","123");
}
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String query = "select * from Stocktbl";
rs = stmt.executeQuery(query);
}catch(Exception ex){
System.out.println(ex);
Logger.getLogger(StockScr.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
if(con != null)
{
con.close();
}
}catch(Exception ex){
}
}
private void NextBtnActionPerformed(java.awt.event.ActionEvent evt) {
try
{
if (rs == null)
{
fetchResultSet();
}
if (rs!=null)
{
if (rs.next())
{
TxtStockid.setText(rs.getString("StockId"));
TxtItem.setText(rs.getString("ItemName"));
TxtQuantity.setText(rs.getString("Quantity"));
TxtUnitprice.setText(rs.getString("UnitPrice"));
TxtNetprice.setText(rs.getString("NetPrice"));
TxtUnitweight.setText(rs.getString("UnitWeight"));
TxtNetweight.setText(rs.getString("Netweight"));
TxtDescription.setText(rs.getString("Description"));
}
else
{
rs = null;
}
}
}catch(Exception ex){
System.out.println(ex);
}
}