Statement or Prepared Statement?

Hi,

Since the PreparedStatement is a Precompiled Statement , can i use it for normal SQL statements where i need to pass a parameter for a select statement like:-

Instead of this:-

"Select name from employee where empId= "+empId

Shall i use :-

PreparedStatement ps=con.prepareStatement( "Select name from employee where empId= ?")

Resultset rs=ps.executeQuery();

if(rs.next()){

rs.setString(1,empId);

ps.executeUpdate();

}

Which one is better here? Where i havn't used PreparedStatement repeated execution of statement here. Pls. do provide a solution...

Thanks,

--JavaCrazyLover--

[668 byte] By [AnanthJavaa] at [2007-11-27 9:22:53]
# 1
PreparedStatements prevents you from SQL injections. But the way you're using it is wrong. Please refer to the Sun's JDBC tutorial how to use them correctly: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
BalusCa at 2007-7-12 22:17:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
My doubt is since PreparedStatement is precompiled and does have all the advantages why to go for normal Statement interface which operates during the runtime and slow?.
AnanthJavaa at 2007-7-12 22:17:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> why to go for normal Statement interface which operates

> during the runtime and slow?.

Statement is not slow (unless you define what you mean by slow). They can be efficient in some situations. However, the difference in execution times using a PreparedStatement even in such cases is not very significant. So, it is advisable to always use PreparedStatement.

aniseeda at 2007-7-12 22:17:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...