数据库查询在JAVA中不起作用

时间:2016-07-19 19:27:00

标签: java jdbc ucanaccess

我正在编辑JAVA中的图书馆管理系统,我遇到搜索功能问题。每本书都有BookID,Subject,Title,Author等,每个成员都有一个MemberID,Name等。

如果我选择按主题,标题或作者搜索,它会从数据库中返回正确的信息并显示它。但是当我尝试通过BookID搜索时,我收到错误。

这是表单和验证逻辑的代码,然后传递给ListSearchBooks.java和ListSearchMembers.java,它从数据库执行查询并显示它。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SearchBooksAndMembers extends JInternalFrame {
/***************************************************************************
 ***      declaration of the private variables used in the program       ***
 ***************************************************************************/

//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creating the label
private JLabel title = new JLabel("Search for Books and Members");

//for creating the center
private JPanel center = new JPanel();

//for creating the Center Panel
private JPanel centerBooksPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchBooksPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchBooksButtonPanel = new JPanel();

//for creating the table
private JLabel searchBooksLabel = new JLabel(" Search by: ");
//for creating JComboBox
private JComboBox searchBooksTypes;
//for creating String[]
private String[] booksTypes = {"BookID", "Subject", "Title", "Author", "Publisher", "ISBN"};
//for creating the label
private JLabel booksKey = new JLabel(" Write the Keyword: ");
//for cearting the text field
private JTextField booksKeyTextField = new JTextField();
//for creating the button
private JButton searchBooksButton = new JButton("Search");

//for creating the Center Panel
private JPanel centerMembersPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchMembersPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchMembersButtonPanel = new JPanel();

//for creating the table
private JLabel searchMembersLabel = new JLabel(" Search by: ");
//for creating JComboBox
private JComboBox searchMembersTypes;
//for creating String[]
private String[] membersTypes = {"MemberID", "Name", "E-Mail", "Major"};
//for creating the label
private JLabel membersKey = new JLabel(" Write the Keyword: ");
//for cearting the text field
private JTextField membersKeyTextField = new JTextField();
//for creating the button
private JButton searchMembersButton = new JButton("Search");

//for creating the south panel
private JPanel southPanel = new JPanel();
//for creating the button
private JButton cancelButton = new JButton("Cancel");

//for creating an array of string to store the data
private String[] booksData;
private String[] membersData;
//create objects from another classes for using them in the ActionListener
private ListSearchBooks listBooks;
private ListSearchMembers listMembers;
private Books book;
private Members member;

//for checking the information from the text field
public boolean isBooksDataCorrect() {
    booksData = new String[2];
    booksData[0] = searchBooksTypes.getSelectedItem().toString();
    for (int i = 1; i < booksData.length; i++) {
        if (!booksKeyTextField.getText().equals("")) {
            if (searchBooksTypes.getSelectedItem().toString().equals("BookID")) {
                booksData[i] = booksKeyTextField.getText();
            }
            else
                booksData[i] = "'%" + booksKeyTextField.getText() + "%'";
        }
        else
            return false;
    }
    return true;
}

//for checking the information from the text field
public boolean isMembersDataCorrect() {
    membersData = new String[2];
    membersData[0] = searchMembersTypes.getSelectedItem().toString();
    for (int i = 1; i < membersData.length; i++) {
        if (!membersKeyTextField.getText().equals("")) {
            if (searchMembersTypes.getSelectedItem().toString().equals("MemberID")) {
                membersData[i] = membersKeyTextField.getText();
            }
            else
                membersData[i] = "'%" + membersKeyTextField.getText() + "%'";
        }
        else
            return false;
    }
    return true;
}

//constructor of searchBooksAndMembers
public SearchBooksAndMembers() {
    //for setting the title for the internal frame
    super("Search", false, true, false, true);
    //for setting the icon
    setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Find16.gif")));
    //for getting the graphical user interface components display area
    Container cp = getContentPane();

    //for setting the layout
    northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    //for setting the font
    title.setFont(new Font("Tahoma", Font.BOLD, 14));
    //for adding the label
    northPanel.add(title);
    //for adding the north panel to the container
    cp.add("North", northPanel);

    //for setting the layout
    center.setLayout(new BorderLayout());

    //for setting the layout
    centerBooksPanel.setLayout(new BorderLayout());
    //for setting the layout
    searchBooksPanel.setLayout(new GridLayout(2, 2, 1, 1));
    //for adding the label
    searchBooksPanel.add(searchBooksLabel);
    //for adding the JComboBos[]
    searchBooksPanel.add(searchBooksTypes = new JComboBox(booksTypes));
    //for adding the label
    searchBooksPanel.add(booksKey);
    //for adding the text field
    searchBooksPanel.add(booksKeyTextField);
    //for adding the internal panel to the panel
    centerBooksPanel.add("North", searchBooksPanel);

    //for setting the layout
    searchBooksButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    //for adding the button
    searchBooksButtonPanel.add(searchBooksButton);
    //for adding the internal panel to the center panel
    centerBooksPanel.add("South", searchBooksButtonPanel);
    //for setting the border
    centerBooksPanel.setBorder(BorderFactory.createTitledBorder("Search for a book:"));
    //for adding center panel to the center
    center.add("West", centerBooksPanel);

    //for setting the layout
    centerMembersPanel.setLayout(new BorderLayout());
    //for setting the layout
    searchMembersPanel.setLayout(new GridLayout(2, 2, 1, 1));
    //for adding the label
    searchMembersPanel.add(searchMembersLabel);
    //for adding the JComboBos[]
    searchMembersPanel.add(searchMembersTypes = new JComboBox(membersTypes));
    //for adding the label
    searchMembersPanel.add(membersKey);
    //for adding the text field
    searchMembersPanel.add(membersKeyTextField);
    //for adding the internal panel to the panel
    centerMembersPanel.add("North", searchMembersPanel);

    //for setting the layout
    searchMembersButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    //for adding the button
    searchMembersButtonPanel.add(searchMembersButton);
    //for adding the internal panel to the center panel
    centerMembersPanel.add("South", searchMembersButtonPanel);
    //for setting the border
    centerMembersPanel.setBorder(BorderFactory.createTitledBorder("Search for a member:"));
    //for adding center panel to the center
    center.add("East", centerMembersPanel);

    //for adding the center to the container
    cp.add("Center", center);

    /**
     *for setting the font to the lables & buttons
     */
    searchBooksLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
    searchBooksTypes.setFont(new Font("Tahoma", Font.BOLD, 11));
    booksKey.setFont(new Font("Tahoma", Font.BOLD, 11));
    booksKeyTextField.setFont(new Font("Tahoma", Font.PLAIN, 11));
    searchBooksButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    cancelButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    searchMembersLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
    searchMembersTypes.setFont(new Font("Tahoma", Font.BOLD, 11));
    membersKey.setFont(new Font("Tahoma", Font.BOLD, 11));
    membersKeyTextField.setFont(new Font("Tahoma", Font.PLAIN, 11));
    searchMembersButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    cancelButton.setFont(new Font("Tahoma", Font.BOLD, 11));

    //for setting the layout
    southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    //for adding the button
    southPanel.add(cancelButton);
    //for setting the border
    southPanel.setBorder(BorderFactory.createEtchedBorder());
    //for adding the south panel to the container
    cp.add("South", southPanel);

    /***********************************************************************
     * for adding the action listener to the button,first the text will be *
     * taken from the JTextField and passing them to listSearchBooks object*
     ***********************************************************************/
    searchBooksButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            //for checking if there is a missing information
            if (isBooksDataCorrect() == true) {
                book = new Books();
                String bookQuery = "SELECT BookID, Subject, Title, Author,     Publisher," +
                        "Copyright, Edition, Pages,   NumberOfBooks,ISBN,Library,Availble,ShelfNo FROM Books" +
                        " WHERE " + booksData[0] + " LIKE " + booksData[1];
                book.connection(bookQuery);
                int bookID = book.getBookID();
                if (bookID != 0) {
                    listBooks = new ListSearchBooks(bookQuery);
                    getParent().add(listBooks);
                    try {
                        listBooks.setSelected(true);
                    }
                    catch (java.beans.PropertyVetoException e) {
                    }
                    dispose();
                }
                else {
                    JOptionPane.showMessageDialog(null, "No Match(es)", "Error", JOptionPane.ERROR_MESSAGE);
                    booksKeyTextField.setText(null);
                }
            }
            else
                JOptionPane.showMessageDialog(null, "Please enter a keyword", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    });
    /***********************************************************************
     * for adding the action listener to the button,first the text will be *
     * taken from the JTextField and passing them to listSearchBooks object*
     ***********************************************************************/
    searchMembersButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            if (isMembersDataCorrect()) {
                member = new Members();
                String memberQuery = "SELECT MemberID, ID, Name, EMail, Major, Expired" +
                        " FROM Members WHERE " + membersData[0] + " LIKE " + membersData[1];
                member.connection(memberQuery);
                int memberID = member.getMemberID();
                if (memberID != 0) {
                    listMembers = new ListSearchMembers(memberQuery);
                    getParent().add(listMembers);
                    try {
                        listMembers.setSelected(true);
                    }
                    catch (java.beans.PropertyVetoException e) {
                    }
                    dispose();
                }
                else {
                    JOptionPane.showMessageDialog(null, "No Match(es)", "Error", JOptionPane.ERROR_MESSAGE);
                    membersKeyTextField.setText(null);
                }
            }
            else
                JOptionPane.showMessageDialog(null, "Please enter a keyword", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    });
    //for adding the action listener for the button to dispose the frame
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            dispose();
        }
    });
    //for setting the visible to true
    setVisible(true);
    //show the internal frame
    pack();
}
}

