Aight... I need to either use setInt or setString for the PreparedStatement class.
Is there anyway I can check whether a string is a number/int or is just a normal string?
In javascript, theres a function called isNaN(), where it tells a certain var is not a number.
What about java?
hmm. in c++ i guess u can use isdigit, isalnum, isalpha.
like:
string x;
if (!isalpha(x)) then
cout<< "its not a string"<
else
cout<<"it is a string"<
i think java is something like this too
Found another way to check. Basically using Number format exceptions to validate. Thanks though.
Now for the bigger headache.
I've been trying to add values into a vector, and retrieve them to form my PreparedStatements' setString, setInt methods. So far, so good i think... except that when i need to executeUpdate(), it keeps telling "java.sql.SQLException: No value specified for parameter 2"
Any case, here are the codes. If anyone has any clues to why this is happening, feel free to reply. Ignore the System.out.printlns, those are just for me to do my own "debugging."
public void processStrings(){
int size= valueStrings.size();
System.out.println("Size is "+size);
System.out.println("object at first position is"+valueStrings.elementAt(0));
System.out.println("object at 2nd position is"+valueStrings.elementAt(1));
// ListIterator list = valueStrings.listIterator();
for (int i = 0; i < size; i++){
int j = i+1;
String test = ""+valueStrings.elementAt(i);
System.out.println("value at "+valueStrings.elementAt(i));
System.out.println("value of i ="+i);
System.out.println("value of j ="+j);
try {
int testInt = Integer.parseInt(test);
try {
pstmt.setInt(i,testInt);
System.out.println("setInt worked= "+testInt);
} catch (SQLException ex) {
System.out.println("setInt didnt work");
ex.printStackTrace();
}
} catch (NumberFormatException ex) {
System.out.println("Is not a number "+test);
try {
pstmt.setString(j, ""+valueStrings.elementAt(i));
System.out.println("setString worked= ");
} catch (SQLException ex2) {
System.out.println("Some error here");
ex.printStackTrace();
}//end catch ex2
}//end catch ex
}// end while
}
public int pExecuteUpdate(){
int i= 0;
try {
i = pstmt.executeUpdate();
System.out.println("Wtf happened here? "+i);
} catch (SQLException ex) {
System.out.println("Something bad happened here" +i);
ex.printStackTrace();
}
return i;
}
OUTPUT Of the System.out.printlns
Size is 2
object at first position ispassword1
object at 2nd position is3
value at password1
value of i =0
value of j =1
Is not a number password1
setString worked=
value at 3
value of i =1
value of j =2
setInt worked= 3
Something bad happened here0