Java 导航按钮不工作

Java 导航按钮不工作,java,ms-access,Java,Ms Access,我读过一篇类似的帖子,但我仍然不知道问题出在哪里 我在ms access中创建了一个名为DOCTOR的表,有以下列:DoctorID(数字)、Name(文本)、PhoneNumber(数字)、Department(文本)和Specialization(文本) 我通过UCanAccess将数据库连接到java,下面是获得连接的代码 import java.sql.*; public class Doctor { public static Connection connection;

我读过一篇类似的帖子,但我仍然不知道问题出在哪里

我在ms access中创建了一个名为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的方法来显示从数据库到textfield的数据

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();
        }
    }
下面是按钮1的代码,它是下一个按钮

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();
            }
        }

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

当然,您可以做您正在做的事情,一次显示一条记录,但是您应该真正地在数据库中查询同一条记录,一次显示一条特定的记录。为此,您可以修改SQLSELECT语句,添加WHERE子句语句,并为每个数据库表记录播放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表中,其中表记录已被删除。您可能会认为被删除的ID槽将可用于添加到表中的下一条记录,因此将保留被删除的ID值,但事实并非如此。自动索引增量(autonumber)只是继续提供递增的增量值。当然,有一些方法可以修复这种排序不匹配,但它们从来都不是一个好主意,应该真正避免,因为这样做确实会打乱表关系和数据库中的其他事情。总之,在试验数据库之前,请务必先备份该数据库

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

您的前进(下一步)导航按钮:

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(); }
}
if(evt.getSource()==nextButton){
试一试{
connection=Doctor.connect();
connection.setAutoCommit(false);
数字++;
长最大值=0,最小值=0;
结果集结果;
语句Statement=connection.createStatement(ResultSet.TYPE\u SCROLL\u不敏感,ResultSet.CONCUR\u可更新);
//获取医生表中的最小医生ID值。
String sql0=“选择最小值(DoctorID)作为医生的下限”;
结果=statement.executeQuery(sql0);
while(results.next()){min=results.getLong(“lowstid”);}
//获取医生表中的最大医生ID值。
sql0=“从医生中选择最大值(医生ID)作为最高ID”;
结果=statement.executeQuery(sql0);
while(results.next(){max=results.getLong(“HighestID”);}
if(max-min){previousButton.setEnabled(true);}
如果(数量>最大值){
nextButton.setEnabled(false);
showMessageDialog(null,“无更多记录”);
数字--;
}
结果=空;
while(结果==null){
字符串sql1=“从医生中选择*,其中DoctorID=“+number+”;”;
结果=statement.executeQuery(sql1);
长id=0;
//填写GUI表单字段。。。。
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(“专门化”));
commit();
返回;
}
// ----------------------------------------------------------
如果(id!=number){results=null;number++;}
}
}
catch(异常ex){ex.printStackTrace();}
}
您的反向(上一个)导航按钮:

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(); }
}
if(evt.getSource()==previousButton){
试一试{
connection=Doctor.connect();
connection.setAutoCommit(false);
数字--;
长最大值=0,最小值=0;
结果集结果;
语句Statement=connection.createStatement(ResultSet.TYPE\u SCROLL\u不敏感,ResultSet.CONCUR\u可更新);
//获取医生表中的最小医生ID值。
String sql0=“选择最小值(DoctorID)作为医生的下限”;
结果=statement.executeQuery(sql0);
while(results.next()){min=results.getLong(“lowstid”);}
// --------------------------------------------------------------------------
//获取医生表中的最大医生ID值。
sql0=“从医生中选择最大值(医生ID)作为最高ID”;
结果=statement.executeQuery(sql0);
while(results.next(){max=results.getLong(“HighestID”);}
// --------------------------------------------------------------------------            
如果(最大值)