• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I am stuck

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day,
Please check out the codes below, seems am stock with my "if else" control.
This is the concept....
i want to asign a secret code for every database input, but where the "Name" exit in the database, it only re-asign the old code.
on test-running, i noticed that "if(myResult == d){...}" is always skipped when "myResult == d" and when "myResult != d". what is the problem?

public String createSceretCode(String myCode)
{

try
{
// Load the Driver(registers itself)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
}


catch(ClassNotFoundException cnfex)
{
JOptionPane.showMessageDialog(null,"The driver can not be loaded...",
"Driver Error!!!",JOptionPane.ERROR_MESSAGE);
}

try
{

// Connect to the database
con = DriverManager.getConnection("jdbc dbc:AccountDepartment","","");
System.out.println("Connected to database");

}

catch(SQLException sqlex)
{
JOptionPane.showMessageDialog(null,"Cannot connect to the database...",
"Connection Error!!!",JOptionPane.ERROR_MESSAGE);
}

try
{
stmt = con.createStatement();
String d = f1.getText();
System.out.println(d);
r = stmt.executeQuery("SELECT Name,SecretCode FROM Account WHERE Name = '" +d +"'");
System.out.println(d);
if(r.next())
myResult = r.getString("Name");


if(myResult == d)
{

System.out.println(d);
myCode = r.getString("SecretCode");
}

else
{
query = "SELECT * FROM code";
r = stmt.executeQuery(query);
if(r.next())

codee = Integer.parseInt(r.getString("CreateCode"));
System.out.println(codee);
System.out.println(query);
codee += 1;
myCode = "CTL-" +codee;
System.out.println(codee);
query = "UPDATE code SET CreateCode ='" +codee +"'";
stmt.executeUpdate(query);

}

stmt.close();
con.close();
}

catch(SQLException sqlex)
{
JOptionPane.showMessageDialog(null,"Cannot Create Secret Code because an Error has occurred.",
"Connection Error!!!",JOptionPane.ERROR_MESSAGE);
}

return myCode;

}

[ June 29, 2005: Message edited by: Bear Bibeault ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are forgetting the difference between "==" and "equals()".

Perhaps this will explain your problem:


Remember that if you use the relational operator == to compare two objects you are comparing object references,not the value of the object. If I did this:

I would get the output "B == C", because both B and C are references to the same object.
[ June 29, 2005: Message edited by: Paul Sturrock ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic