String format ( or similar function ) or PreparedStatement - java

Is there a difference and what is better practice to use: String.format() and manually insert values or PreparedStatement and parse values to placeholders (what is more size of code)?

As a general rule, never ever use plain string formatting where PreparedStatement could be used. That latter has knowledge of your database SQL syntax. And will takes care of "shielding" every special characters much better than you.
Failure to follow that rule will result in high risks of SQL Injection in your code.
See Does the preparedStatement avoid SQL injection?

There is a fundamental difference: a PreparedStatement will make whatever it takes so that the values you feed to it (via the .set*() methods) come as "neutral" to the database (or, rather, the "JDBC engine").
Note that PreparedStatement is an interface. As such, when using a JDBC driver for this or that database engine, it will be able to act differently depending upon said engine.
Do not use String.format() for that. Its role is quite different! String.format() cannot prevent SQL injection attacks; PreparedStatement can, unless its implementor did a really, really bad job.

Related

Concatenation in prepared statement

I have written universal DAO layer for mySQL (it can save\get any class object that extends Entity to\from table using Reflection and ResultSetMetaData). My implementation of it has little concatenation in sql query. Is it waste all advantages of prepared statement or I just loose little perfomance to concat String and nothing more?
For example piece of code for entity deletion:
PreparedStatement prepStatement = con.prepareStatement("DELETE FROM "
+ tableName + " WHERE id = ?");
prepStatement.setLong(1, id);
The main benefit of PreparedStatements is when you have code that behaves in a similar way to this pseudocode:
PreparedStatement ps = con.prepareStatement("blabalblabla");
for (int i = 0; i < a gazillion times; i++) {
// Set parameters into ps
...
// execute already prepared statement
ps.execute();
}
That is, you prepare once and execute many times, each time with different sets of parameters. This allows the driver / database to perform potentially costly operations (such as parsing) only once and then reuse that work. Apart from that, using PreparedStatement may be interpreted as a hint to the driver that it should cache that statement resources or something because it is going to be used later, but I don't think it will have as much impact as the "prepare once execute many" approach.
Your use of concatenation to add the table names won't disable the optimizations that your JDBC driver does (if any). But anyway, if your code does more of "prepare once execute once" than it does "prepare once execute many", then PreparedStatement might only have a minor performance benefit.
Note that all of the above is highly database / driver dependent. For example, Oracle performs a lot better if you use PreparedStatements in the way I have described as "prepare once execute many". And as a last advice, don't forget that you should avoid concatenating parameter values unless you have no other option, for both performance AND SECURITY reasons.
It's recommended to use the prepared statements for the DB performance improvement.
In theory the DB drivers cache the prepared statements (you might require to enable the caching on connection object).
I would assume that concatenation is not as critical.
Keep in mind that tableName might be case sensitive in the driver cache.
I would review your DB driver features, and you should be able to debug the driver, and monitor the database to see how your statements are handled/executed.
The variable tableName in your example may introduce vulnerability for SQL injection but it may be alternative ways to protect against this. For instance,
Map<String,String> myTables; // key and value are the same.
tableName = myTables.get(tableName); // safe known value or null.
Generally, it is better just to use prepared statements consistently to stay away out of trouble. However sometimes building query (most often where query) "on the fly" can save many lines otherwise close to duplicate code so it is difficult to say "never do this".

Should I use Prepared Statement for inserting one row?

