• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error Checking Problem  RSS feed

 
Donna Harrington
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I have an error problem that is driving me I cant seem to figure out what the problem is. Ok, when I run my application the first error checking statement works ( either stockId or custId) but after that if i put in other id's that are not in my database it still says " This title has been successfully rented" even though the id's i put in the textfields are not in database. I hope somebody can spot what the problem is as i've been trying to figure it out for 3 hrs and getting nowhere.
Thanx
Donna
Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import java.sql.*;
import java.util.Calendar;
public class titleRental extends JPanel implements ActionListener {
JPanel p1, custOpt, cards;
CardLayout cl;
JLabel newLabel;
static JButton searchR;
static JButton rent;
char a = 'a';
char r = 'r';
Calendar date=Calendar.getInstance();
JTextField custIdTF,stockIdTF;
String stockId,custId,rentalDay,availability;
String status="";
int dayOfYear;
String custCheck ="",stockCheck="";
//int stockNum=0;
boolean cCheck,check;
//boolean cCheck=false;boolean check=false;
public titleRental(CardLayout cl, JPanel p) //constructor for class title Rental
{
setSize(500,500);
this.cl = cl;
cards = p;
p1 = new JPanel();
p1.setLayout(new GridLayout(7,2));

JLabel stockIdentificationLabel = new JLabel("Stock Identification");
stockIdTF = new JTextField(8);
p1.add(stockIdentificationLabel);
p1.add(stockIdTF);

JLabel customerIdentificationLabel = new JLabel("Customer Identification");
custIdTF = new JTextField(8);
p1.add(customerIdentificationLabel);
p1.add(custIdTF);

searchR = new JButton("SEARCH");
searchR.setActionCommand("SEARCH");
searchR.addActionListener(this);
p1.add(searchR);

rent = new JButton("RENT");
rent.setActionCommand("RENT");
rent.addActionListener(this);
p1.add(rent);
connect();
add(p1);
}


public void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Error creating class: "+e.getMessage());
e.printStackTrace();
return;
}
}
public void clear(){
stockIdTF.setText("");
custIdTF.setText("");

}

public void custExists()
{
try
{
Connection conn = DriverManager.getConnection("jdbc dbc:acidmenuDatabase", "", "");
Statement stmt= conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT custId FROM customer WHERE custId = '"+custId+"';");

if(rs.next()){
custCheck=(rs.getString("custId"));
}

stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return;
}
}


public void stockExists()
{
try
{
Connection conn = DriverManager.getConnection("jdbc dbc:acidmenuDatabase", "", "");
Statement stmt= conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT stockId FROM StockTable WHERE stockId = '"+stockId+"';");


if(rs.next()){
stockCheck=(rs.getString("stockId"));
}
stmt.close();
conn.close();

}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return;
}
}

public void update()
{ try
{
Connection conn = DriverManager.getConnection("jdbc dbc:acidmenuDatabase", "", "");
PreparedStatement pstmt = conn.prepareStatement("UPDATE customer SET stockId='"+stockId+"',rentalDay='"+rentalDay+"' WHERE custId = '"+custId+ "';");
pstmt.clearParameters();
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return;
}
}
public void updateDetails()
{ try
{
Connection conn = DriverManager.getConnection("jdbc dbc:acidmenuDatabase", "", "");
PreparedStatement pstmt = conn.prepareStatement("UPDATE StockTable SET availability = 'r', rentalDay ='"+rentalDay+"' WHERE stockId = '"+stockId+"';");
pstmt.clearParameters();
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return;
}
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
cl = (CardLayout)(cards.getLayout());
cl.show(cards,command );
System.out.println(command);
custId =custIdTF.getText().trim();
stockId = stockIdTF.getText().trim();


if (command.equals("RENT"))
{

date.getTime();
dayOfYear=date.get(Calendar.DAY_OF_YEAR);
date.getTime();
rentalDay=Integer.toString(dayOfYear);
connect();
getAvailability();
stockExists();
custExists();
System.out.println(cCheck);
System.out.println(check);
//System.out.println(stockCheck);
/*if(stockCheck.equals(""))
check=true;
else if(custCheck.equals(""))
cCheck=true;
else {
check=false;
cCheck=false;
}*/
if(stockCheck.equals(""))
check=true;

else{
check=false;
}
if(custCheck.equals(""))
cCheck=true;

else{
cCheck=false;
}


if(custId.equals("")){
JOptionPane.showMessageDialog(this, "You must enter both customer Id and Stock Id", "Invalid Input",JOptionPane.ERROR_MESSAGE);
}

else if(stockId.equals("")){
JOptionPane.showMessageDialog(this, "You must enter both customer Id and Stock Id", "Invalid Input",JOptionPane.ERROR_MESSAGE);

}
else if(check){
JOptionPane.showMessageDialog(this, "This Stock Id does not exist!", "Invalid Input",JOptionPane.ERROR_MESSAGE);
//cCheck=false;
}
else if(cCheck){
JOptionPane.showMessageDialog(this, "This Customer Id does not exist!", "Invalid Input",JOptionPane.ERROR_MESSAGE);
//check=false;

}
else if( status.equals("r"))
{
clear();
JOptionPane.showConfirmDialog(this, "This Title is not available for rent", "ACID Systems", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE);

}
else
{
updateDetails();
update();
clear();
JOptionPane.showConfirmDialog( this," This Title has been successfully Rented ", "ACID Systems", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE );
cCheck=true;
check=true;
}
}

if(command.equals("SEARCH"))
{
stockId = stockIdTF.getText().trim();


connect();
clear();
JOptionPane.showConfirmDialog( this, "Searching the System", "ACID Systems", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE );
cl.show(cards,"Display Available Videos");
}
}


/*public static String getStockId(){
return stockId;
}*/


public void getAvailability()
{
try
{
Connection conn = DriverManager.getConnection("jdbc dbc:acidmenuDatabase", "", "");
//System.out.println(stockNum);
Statement stmt= conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT availability FROM StockTable WHERE stockId='"+stockId+"';");
if (rs.next() )
{
status=(rs.getString("availability"));
}

stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return;
}
};
}
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some friendly words of advice...you have to do a bit work to help us help you Your question is a bit vague, and honestly, not many people are going to look through all that code, especially since it's making database calls and we can't actually run your code as-is. Also, if you edit your post to use the code tags, it will keep your code aligned and make it much easier to read. (You can edit your previous post, and when you do, look at the list of buttons below the text-entry area and click the "code" button. Paste the first tag at the start of your code, and the second one at the end.)
If you can narrow the focus, ask one or more focused questions (one per thread please), and post smaller snippets of relevant code where you have the question, you are likely to get a lot more help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!