Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

java - JDBC - Statement, PreparedStatement, CallableStatement and caching

I am wondering what are the differences and when to use Statement, PreparedStatement, and CallableStatement.

What is the best practice and typical scenario of using each of these?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Statement vs PreparedStatement

  1. Performance can be better with PreparedStatement but is database dependent.

  2. With PreparedStatement you avoid SQL injection. How does a PreparedStatement avoid or prevent SQL injection?

  3. Better type check with preparedStatement by setInt, setString where as statement you just keep appending to the main SQL.

Similar Post:

Difference between Statement and PreparedStatement

CallableStatement - Java answer to access StoredProcedures across all databases.

Similar post

CallableStatement vs Statement

With PreparedStatement and Callable you already have caching, also caching is a big topic in its own, you wouldn't like to do all of that instead look at ehcache

You should almost always prefer PreparedStatement over Statement

If you have to operate over StoredProcedure you have just one option CallableStatement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...