Vous êtes sur la page 1sur 10

Shankersinh Vaghela Bapu Institute of Technology

Branch: - Information Technology Subject: - Advance Java Technology

Practical -2 1). Consider a bank table which consist attributes (id, acc_name,acc_type, balance,mobile). Insert data with help of Statement, PreparedStatment and CallableStatment. For CallableStatment, you need to create a procedure. Ans.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package practical.pkg2; import java.sql.*; import javax.swing.*; /** * * @author sonal */ public class newbank1 { public static void main(String args[]) { newbank nb = new newbank(); JOptionPane.showMessageDialog(null, "JDBC Programming showing Statement, PreparedStatment and CallableStatment"); int choice = -1;

090750116062

Soni Sonal G.

Page 20

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology do { choice = nb.getChoice(); if (choice != 0) { nb.getSelected(choice); } } while (choice != 0); System.exit(0); } } class newbank { Connection con = null; String url = "jdbc:mysql://localhost/mysql"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Subject: - Advance Java Technology

Statement st = null; PreparedStatement pst = null; CallableStatement cb = null;

public static int getChoice() { String choice; int ch;


090750116062 Soni Sonal G. Page 21

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology choice = JOptionPane.showInputDialog(null, "1. Statement_Insert_Demo\n" + "2. PreparedStatement_Insert_Demo\n" + "3. CallableStatement_Insert_Demo\n" + "0. Exit\n\n" + "Enter your choice"); ch = Integer.parseInt(choice); return ch; } public void getSelected(int choice) { if (choice == 1) { Statement_Insert_Demo(); } if (choice == 2) { PreparedStatement_Insert_Demo(); } if (choice == 3) { CallableStatement_Insert_Demo(); } if (choice == 0) { dynamicQuery(); } }
090750116062 Soni Sonal G. Page 22

Subject: - Advance Java Technology

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology public void Statement_Insert_Demo() { try { Class.forName(driver); con = DriverManager.getConnection(url, user, pass); String sql = "insert into bank(id,acc_name,acc_type,balance,mobile) VALUES ('3','roshani','saving','10000','9876')"; st = con.createStatement(); st.executeUpdate(sql); System.out.print("Record inserted "); } catch (Exception e) { e.printStackTrace(); } } public void PreparedStatement_Insert_Demo() { try { Class.forName(driver); con = DriverManager.getConnection(url, user, pass); String sql = "insert into bank values(?,?,?,?,?)"; PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1, 4); pst.setString(2, "sweety"); pst.setString(3, "current"); pst.setInt(4, 30000);
090750116062 Soni Sonal G. Page 23

Subject: - Advance Java Technology

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology pst.setInt(5, 998823654); pst.executeUpdate(); System.out.print("Record inserted"); } catch (Exception e) { e.printStackTrace(); } } public void CallableStatement_Insert_Demo() { try { Class.forName(driver); con = DriverManager.getConnection(url, user, pass); String sql = "{call insert_data(?,?,?,?,?)}"; CallableStatement cb = con.prepareCall(sql); cb.setInt(1, 6); cb.setString(2, "shehrana"); cb.setString(3, "saving"); cb.setInt(4, 9920000); cb.setInt(5, 897652456); cb.executeUpdate(); System.out.print("Record inserted"); } catch (Exception e) { e.printStackTrace(); }
090750116062 Soni Sonal G. Page 24

Subject: - Advance Java Technology

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology } private void dynamicQuery() { System.exit(0); } } Subject: - Advance Java Technology

Output:-

090750116062

Soni Sonal G.

Page 25

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology Subject: - Advance Java Technology

090750116062

Soni Sonal G.

Page 26

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology Subject: - Advance Java Technology

Procedure:CREATE DEFINER=root@localhost PROCEDURE Insert_data{ IN p_bank_id INT(11), IN p_acc_name VARCHAR(50), IN p_acc_type VARCHAR(20),
090750116062 Soni Sonal G. Page 27

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology IN p_acc_balance INT(15), IN P_mobile VARCHAR(15) ) BEGIN INSERT INTO bank ( bank_id, acc_name, acc_type, acc_balance, mobile ) VALUES ( p_bank_id , p_acc_name, p_acc_type , p_acc_balance, P_mobile ); END$$ ***/ Subject: - Advance Java Technology

090750116062

Soni Sonal G.

Page 28

Shankersinh Vaghela Bapu Institute of Technology


Branch: - Information Technology Subject: - Advance Java Technology

090750116062

Soni Sonal G.

Page 29

Vous aimerez peut-être aussi