MySQL JDBC Insert Data Using PreparedStatement Interface - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

MySQL JDBC Insert Data Using PreparedStatement Interface

Share This




package com.t4b.jdbc.mysql.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestMain { static { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) { Connection con = null; PreparedStatement pstmt = null; try { con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library", "root", "pass123"); pstmt = con.prepareStatement("insert into books values (?, ?, ?)"); pstmt.setString(1, "Java"); pstmt.setString(2, "Raman"); pstmt.setString(3, "Willey"); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.