I just found that there are places in our code that use Prepared Statement even though we always deal with inserting one row to the table.
I'm wondering if using Prepared Statement when only inserting one row has some overhead that worth modifying this code to use Statement.
When you use PreparedStement, not just your query execution is faster there are other advantages too.
You queries execute fast as PreparedStatement results into the query being precompiled on the database and reused.
PreparedStatement, your queries are dynamic. Meaning, you define the query only once, and reuse the same again with different parameters. String concatenation also achieves it but its crude way doing this. Quoting this link
The important thing to remember is to never construct SQL statements
using string concatenation of unchecked input values. Creating of
dynamic queries via the java.sql.Statement class leads to SQL
Injection.
When you use PreparedStatement you prevent the SQL injection attacks. In Prepared statement, you do not use string concatenation for adding the runtime parameters but instead set the parameter explicitly in the compiled query and the parameters passed are escaped automatically by JDBC Driver for PreparedStatement.
On the security side, Prepared Statements are used especially to prevent SQL Injection attacks. About efficiency, it very much depends on the nature of the statement you are dealing with. You may also find interesting this other answer:
Why PreparedStatement is preferable over Statement
A PreparedStatement is prefrable to a simple Statement as it offes your some security against SQL injection.
In a PreparedStatement every parameter is checked for its type and automatically escaped. This means that inserting String with an ' is save with PreparedStatement whereas you have to escape special characters yourself when not using a PreparedStatement.
Also you cannot insert some String where a Number is expected.
If even your code inserts only one row it may be called many times in which case PreparedStatement is supposed to be faster. As for Statement it also needs to be compiled before execution so it's hardly be any faster

Method to unquote SQL string

I have to process some parameter that is passed in as a quoted SQL string and then quote it again. I am wondering is there any utility in Hibernate or JDBC or some common Java library that can do the unquoting bit?
(Otherwise I guess I can do str.replace("''","'"). Is this correct?)
Edit:
Just some background. I am trying to add functionality to Hibernate. The Hibernate class I am overriding receives its argument as quoted strings. I have no access to the original argument submitted for the bind variable (without rewrite a number of Hibernate classes).
Just don't quote the String object.
there is an issue with your design, you should not be receiving it already quoted, if under any circumstance you need it quoted (avoid that), do it before you are going to use it.
for the JDBC side, you can always do the following, so you don't need SQL quoted strings:
String query ="select myColumn from MyTable where fullname = ?";
PreparedStatement stm = c.prepareStatement(query);
stm.setString(1, "Garis M. Suero");
It's a very good practice to design your software in a simple way in order to mitigate future confusions and errors when developers are coding your software.
For example, if you are receiving this from a third party software you can make an object that will take the string and convert it to the correct value, and maybe have two methods, one 'getValue()and another:getQuotedValue()`

JDBC: How to deal with wildcards in Java PreparedStatement

For example if I have:
connection.prepareStatement("SELECT * FROM users
WHERE status=? AND otherAttribute=?");
Where in some cases I might want a specific status and in others I want all statuses. How can I basically do something like:
preparedStatement.setInt(1, mySpecificRequestedStatus);
and in other cases:
preparedStatement.setInt(1, allStatuses); // wildcard
So that I don't have to have multiple PreparedStatements, especially for more complex WHERE clauses in the SELECT statements where the permutations can be higher.
You can do it like this:
connection.prepareStatement("SELECT * FROM users
WHERE (status=? OR ?=1) AND otherAttribute=?");
The second ? is for the "all statuses" attribute: set it to 1 if you want to ignore the status; set it to zero if you would like the specific status to be considered.
You don't have a choice, you have to pick one or the other as I originally suspected and asked.
You can either:
Create a complex prepared statement with if clauses
Create a complex SQL statement which doesn't have the best performance
There really isn't a good solution to this. This is why ORM's exist, to abstract this out. The problem is that ORM introduce other issues/complexities in exchange for simplifying other things.
Stephane Grenier,
Why not create a function or procedure in database and call function or procedure from java? This would solve many hassles, would keep your java code neat and of course no hard coding of sql queries.
Regards

How to determine database type for a given JDBC connection?

I need to handle resultsets returning stored procedures/functions for three databases (Oracle, sybase, MS-Server). The procedures/functions are generally the same but the call is a little different in Oracle.
statement.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
...
statement.execute();
ResultSet rs = (ResultSet)statement.getObject(1);
JDBC doesn't provide a generic way to handle this, so I'll need to distinguish the different types of DBs in my code. I'm given the connection but don't know the best way to determine if the DB is oracle. I can use the driver name but would rather find a cleaner way.
I suspect you would want to use the DatabaseMetaData class. Most likely DatabaseMetaData.getDatabaseProductName would be sufficient, though you may also want to use the getDatabaseProductVersion method if you have code that depends on the particular version of the particular database you're working with.
You can use org.apache.ddlutils, class Platformutils:
databaseName = new PlatformUtils().determineDatabaseType(dataSource)

Categories