We just need the save button to save to a sql database. This is done other than that. it is saying, "Error: No suitable driver found for jdbc:derby:charityDB;create=true"
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; // Event listener
import java.text.NumberFormat;
import javax.swing.*; // GUI building
import java.io.*;
import java.sql.*; // JDSB
import java.util.logging.Level;
import java.util.logging.Logger;
public class FundraiserGUI extends JFrame {
// Gui dimension
private JPanel charityPanel;
private final int WINDOW_HEIGHT = 525;
private final int WINDOW_WIDTH = 800;
// declare Labels
private JLabel donorLastName; // Last name of donor
private JLabel donorFirstName; //First name of donor
private JLabel donorCharity; // Charity to be donated too
private JLabel charityDon; //Donation amount
// declare TextFields for user input
private JTextField donorLastNameTextField;
private JTextField donorFirstNameTextField;
private JTextField donorCharityTextField;
private JTextField charityDonTextField;
// declare Buttons & TextArea
private JButton saveButton;
private JButton exitButton;
private JButton resetButton;
private JTextArea donorList;
private JPanel buttonPanel;
private DataOutputStream ostream;
private DataInputStream istream;
//Constructor
public FundraiserGUI() { //set title
super("Fundraiser Donor List");
//set window size
setSize(WINDOW_HEIGHT, WINDOW_WIDTH);
//this is for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes window
this.buildPanel();
setVisible(true); // this will enable the gui to be seen
}
private void buildPanel() {
// below are the fields requesting entry & text entry fields for end
// user input
donorLastName = new JLabel("Enter last name: ");
donorFirstName = new JLabel("Enter first name: ");
donorCharity = new JLabel("Name of charity: ");
charityDon = new JLabel("Charity amount: ");
donorLastNameTextField = new JTextField(25);
donorFirstNameTextField = new JTextField(15);
donorCharityTextField = new JTextField(75);
charityDonTextField = new JTextField(10);
// below are the save & exit buttons
saveButton = new JButton("Save");
exitButton = new JButton("Exit");
resetButton = new JButton("Reset"); //clears all user input
// below is the TextArea for the pledger list
donorList = new JTextArea(25, 75);
// required action listeners for button functionality
saveButton.addActionListener(new saveButtonListener());
exitButton.addActionListener(new exitButtonListener());
resetButton.addActionListener(new resetButtonListener());
// panel creation
charityPanel = new JPanel();
buttonPanel = new JPanel();
// add everything to new charityPanel in succession
charityPanel.setLayout(new GridLayout(4, 2));
charityPanel.add(donorLastName);
charityPanel.add(donorLastNameTextField);
charityPanel.add(donorFirstName);
charityPanel.add(donorFirstNameTextField);
charityPanel.add(donorCharity);
charityPanel.add(donorCharityTextField);
charityPanel.add(charityDon);
charityPanel.add(charityDonTextField);
buttonPanel.setLayout(new GridLayout(1, 3));
buttonPanel.add(saveButton);
buttonPanel.add(exitButton);
buttonPanel.add(resetButton);
//Add layout manager to JFrame
this.setLayout(new BorderLayout());
//Location of the panel
this.add(charityPanel, BorderLayout.NORTH);
this.add(donorList, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
// establish hidden class for save button Listener class listed above
// in the panel build
private class saveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// create a separate database file
/*
try {
//define dataFile
// declare strings for database
String donorLastName;
String donorFirstName;
String donorCharity;
String charityDon; //
// required 'get' methods for Strings
donorLastName = donorLastNameTextField.getText();
donorFirstName = donorFirstNameTextField.getText();
donorCharity = donorCharityTextField.getText();
charityDon = charityDonTextField.getText();
// required conversion of pledge amount from text to integers
//try {
double donation = Double.parseDouble(charityDon);
donorList.append(donorLastName + " " + donorFirstName + " " + donorCharity + " " + charityDon + "\n");
// Write information to file
// Create the file, open for append
/* ostream = new DataOutputStream(new FileOutputStream("Charity.txt", true));
ostream.writeUTF(donorLastName);
ostream.writeUTF(donorFirstName);
ostream.writeUTF(donorCharity);
ostream.writeDouble(donation);
ostream.close();
} catch (IOException ex) {
System.err.println("File not opened");
System.exit(1);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "There was an error in the donation box.");
}
//input datastream
//We couldnt get this code to work corrctly
/* istream = new DataInputStream (new FileInputStream ("Charity.txt", true));
istream.writeUTF(donorLastName);
istream.writeUTF(donorFirstName);
istream.writeUTF(donorCharity);
istream.writeDouble(donation);
istream.close();
*/
}
}
// establish hidden class for exit button Listener class listed above
// in the panel build
private class exitButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
private class resetButtonListener implements ActionListener {
// we cannot see the reset button in the GUI
@Override
public void actionPerformed(ActionEvent e) {
//resets form
donorLastNameTextField.setText("");
donorFirstNameTextField.setText("");
donorCharityTextField.setText("");
charityDonTextField.setText("");
}
}
public static void main(String[] args) {
FundraiserGUI fundraiserGUI;
fundraiserGUI = new FundraiserGUI();
createDatabase();
}
// this is specific for JDB
private static void createDatabase(){
final String DB_URL =
"jdbc:derby:charityDB;create=true";
// Create a connection to the database.
try
{
//create the connection to the database
Connection conn = DriverManager.getConnection (DB_URL);
// Create a Statement object.
Statement stmt = conn.createStatement();
System.out.println("Creating the donation table...");
stmt.execute("CREATE TABLE Donation (" +
"Last Name CHAR(25), " +
"First Name CHAR (25), " +
"Charity Name CHAR (75)," +
"Donation Amount ( DOUBLE)");
//Close the resources.
stmt.close();
conn.close();
System.out.println("Complete");
}
catch (Exception ex)
{
System.out.println ("Error: "+ex.getMessage());
}
}
}