Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

I am stuck

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic