ラジオ ボタンを選択して [実行] ボタンをクリックすると、検索結果で著者のタイトルと年の結果が返されるようにします。たとえば、著者をクリックして、データベースに保存されている著者の名前を入力した場合、著者、タイトル、および年を取得したいと考えています。問題は、ラジオ ボタンを実行ボックスに配線することにあります。前もって感謝します。
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();
}
}