I am pretty new to programming in Java and am having a little difficulty. I have searched around but could not get an exact answer for what I am trying to do.
Basically, I have created a 3-tier application that uses servlets running on a server to access a mySQL database using JDBC.
For the most part, my application runs fine. I am able to run my queries and display my results.
The problem is, I need to implement some logic based on my query, which is a String.
For example,
ex1_query: insert into parts values ('S5', 'P6', 'J7', 50);
ex2_query: insert into parts values ('S5', 'P6', 'J7', 400);
My queries are being submitted through an HTML form as single string. As you can see, the only difference between ex1_query and ex2_query is their last value (50 & 400).
The logic is, that if that last value is >=100, I need to implement some stuff…
If that value is <100, just run the query as specified.
My queries will always be entered in that format, but that last value may vary from 0 - 1000.
So my question is… how can I parse just that last int from the string? So I can determine whether I should or should not implement some specific logic?
I know it sounds pretty simple, but I am having a little trouble. Any help or guidance would be appreciated. Thanks
User Prepared Statement
String param1= "S5";
String param2= "p6";
String param3= "j7";
int param4= 50 0r 400 ;
public void someMethod(String param1,String param2,String param3,String param4){
if(param4 >= 100){
//your stuff
param4 = ; //your new value;
}
query = insert into parts values (?, ?, ?,?);
And Execute your Query
If you do not have any control about the query you might want to befriend java.util.regex and parse for something like:
"^\s*insert\s+into\s+([A-Za-z0-9]+)\s+values\s+\(\s*('\w+'\s*,\s*){3}([0-9]+)\)\s*$"
This is actually an old-style grep regex but a regex with the same function is possible for java.util.regex. It checks whether the string follows your pattern. submatch #1 should give you your table name. submatch #2-#4 should give you the values 1-3 and submatch #5 contains the number (as string of course) you're looking for. Also a nice way to reject queries you do not like.
But If you don't wanted to go with Preparedstatement
then here is one more option
String ex1_query = "insert into parts values ('S5', 'P6', 'J7', 50);";
String lastValue = ex1_query.substring( ex1_query.lastIndexOf(",")+1, ex1_query.lastIndexOf(")")).trim();
Integer lastNumber = null;
try {
lastNumber = Integer.parseInt(lastValue);
} catch (NumberFormatException e) {
// if last value is not int then handle here
e.printStackTrace();
}
if(lastNumber >=100){
}
In lastValue you can get 50/400 or any value and do your stuff
If your query will remain same except that integer
value, you can use substring
.
String query = "insert into parts values ('S5', 'P6', 'J7', 50);"
int value = Integer.parseInt(query.substring(43, query.length()-2));
Or you can use regular expression to accomplish this.
String query = "insert into parts values ('S5', 'P6', 'J7', 50);";
Pattern pattern = Pattern.compile("(?<=(.+,\\s?))(\\d+)(?=(\\);))");
int value = Integer.parseInt(pattern.matcher(query).group());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.