导航按钮无效

时间:2017-03-24 18:20:27

标签: java ms-access

我看过类似的帖子,但我仍然无法解决问题。

我在ms访问中创建了一个名为DOCTOR的表,有列:DoctorID(数字),Name(文本),PhoneNumber(数字),Department(文本)和Specialization(文本)

我通过UCanAccess将数据库连接到java,下面是获取连接的代码

import java.sql.*;

public class Doctor 
{

    public static Connection connection; //sharing the memory

    public static Connection connect() throws ClassNotFoundException, SQLException
    {
            String db = "net.ucanaccess.jdbc.UcanaccessDriver";
            Class.forName(db);

            String url = "jdbc:ucanaccess://C:/Users/user.oemuser/Documents/Doctor.accdb";
            connection = DriverManager.getConnection(url);
            return connection;
   }

}

在我的GUI类中,我有一个名为getConnect的方法来显示从数据库到文本字段的数据

public void getConnect()
    {
        try
        {
            connection = Doctor.connect();
            statement=connection.createStatement();
            String sql = "SELECT * FROM DOCTOR";
            results = statement.executeQuery(sql);

            results.next();
            id = results.getInt("DoctorID");
            name = results.getString("DoctorName");
            phone = results.getInt("PhoneNumber");
            dept = results.getString("Department");
            spec = results.getString("Specialization");

            textField1.setText("" +id);
            textField2.setText(name);
            textField3.setText("" +nf3.format(phone));
            textField4.setText(dept);
            textField5.setText(spec);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

及以下是button1的代码,它是下一个按钮。

if(evt.getSource() == button1)
        {
            try
            {
                connection = Doctor.connect();
                connection.setAutoCommit(false); 
                statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                String sql1 = "SELECT * FROM DOCTOR";
                results = statement.executeQuery(sql1);


                if(results.next())
                {
                    textField1.setText("" +results.getInt("DoctorID"));
                    textField2.setText(results.getString("DoctorName"));
                    textField3.setText("" +nf3.format(results.getInt("PhoneNumber")));
                    textField4.setText(results.getString("Department"));
                    textField5.setText(results.getString("Specialization"));
                }
                else
                {
                    results.previous();
                    JOptionPane.showMessageDialog(null, "No more records");
                }
                connection.commit();
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
        }

1 个答案:

答案 0 :(得分:0)

显然,如果要查询特定数据库表中的所有记录,或者至少将结果集放入ArrayList,那么这里使用的最佳组件是JTable,数据库表可以容纳数百万条记录,因此内存消耗可能关注一下。现在,我并不是说你的特定表格包含那么多数据(很多医生),但其他表格可能会这样。

您当然可以执行您正在执行的操作并一次显示一条记录,但您应该一次查询数据库中的同一条记录。您可以通过添加 WHERE 子句语句修改SQL SELECT 语句并为每个数据库表记录播放 ID 来执行此操作像这样:

String sql = "SELECT * FROM DOCTOR WHERE DoctorID = " + number + ";";

但是,我们需要记住,如果您的 DoctorID 字段的架构设置为自动索引,这当然允许数据库自动将递增的数字ID值放入此字段,指数可能不一定是统一的连续顺序,例如:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,.......

相反,它可能按此顺序:

1, 3, 4, 5, 6, 9, 10, 11, 16, 17,....

这种事情发生在已删除表记录的MS Access Tables中。您可能会认为删除的ID插槽可用于添加到表中的下一条记录,因此会保留已删除的ID值,但情况并非如此。自动索引增量(自动编号)只是继续提供增加的增量值。当然有办法解决这种顺序错配,但它们绝不是一个好主意,应该真正避免,因为这样做可能会破坏表关系和数据库中的其他内容。最重要的是,在试验数据库之前总是首先备份该数据库。

因此,要利用 WHERE 子句来对抗有效记录ID,我们需要使用我们的前进和后退导航按钮执行类似的操作:< / p>

您的转发(下一个)导航按钮:

if(evt.getSource() == nextButton) {
    try {
        connection = Doctor.connect();
        connection.setAutoCommit(false); 

        number++;
        long max = 0, min = 0; 
        ResultSet results;
        Statement statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        // Get the minimum DoctorID value within the DOCTOR table.
        String sql0 = "SELECT MIN(DoctorID) AS LowestID from DOCTOR"; 
        results = statement.executeQuery(sql0);
        while (results.next()){ min = results.getLong("LowestID"); }

        // Get the maximum DoctorID value within the DOCTOR table.
        sql0 = "SELECT MAX(DoctorID) AS HighestID from DOCTOR";
        results = statement.executeQuery(sql0);
        while (results.next()){ max = results.getLong("HighestID"); }

        if (max <= 0) { 
            JOptionPane.showMessageDialog(null, "No records found in Doctor Table."); 
            return; 
        }
        if (number > min) { previousButton.setEnabled(true); }

        if (number > max) {
            nextButton.setEnabled(false);
            JOptionPane.showMessageDialog(null, "No more records"); 
            number--;
        }

        results = null;
        while (results == null) {
            String sql1 = "SELECT * FROM DOCTOR WHERE DoctorID = " + number + ";";
            results = statement.executeQuery(sql1);
            long id = 0;
            // Fill The GUI Form Fields....
            while (results.next()){
                //id = results.getLong("DoctorID");
                textField1.setText("" +results.getInt("DoctorID"));
                textField2.setText(results.getString("DoctorName"));
                textField3.setText("" + results.getString("PhoneNumber"));
                textField4.setText(results.getString("Department"));
                textField5.setText(results.getString("Specialization"));

                connection.commit();
                return;
            }
            // ----------------------------------------------------------
            if (id != number) { results = null; number++; }
        }
    }
    catch(Exception ex){ ex.printStackTrace(); }
}

您的反向(上一个)导航按钮:

if(evt.getSource() == previousButton) {
    try {
        connection = Doctor.connect();
        connection.setAutoCommit(false); 

        number--;
        long max = 0, min = 0; 
        ResultSet results;
        Statement statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        // Get the minimum DoctorID value within the DOCTOR table.
        String sql0 = "SELECT MIN(DoctorID) AS LowestID from DOCTOR";
        results = statement.executeQuery(sql0);
        while (results.next()){ min = results.getLong("LowestID"); }
        // --------------------------------------------------------------------------

        // Get the maximum DoctorID value within the DOCTOR table.
        sql0 = "SELECT MAX(DoctorID) AS HighestID from DOCTOR";
        results = statement.executeQuery(sql0);
        while (results.next()){ max = results.getLong("HighestID"); }
        // --------------------------------------------------------------------------            

        if (max <= 0) { 
            JOptionPane.showMessageDialog(null, "No records found in Doctor Table.");
            return;
        }

        if (number < min) { 
            previousButton.setEnabled(false); 
            JOptionPane.showMessageDialog(null, "No more records"); 
            number++;
        }

        if (number < max) { nextButton.setEnabled(true); }

        results = null;
        while (results == null) {
            String sql1 = "SELECT * FROM DOCTOR WHERE DoctorID = " + number + ";";
            results = statement.executeQuery(sql1);
            long id = 0;
            // Fill The GUI Form Fields....
            while (results.next()){
                textField1.setText("" +results.getInt("DoctorID"));
                textField2.setText(results.getString("DoctorName"));
                textField3.setText("" + results.getString("PhoneNumber"));
                textField4.setText(results.getString("Department"));
                textField5.setText(results.getString("Specialization"));

                connection.commit();
                return;
            }
            // ----------------------------------------------------------
            if (id != number) { results = null; number--; }
        }
    }
    catch(Exception ex){ ex.printStackTrace(); }
}

要做的事情......

  1. 为了删除重复的代码,请创建一个名为的方法 getMinID(),返回Long Integer数据类型。允许此方法接受两个字符串参数( fieldName 和 的 表名 )。使用上面的代码部分来将DOCTOR表中的最小DoctorID值收集到新的中 ** getMinID()方法。使用此新方法替换前进(下一个)和下一个(上一个)的上述代码 的按钮。

  2. 为了删除重复的代码,请创建一个名为的方法      getMaxID(),返回Long Integer数据类型。允许此方法接受两个字符串参数( fieldName 和     的 表名 )。使用上面的代码部分来收集DOCTOR表中的最大DoctorID值到新的      getMaxID()方法。使用此新方法替换前进(下一个)和下一个(上一个)的上述代码     的按钮。

  3. 为了删除重复的代码,请创建一个名为的void方法     的 fillFormFields()即可。允许此方法接受两个参数,一个作为Connection( * connection ),另一个作为ResultSet     (结果)。使用上面用于填充GUI的代码部分     将字段表单添加到新的 fillFormFields()方法中。使用这个新的     替换前述代码的方法(下一个)     和Revese(上一页)按钮。

  4. 要阅读的内容可能有帮助:

    The SQL WHERE clause statementSQL ORDER BY statement用于对结果集进行排序。 Searching For Records