The post (“JDBC Delete Record Using PreparedStatement”) will give an example, and show how to delete a record using PreparedStatement.
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 |
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 JdbcDeleteRecordExample2 { private final static String USERNAME = "deploy"; private final static String PASSWORD = "Ts78W$k"; public static void main(String[] args) { JdbcDeleteRecordExample2 instance = new JdbcDeleteRecordExample2(); instance.updateRecord(); } public void updateRecord() { Connection conn = null; PreparedStatement stmt = null; try{ // Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Open a connection (MySQL as example) conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytecbar", USERNAME, PASSWORD); // Execute a query final String sql = "DELETE FROM category where id = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, 1); stmt.executeUpdate(); } catch(SQLException sle){ // process exception here sle.printStackTrace(); } catch(Exception e){ // process exception here e.printStackTrace(); } finally{ release(conn, stmt, null); } } public 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
Really helpful!
Thankѕ to my father whօ stated too mе гegarding tɦіѕ website, this web site is actuallʏ
awesome.
Thankyou foг helping out, fantastiс info.
I just like the helpful information you supply on your articles.
I’ll bookmark your weblog and check once more right here frequently.
I’m relatively certain I will learn a lot of new stuff proper here!
Best of luck for the following!
First off I want to say wonderful blog! I had a quick question in which I’d like
to ask if you don’t mind. I was interested to know how you center yourself and
clear your mind before writing. I’ve had a hard time clearing my
mind in getting my ideas out there. I do take pleasure in writing but it just seems like the first 10 to 15 minutes are lost simply just trying to figure out how to begin. Any recommendations or hints?
Many thanks!
Hey theгe! This iѕ my first visit tо your blog!
We are a team of volunteers ɑnd starting a new project in a
community in tthe same niche. Yоur blog provided us valuable infօrmation tо work on.
Yߋu Һave dοne a wonderful job!