这是ListBooks.Java的代码

import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.SQLException;

public class ListSearchBooks extends JInternalFrame {
/***************************************************************************
 ***      declaration of the private variables used in the program       ***
 ***************************************************************************/

//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating the label
private JLabel label = new JLabel("List of Searched Books");
//for creating the button
private JButton printButton;
//for creating the table
private JTable table;
//for creating the TableColumn
private TableColumn column = null;
//for creating the JScrollPane
private JScrollPane scrollPane;

//for creating an object for the ResultSetTableModel class
private ResultSetTableModel tableModel;

//constructor of listSearchBooks
public ListSearchBooks(String query) {
    //for setting the title for the internal frame
    super("Searched Books", false, true, false, true);
    //for setting the icon
    setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/List16.gif")));
    //for getting the graphical user interface components display area
    Container cp = getContentPane();

    /***********************************************************************
     *for setting the required information for the ResultSetTableModel class*
     ************************************************************************/
    final String JDBC_DRIVER ="net.ucanaccess.jdbc.UcanaccessDriver";
    final String DATABASE_URL = "jdbc:ucanaccess://JLibrary.mdb";

    final String DEFAULT_QUERY = query;

    //for bassing the required information to the ResultSetTableModel object
    try {
        tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, DEFAULT_QUERY);
        //for setting the Query
        try {
            tableModel.setQuery(DEFAULT_QUERY);
        }
        catch (SQLException sqlException) {
        }
    }
    catch (ClassNotFoundException classNotFound) {
    }
    catch (SQLException sqlException) {
    }

