*
The moose likes JDBC and the fly likes I am stuck Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "I am stuck" Watch "I am stuck" New topic
Author

I am stuck

Ola Kesh
Ranch Hand

Joined: Dec 24, 2002
Posts: 40
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 ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: I am stuck
 
Similar Threads
help (getting started)
NullPointerException
Display information from a database in a custom made GUI
ResultSet is null
Can't insert Data