0

我希望当我选择一个单选按钮并单击执行按钮时,我希望我的搜索结果返回作者标题和年份的结果。例如,如果我单击作者并输入存储在数据库中的作者姓名,我想获取作者、标题和年份。问题在于将单选按钮连接到执行框。提前致谢。

   package data


         import java.awt.*;
         import java.awt.event.*;
         import javax.swing.*;
         import java.sql.*;
         import java.util.ArrayList;
         public class data extends JFrame implements ActionListener{

/**
 * @param args the command line arguments
 */
//Database Globals
Connection conn;
Statement stmt;
ResultSet result;

//Instance Variables
private Container contentPane;
private JLabel labelSearch;
private JTextField  tfSearch;
private JButton execButton;
private JRadioButton rbAuthorName, rbTitle, rbYear;  
private ButtonGroup searchChoices;
ArrayList<JRadioButton> radioButtonList = new ArrayList<JRadioButton>();

//Constructor
public data(){
    super("Database Search");
    connect();        
}    
//Creating the GUI 
public void buildGui(){

contentPane= getContentPane();
contentPane.setLayout(new FlowLayout());

    //set pane size
    setVisible(true);
    setSize(200,150);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Initializing objects
    labelSearch = new JLabel("Look Up");
    tfSearch = new JTextField(30);
    rbAuthorName = new JRadioButton("Author");
    rbTitle = new JRadioButton("Title");
    rbYear = new JRadioButton("Year");
    searchChoices = new ButtonGroup();
    execButton = new JButton("Execute");

    //Adding objects to the component pane
    contentPane.add(rbAuthorName);
    contentPane.add(rbTitle);
    contentPane.add(rbYear);
    contentPane.add(execButton);
    contentPane.add(labelSearch);
    contentPane.add(tfSearch);              
    radioButtonList.add(rbAuthorName);
    radioButtonList.add(rbTitle);
    radioButtonList.add(rbYear);
    execButton.addActionListener(this);
}

public void connect(){
     try
    {
       System.out.println("Connection to Driver..."); 
       Class.forName("com.mysql.jdbc.Driver");
       System.out.println("Connection successful");
       System.out.println("Connection to Database...");
       conn = DriverManager.getConnection("jdbc:mysql://localhost/tbl_books","root","");
       System.out.println("Connection successful");   

    }catch(ClassNotFoundException cfe)
    {
        System.out.println ("Connection error: "+cfe.getMessage());                    
    }        
    catch(SQLException sqle)
    {
        System.out.println ("Connection error: "+sqle.getMessage());                    
    }           
}

@Override public void actionPerformed(ActionEvent e){

        //Object src = e.getSource();
        if(e.getSource()==execButton){
            String buttonName ="";

        for(JRadioButton button: radioButtonList){
         if(button.isSelected()){
           buttonName = button.getName();
        }
            searchData(buttonName);

        }   

    }
}
public void searchData(String strData){
     strData= tfSearch.getText();   

    try
    {

        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        String sqlString = "SELECT tbl_authors.author, tbl_books.title, tbl_books.year " + "from tbl_authors, tbl_books " + "where tbl_authors.isbn = tbl_books.isbn";

        if(rbAuthorName.isSelected()){
            sqlString = sqlString + "author LIKE '%" + strData + "%'";
        }

        else if(rbTitle.isSelected()){
            sqlString = sqlString + "title LIKE '%" + strData + "%'";
        }

        else if(rbYear.isSelected()){
            sqlString = sqlString + "date LIKE '%" + strData + "%'";
        }

        result =stmt.executeQuery(sqlString);

        int test = 0;
        String store = "";
        String response = "";

        while(result.next()){
            test ++;
            store = store + "\n"+ test + "." +
            result.getString(1)+ "" +
            result.getString(2)+ "" +
            result.getString(3);
        }
        if(test !=0){
            result.absolute(test);
            //displayData();
            response = "Number of Records" + test + store;
            JOptionPane.showMessageDialog(null, response);
        }
        else{
            JOptionPane.showMessageDialog(null,"Could not find data");
        }

    }catch(Exception e){
        e.getMessage();           
       }
}
//public void displayData(){

//}

public static void main(String[] args) {
    // TODO code application logic here
     CS6153Assignment2 data1 = new CS6153Assignment2();
     data1.connect();
     data1.buildGui();


}

}

4

1 回答 1

0

我认为您想要做的是当您单击执行按钮时,它会查找当前选定的RadioButton. 最简单的方法是将所有按钮保存到集合中(ArrayList例如):

ArrayList<JRadioButton> radioButtonList = new ArrayList<JRadioButton>();
radioButtonList.add(rbAuthorName);
radioButtonList.add(rbTitle);
radioButtonList.add(rbtYear);

而不是在执行按钮的 ActionListener 中迭代该列表并查找选定的列表:

@Override
public void actionPerformed(ActionEvent e){
        if(e.getSource()==execButton){

          String buttonName="";

          for(JRadioButton button: radioButtonList){
             if(button.isSelected()){
               buttonName = button.getName;
          }

          searchData(buttonName);
        }
    }

之后,您只需searchData稍微修改方法strData即可获取参数的值。

于 2012-10-31T09:02:55.023 回答