    //for setting the table with the information
    table = new JTable(tableModel);
    //for setting the size for the table
    table.setPreferredScrollableViewportSize(new Dimension(990, 200));
    //for setting the font
    table.setFont(new Font("Tahoma", Font.PLAIN, 12));
    //for setting the scrollpane to the table
    scrollPane = new JScrollPane(table);

    //for setting the size for the table columns
    for (int i = 0; i < 13; i++) {
        column = table.getColumnModel().getColumn(i);
        if (i == 0) //BookID
            column.setPreferredWidth(20);
        if (i == 1) //Subject
            column.setPreferredWidth(100);
        if (i == 2) //Title
            column.setPreferredWidth(150);
        if (i == 3) //Auther
            column.setPreferredWidth(50);
        if (i == 4) //Publisher
            column.setPreferredWidth(70);
        if (i == 5) //Copyright
            column.setPreferredWidth(40);
        if (i == 6) //Edition
            column.setPreferredWidth(40);
        if (i == 7) //Pages
            column.setPreferredWidth(40);
        if (i == 8) //NumberOfBooks
            column.setPreferredWidth(80);
        if (i == 9) //ISBN
            column.setPreferredWidth(70);
        if (i == 10) //Library
            column.setPreferredWidth(30);
        if (i == 11) //Availble
            column.setPreferredWidth(30);
                    if (i == 12) //ShelfNo
            column.setPreferredWidth(30);
    }
    //for setting the font to the label
    label.setFont(new Font("Tahoma", Font.BOLD, 14));
    //for setting the layout to the panel
    northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    //for adding the label to the panel
    northPanel.add(label);
    //for adding the panel to the container
    cp.add("North", northPanel);

