Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6.  
  7. /**
  8. *
  9. * @author aic
  10. */
  11. // You need to import the java.sql package to use JDBC
  12. import java.sql.*;
  13.  
  14. // We import java.io to be able to read from the command line
  15.  
  16. import java.io.*;
  17. import java.util.Scanner;
  18. public class JDBCORACLE {
  19.  
  20. public static void main(String[] args) {
  21. try
  22. {
  23. Class.forName("oracle.jdbc.driver.OracleDriver"); //Load the Oracle JDBC driver
  24. String url="jdbc:oracle:thin:@fedora1.uscupstate.edu:1521:xe";
  25. Connection con = DriverManager.getConnection(url,"testa","t123456");//connect oracle
  26.  
  27. executeTransaction(con);
  28.  
  29. con.close();
  30. }
  31. catch (Exception e)
  32. {
  33. System.out.println(e);
  34. }
  35. }
  36. public static void executeTransaction(Connection con) {
  37. try {
  38. Scanner scan = new Scanner(System.in);
  39. //Switch to manual transaction mode by setting
  40. //autocommit to false. Note that this starts the first
  41. //manual transaction.
  42. con.setAutoCommit(false);
  43. Statement stmt = con.createStatement();
  44. System.out.println("Please enter the bank account number with which you wish to "
  45. + "transfer funds from");
  46. int account1 = scan.nextInt();
  47. System.out.println("Please enter the bank account number with which you wish to "
  48. + "transfer funds to");
  49. int account2 = scan.nextInt();
  50. System.out.println("Please enter the amount of funds you wish to transfer");
  51. int transferAmount = scan.nextInt();
  52. String returnAmount1 = "SELECT balance FROM bankaccount WHERE accnum='" + account1 +"';";
  53. String returnAmount2 = "SELECT balance FROM bankaccount WHERE accnum='" + account2 +"';";
  54. stmt.executeQuery(returnAmount1);
  55. stmt.executeQuery(returnAmount2);
  56. int finalAmount = Integer.valueOf(returnAmount1) - transferAmount;
  57. int finalAmount2 = Integer.valueOf(returnAmount2) + transferAmount;
  58. stmt.executeUpdate("insert into bank values('"+account1+"','"+ finalAmount + "')");
  59. stmt.executeUpdate("insert into bank values('"+account2+"','"+ finalAmount2 + "')");
  60. //con.rollback();
  61. con.commit(); //This commits the transaction and starts a new one.
  62. stmt.close(); //This turns off the transaction.
  63. System.out.println("Transaction succeeded. Both records were written to the database.");
  64. }
  65. catch (SQLException ex) {
  66. ex.printStackTrace();
  67. try {
  68. System.out.println("Transaction failed.");
  69. con.rollback();
  70. }
  71. catch (SQLException se) {
  72. se.printStackTrace();
  73. }
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement