Here is the array of all the Java reserved keywords (taken from here and keep being updated from here):
String keywords[] = { "abstract", "assert", "boolean", "break", "byte", "case", "catch",
"char", "class", "const", "continue", "default", "do", "double", "else", "extends",
"false", "final", "finally", "float", "for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try",
"void", "volatile", "while"
};
You have to navigate to the library where is Hashtable class using the JAVA_HOME system variable.
System.getenv("JAVA_HOME");
The java.util.Hashtable (and others) is located at %JAVA_HOME%/jre/lib/rt.jar library.
You have to find a way to extract the package, find the required file, decompile it and read lane by lane (using f.e. Regex). I recommend you to start reading answers of this question.
Unfortunately, there is NO other way.
Answer from Nikolas on Stack OverflowVideos
Here is the array of all the Java reserved keywords (taken from here and keep being updated from here):
String keywords[] = { "abstract", "assert", "boolean", "break", "byte", "case", "catch",
"char", "class", "const", "continue", "default", "do", "double", "else", "extends",
"false", "final", "finally", "float", "for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try",
"void", "volatile", "while"
};
You have to navigate to the library where is Hashtable class using the JAVA_HOME system variable.
System.getenv("JAVA_HOME");
The java.util.Hashtable (and others) is located at %JAVA_HOME%/jre/lib/rt.jar library.
You have to find a way to extract the package, find the required file, decompile it and read lane by lane (using f.e. Regex). I recommend you to start reading answers of this question.
Unfortunately, there is NO other way.
A list of Java keywords can be found in the Java documentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
With a list like this, you can create a String array that you can search.
DO NOT build SQL queries using string concatenation - you should be using bind parameters.
Your query string should be:
query="select * from books where BookName LIKE ?";
and then you can do something like:
Class.forName( "oracle.jdbc.OracleDriver" ); // If you are using the Oracle driver.
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE",
"username",
"password"
);
final String query="select * from books where BookName LIKE ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString( 1, "%" + txt1.getText() + "%" );
ResultSet rs = ps.executeQuery();
// Loop through the result set.
// Close statement/connections
(you will need to handle exceptions, etc.)
and:
- You should not need to change the query to swap between MySQL and Oracle (just change the driver and connection string).
- You do not need to escape any single or double quotation marks in the input string.
- You are protected from SQL injection attacks.
- Oracle can cache the query with the bind parameter and does not have to re-parse / re-compile it when the bind parameter changes.
If you are going to write the query as a string then string literals are surrounded by single quotes (not double quotes) in SQL:
query="select * from books where BookName LIKE '%your_string%'";
and you need to make sure that any single quotes in your string are properly escaped (but just use a bind parameter instead).
problem solved with this..
query="select * from books where BookName LIKE '%" +txt1.getText()+"%'";
thanks everyone :)
From axis.apache.org
Basically, Pre-Sort the keywords and store it in an array and using Arrays.binarySearch on your keyword for the good'ol O(logn) complexity
import java.util.Arrays;
public class MainDemo {
static final String keywords[] = { "abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "extends", "false",
"final", "finally", "float", "for", "goto", "if", "implements",
"import", "instanceof", "int", "interface", "long", "native",
"new", "null", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "true",
"try", "void", "volatile", "while" };
public static boolean isJavaKeyword(String keyword) {
return (Arrays.binarySearch(keywords, keyword) >= 0);
}
//Main method
public static void main(String[] args) {
System.out.println(isJavaKeyword("void"));
}
}
Output:
True
Alternatively, as users @typeracer,@holger suggested in the comments,
you can use SourceVersion.isKeyword("void") which uses javax.lang.model.SourceVersion library and Hashset Data structure internally and keeps the list updated for you.
I'm surprised that no one suggested javax.lang.model.SourceVersion yet, because it's actually been around since Java 1.6.
If you need to check whether some string is a reserved keyword, you can just call:
SourceVersion.isKeyword(str)
And if you really need the full list of the reserved keywords, you can obtain it from the source code of that class:
private final static Set<String> keywords;
static {
Set<String> s = new HashSet<String>();
String [] kws = {
"abstract", "continue", "for", "new", "switch",
"assert", "default", "if", "package", "synchronized",
"boolean", "do", "goto", "private", "this",
"break", "double", "implements", "protected", "throw",
"byte", "else", "import", "public", "throws",
"case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try",
"char", "final", "interface", "static", "void",
"class", "finally", "long", "strictfp", "volatile",
"const", "float", "native", "super", "while",
// literals
"null", "true", "false"
};
for(String kw : kws)
s.add(kw);
keywords = Collections.unmodifiableSet(s);
}
Caution: the above source code is from Java 1.8, so don't just copy & paste from this post if you're using a different version of Java. In fact, it's probably not a good idea to copy it at all — they made the field private for good reason — you probably don't want to have to keep it up-to-date for every new Java release. But if you absolutely must have it, then copy it from the source code in your own JDK distro, keeping in mind that you might have to manually keep updating it later.