    //for setting the layout to the panel
    centerPanel.setLayout(new BorderLayout());
    //for creating an image for the button
    ImageIcon printIcon = new ImageIcon(ClassLoader.getSystemResource("images/Print16.gif"));
    //for adding the button to the panel
    printButton = new JButton("print the books", printIcon);
    //for setting the tip text
    printButton.setToolTipText("Print");
    //for setting the font to the button
    printButton.setFont(new Font("Tahoma", Font.PLAIN, 12));
    //for adding the button to the panel
    centerPanel.add(printButton, BorderLayout.NORTH);
    //for adding the scrollpane to the panel
    centerPanel.add(scrollPane, BorderLayout.CENTER);
    //for setting the border to the panel
    centerPanel.setBorder(BorderFactory.createTitledBorder("Books:"));
    //for adding the panel to the container
    cp.add("Center", centerPanel);

    //for adding the actionListener to the button
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            Thread runner = new Thread() {
                public void run() {
                    try {
                        PrinterJob prnJob = PrinterJob.getPrinterJob();
                        prnJob.setPrintable(new PrintingBooks(DEFAULT_QUERY));
                        if (!prnJob.printDialog())
                            return;
                        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        prnJob.print();
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    }
                    catch (PrinterException ex) {
                        System.out.println("Printing error: " + ex.toString());
                    }
                }
            };
            runner.start();
        }
    });
    //for setting the visible to true
    setVisible(true);
    //to show the frame
    pack();
}
}

为什么问题仅适用于BookID?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

正如问题评论中所述,您的空catch块正在吞噬异常并阻止您查看问题,即:

代码块

if (!booksKeyTextField.getText().equals("")) {
    if (searchBooksTypes.getSelectedItem().toString().equals("BookID")) {
        booksData[i] = booksKeyTextField.getText();
    }
    else
        booksData[i] = "'%" + booksKeyTextField.getText() + "%'";
}
对于任何类型的搜索

将最终导致类似以下的SQL查询,但 BookID

SELECT BookID, Subject, ... FROM Books WHERE Subject LIKE '%waffles%'

但是在搜索BookID时,您的代码将尝试执行

SELECT BookID, Subject, ... FROM Books WHERE BookID LIKE 1234

会导致UCanAccess抛出异常

  

net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.6操作中不兼容的数据类型

您需要修复代码以为BookID搜索生成有效的SQL查询。该查询很可能使用相等(=)比较而不是LIKE

在您熟悉它的同时,您还应该使用PreparedStatement参数化查询来防止SQL注入问题,例如尝试搜索名为&#34的作者; O&#39;洛基&#34;