我想在选择单选按钮并单击执行按钮时,我希望我的搜索结果返回作者标题和年份的结果。例如,如果我点击作者并输入存储在数据库中的作者姓名,我想获得作者,头衔和年份。问题在于将单选按钮连接到执行框。提前致谢。
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());
}
}
@覆盖 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();
}
}
答案 0 :(得分:0)
我认为您要做的是当您点击执行按钮时,它会查找当前选定的RadioButton
。最简单的方法是将所有按钮保存到集合中(例如ArrayList
):
ArrayList<JRadioButton> radioButtonList = new ArrayList<JRadioButton>();
radioButtonList.add(rbAuthorName);
radioButtonList.add(rbTitle);
radioButtonList.add(rbtYear);
并且在执行Button的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
获取参数的值。