Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PreparedStatement, setString() and setInt()

843859Jul 18 2007 — edited Jul 20 2007
Hi,
I am using PreparedStatement in my JDBC program and using the setString() method to replace the placeholders with values dynamically.

PreparedStatement pstmt = new PreparedStatement( "INSERT INTO MYTAB VALUES ( ?, ?, ? )" );
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
//code that takes input from console for the values
String name = br.readLine();
String salary = br.readLine();
String dept = br.readLine();

pstmt.setString( 1, name );
pstmt.setString( 2, salary ); //I could use pstmt.setInt( 2, Integer.parseInt( salary ) );
pstmt.setString( 3, dept );

I have a table called MYTAB and it has three columns: name (varachar2), salary( integer ) and dept (varchar2).
Now I have seen that even if the data type of salary column is integer I can insert data into it using the setString() method. It is true for all other data types.

Now my question is if setString() is sufficient to insert value into a column and if the rest is taken care of, then what is the need to have individual methods for individual data types ( like setInt(), setFloat() etc).

One possible answer could be that a String is an object and hence it occupies more memory that what a primitive type does. Is there any other reason for providing setter methods for different data types?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2007
Added on Jul 18 2007
9 comments
1,047 views