Guest User

h

a guest
Aug 28th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. package com.atos.java.jdbc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Scanner;
  10.  
  11. public class Demo_prepared {
  12.  
  13. private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
  14. private static final String USER_ID ="hr";
  15. private static final String PASSWORD ="hr";
  16.  
  17.  
  18. public static void main(String[] args) {
  19.  
  20.  
  21.  
  22. final String sql= "INSERT INTO bill_master (billno,customer,amount) VALUES(?,?,?)";
  23.  
  24. Connection conn = null;
  25. try
  26. {
  27. Class.forName("oracle.jdbc.OracleDriver");
  28. conn = DriverManager.getConnection(DB_URL, USER_ID, PASSWORD);
  29.  
  30. PreparedStatement pstmt = conn.prepareStatement(sql);
  31.  
  32.  
  33.  
  34. Scanner sc = new Scanner(System.in);
  35. System.out.println("enter billno");
  36. Integer a = sc.nextInt();
  37. System.out.println("enter customer name");
  38. String b = sc.next();
  39. System.out.println("enter amount");
  40. Integer c = sc.nextInt();
  41. pstmt.setInt(1, a);
  42. pstmt.setString(2, b);
  43. pstmt.setInt(3, c);
  44.  
  45. int x = pstmt.executeUpdate();
  46. conn.commit();
  47.  
  48. }
  49. catch (SQLException e)
  50. {
  51. e.printStackTrace();
  52. }
  53. catch(ClassNotFoundException e)
  54. {
  55. e.printStackTrace();
  56. }
  57. finally
  58. {
  59. try
  60. {
  61. conn.close();
  62. }
  63. catch (SQLException e)
  64. {
  65. e.printStackTrace();
  66. }
  67.  
  68.  
  69. }
  70.  
  71. }
  72.  
  73. }
Add Comment
Please, Sign In to add comment