File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes adding an auxiliary class to code accesing database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "adding an auxiliary class to code accesing database" Watch "adding an auxiliary class to code accesing database" New topic
Author

adding an auxiliary class to code accesing database

nelson normahomed
Greenhorn

Joined: Mar 10, 2006
Posts: 1
hie im having a difficult time trying to create and auxiliary class and at the same time access my Ms-acces databse. at the moment my prgram works it is a carpark simulation program. it sends the amount clicked on to the database but when i try and create an auxiliary class for the calculation of the change it is giving me a list of errors.

my code is

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.sql.*;

public class CarPark extends JFrame implements ActionListener {
JButton tenP, twentyP, fiftyP, onePound, twoPounds, less1, oneTwo, twoThree, threeFour, overFour;
JLabel messLabel = new JLabel("Amount to pay: ");
JTextField message = new JTextField(10);
int amount = 0; // payment in pence
int payment = 0;
DecimalFormat pounds = new DecimalFormat("�0.00");
Connection carPark;
Statement myStatement;

public static void main(String[] args) {
CarPark c = new CarPark();
c.setTitle("Car park payment simulator");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(500, 200);
c.setVisible(true);
}

CarPark() {
setLayout(new BorderLayout());
tenP = new JButton("10p"); tenP.addActionListener(this);
twentyP = new JButton("20p"); twentyP.addActionListener(this);
fiftyP = new JButton("50p"); fiftyP.addActionListener(this);
onePound = new JButton("�1"); onePound.addActionListener(this);
twoPounds = new JButton("�2"); twoPounds.addActionListener(this);
less1 = new JButton("Up to 1 hr"); less1.addActionListener(this);
oneTwo = new JButton("1 to 2 hr"); oneTwo.addActionListener(this);
twoThree = new JButton("2 to 3 hr"); twoThree.addActionListener(this);
threeFour = new JButton("3 to 4 hr"); threeFour.addActionListener(this);
overFour = new JButton("Over 4 hr"); overFour.addActionListener(this);


rightButtons();

JPanel leftSide = new JPanel();
leftSide.setLayout(new GridLayout(5, 1));
leftSide.add(tenP);
leftSide.add(twentyP);
leftSide.add(fiftyP);
leftSide.add(onePound);
leftSide.add(twoPounds);
add("West", leftSide);

JPanel rightSide = new JPanel();
rightSide.setLayout(new GridLayout(5, 1));
rightSide.add(less1);
rightSide.add(oneTwo);
rightSide.add(twoThree);
rightSide.add(threeFour);
rightSide.add(overFour);
add("East", rightSide);

JPanel middle = new JPanel();
middle.setLayout(new FlowLayout());
middle.add(messLabel);
message.setEditable(false);
middle.add(message);
add("Center", middle);

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL =
"jdbc dbc river={Microsoft Access Driver (*.mdb)};DBQ=CarPark.mdb;";
carPark = DriverManager.getConnection(sourceURL, "admin", "");
myStatement = carPark.createStatement();
}
// The following exceptions must be caught
catch (ClassNotFoundException cnfe) {
System.out.println(cnfe);
}
catch (SQLException sqle) {
System.out.println(sqle);
}
}



public void actionPerformed(ActionEvent e) {
if (e.getSource() == less1) {payment = amount = 100; leftButtons(); }
if (e.getSource() == oneTwo) {payment = amount = 200; leftButtons(); }
if (e.getSource() == twoThree) {payment = amount = 350; leftButtons(); }
if (e.getSource() == threeFour) {payment = amount = 500; leftButtons(); }
if (e.getSource() == overFour) {payment = amount = 600; leftButtons(); }
if (e.getSource() == tenP) amount -= 10;
if (e.getSource() == twentyP) amount -= 20;
if (e.getSource() == fiftyP) amount -= 50;
if (e.getSource() == onePound) amount -= 100;
if (e.getSource() == twoPounds) amount -= 200;

if (amount > 0) message.setText(pounds.format(amount / 100.0));
else {
message.setText("");
if (amount < 0) {
int change = -amount;
JOptionPane.showMessageDialog(this, "Your change is "
+ pounds.format(change / 100.0)
+ coins(change),
"Change", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Thank you",
"Exact amount", JOptionPane.INFORMATION_MESSAGE);
}

String writeString =
"INSERT INTO Stats(Amount)VALUES(" + (payment / 100.0) + ")";
try {
myStatement.executeUpdate(writeString);

}
catch (SQLException sqle) {
}

rightButtons();
}
}



// enable left buttons, disable right buttons
void leftButtons() {
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
less1.setEnabled(false);
oneTwo.setEnabled(false);
twoThree.setEnabled(false);
threeFour.setEnabled(false);
overFour.setEnabled(false);
}



// enable right buttons, disable left buttons
void rightButtons() {
tenP.setEnabled(false);
twentyP.setEnabled(false);
fiftyP.setEnabled(false);
onePound.setEnabled(false);
twoPounds.setEnabled(false);
less1.setEnabled(true);
oneTwo.setEnabled(true);
twoThree.setEnabled(true);
threeFour.setEnabled(true);
overFour.setEnabled(true);
}

String coins(int change) {
String answer = ":";
if (change >= 100) {
answer += "\nOne �1 coin";
change -= 100;
}
if (change >= 50) {
answer += "\nOne 50p coin";
change -= 50;
}
if (change >= 40) {
answer += "\nTwo 20p coins";
change -= 40;
}
if (change >= 20) {
answer += "\nOne 20p coin";
change -= 20;
}
if (change >= 10) {
answer += "\nOne 10p coin";
change -= 10;
}
return answer;
}
}


i cannot upload my database but if anyone could help could you email me or send me your address then i will be able to send u the database and u could see wat i mean. or if anyone can help me out in creating just an auxiliary class for the program. thank you my email is nelsao1@yahoo.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding an auxiliary class to code accesing database