The post (“JDBC Insert Records Using PreparedStatement”) will give an example, and show how to add records using PreparedStatement. JDBC Prepared Statement is used to handle precompiled query. If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package net.tecbar.jdbc.examples; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcInsertRecordExample2 { private final static String USERNAME = "deploy"; private final static String PASSWORD = "Ts78W$k"; public static void main(String[] args) { Connection conn = null; PreparedStatement stmt = null; final String sql = "INSERT INTO category (id, name, description) VALUES (?,?,?)"; try{ // Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Open a connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytecbar", USERNAME, PASSWORD); // add one record stmt = conn.prepareStatement(sql); stmt.setInt(1, 1); stmt.setString(2, "book"); stmt.setString(3, "there are 50,000 volumes available"); stmt.executeUpdate(); // add next record stmt.setInt(1, 2); stmt.setString(2, "movies"); stmt.setString(3, "31,000 movies are available"); stmt.executeUpdate(); } catch(SQLException se){ // process exception here se.printStackTrace(); } catch(Exception e){ // process exception here e.printStackTrace(); } finally{ release(conn, stmt, null); } } public static void release(Connection conn, Statement stmt, ResultSet rs) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException ex) { // process exception here } } } |
Pingback: JDBC by Examples
Hey verу cool web site!! Guy .. Excsllent .. Superb ..
I’ll bookmark your website and take thee feeds additіonally…I am hzppy to find
so many helpful information right herе within the put up, we want work
out mοre straqtegiеs on this regard, thankѕ for sɦaring.
Hey I know this is offf topic but I was wondering if
you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking forr a plug-in like his for quitee some time and was hoping maybe you would have soome experience with something like this.
Please let me know if you run into anything.
I truly enjoy reading your blog and I look
forward to your new updates.
This post offers clear idea in support of the new visitors of blogging, that truly how to do blogging.
I thе effoгtѕ you have put in this, ɑppeciate it fօr aall the
greatt blog posts.
I’ve learn several just right stuff here. Definitely price bookmarking for revisiting.
I surprise how much effort you place to create this type of wonderful informative web site.
Howdy! Quick question that’s totally off topic. Do you know how
to make your site mobile friendly? My site looks weird when browsing from
my iphone 4. I’m trying to find a theme or plugin that might be
able to resolve this issue. If you have any recommendations, please
share. Cheers!
There’s certainly a great deal to know about this issue. I like all
the points you have made.