Java JDBC PreparedStatement Example - HowToDoInJava
Java JDBC PreparedStatement Example - HowToDoInJava
statements such as queries or updates, the prepared statement takes the form of a template into which
certain constant values are substituted during each execution.
A typical template would look like this: “INSERT INTO EMPLOYEE (ID, NAME) VALUES (?, ?);”
Here values are set in runtime at placeholders represented by “?”.
A Statement will always proceed through the four steps above for each SQL query sent to the database. A
Prepared Statement pre-executes steps (1) – (3) in the execution process above. Thus, when creating a
Prepared Statement some pre-optimization is performed immediately. The effect is to lessen the load on
the database engine at execution time.
Automatic prevention of SQL injection attacks by builtin escaping of quotes and other special
characters. Note that this requires that you use any of the PreparedStatement setXxx() methods to set
the values and not use inline the values in the SQL string by string-concatenating.
Apart from above two main usage, prepared statements makes it easy to work with complex objects
like BLOBs and CLOBs.
If you have missed, in previous posts, we have learned about types of JDBC drivers and some basic
operations like making database connection using JDBC and then how to execute SELECT Query, and then
INSET Query example.
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password");
package com.howtodoinjava.jdbc.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, 87);
pstmt.setString(2, "Lokesh");
pstmt.setString(3, "Gupta");
pstmt.setInt(4, 5);
int affectedRows = pstmt.executeUpdate();
System.out.println(affectedRows + " row(s) affected !!");
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
pstmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Output:
1 row(s) affected !!
Happy Leaning !!
Share this:
Subscribe to Blog
Enter your email address to subscribe and receive notifications of new posts by email.
Join 3,816 other subscribers
Email Address
SUBSCRIBE
A family guy with fun loving nature. Love computers, programming and solving everyday problems.
Find me on Facebook and Twitter.
tanakah
July 26, 2015
Hie Lokesh?
Well I got this very helpful but then how do I use the prepsre statement using values eneterd from
keyboard by the user? Im making a mysql database that holds informstion about administrators
lecturers and students. And I want an admin to register a lecturer by inputing their data through
netbeans then updating automatically updating their table in the database.
Thank you in advance
Lokesh Gupta
July 27, 2015
Is there any problem in passing the values though some method parameters?? Probably I do
not understand your problem correctly.
Samir Badr
November 27, 2013
nice session
Tony
November 24, 2013
Hi Lokesh
Thanks for a nice walk through session on PreaparedStatement.But the suggestion, in which
scenario developer will go for Statement/PreapedStatement if you will include that will be very nice
bcoz now a days that is very common question.
Shawn Irwin
November 24, 2013
Hey Lokesh, I subscribed to your newsletter, and for some reason, I am getting repeated emails
specifically of this article, How to execute PreparedStatement using JDBC. I suspect that there is
some issue with your server or whatever it is you are using to send these articles out. I’m hoping to
remain subscribed, so hopefully you can find the problem . . . . I’m probably not the only person
having this issue. Thanks.
Lokesh Gupta
November 24, 2013
I am sorry for the trouble. I am using wordpress’s native email subscription which works
automatically and I never interfere in this thing. I will however try to find out root cause.
Microservices Tutorial