Advertisement
Guest User

d

a guest
Jan 14th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.io.*;
  2. import java.text.*;
  3. import java.util.*;
  4. import java.sql.*;
  5.  
  6. public class erotima3 {
  7.     public static void main(String args[]){
  8.         String url = "jdbc:sqlserver://altebaran.dmst.aueb.gr:1433;" +
  9.                            "databaseName=DB32;user=G532;password=49bs95959;";
  10.  
  11.   //  Database credentials
  12.  
  13.         Connection dbcon ;
  14.         Statement stmt;
  15.         ResultSet rs;
  16.  
  17.         int cr_number;
  18.         int card_for_delete;
  19.         int cust_code=0;
  20.  
  21.         System.out.println("Δώσε τον αριθμό της πιστωτικής κάρτας που θέλεις να διαγράψεις:");
  22.         Scanner input = new Scanner(System.in);
  23.         card_for_delete = input.nextInt();
  24.  
  25.                                   /* declare ODBC conectivity */
  26. // STEP 2: REGISTER JDBC DRIVER
  27.         try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  } //dynamically load the driver's class file into memory, which automatically registers it
  28.         catch(java.lang.ClassNotFoundException e)
  29.         {System.out.print("ClassNotFoundException: ");
  30.         System.out.println(e.getMessage());}
  31.             /* execute SQL statements */
  32.         try {
  33. //STEP 3: OPEN A CONNECTION
  34.                 dbcon = DriverManager.getConnection(url); //establish a connection using the DriverManager.getConnection() method
  35.             stmt = dbcon.createStatement(); /*Creating Statement Objectdefine the methods and properties that enable
  36.             you to send SQL or PL/SQL commands and receive data from your database.*/
  37.             rs = stmt.executeQuery("SELECT card_number, customer_code FROM credit_card");/*The SQL statements that read data from a database query,
  38.             return the data in a result set. --When you perform a query against the database you get back a ResultSet.
  39.             You can then traverse this ResultSet to read the result of the query.*/
  40.  
  41. //STEP 5: Extract data from result set
  42.             while (rs.next()) {  //the //apo to select from where pigene apo katw (diatreksei ton pinaka)
  43.                 cust_code = rs.getInt("customer_code");
  44.             }
  45.             rs.close();
  46.  
  47. //Retrieve by column name
  48.             String sql1 = "DELETE FROM payment WHERE customer_code="+cust_code;
  49.             stmt.executeUpdate(sql1);
  50.  
  51.             String sql2 = "DELETE FROM purchase WHERE card_number="+card_for_delete;
  52.             stmt.executeUpdate(sql2);
  53.  
  54.             String sql3 = "DELETE FROM credit_card WHERE card_number="+card_for_delete;
  55.             stmt.executeUpdate(sql3);
  56.  
  57.             stmt.close(); //Closing Statement Object
  58.  
  59.             dbcon.close();
  60.  
  61.             }
  62.         catch(SQLException e)
  63.         {
  64.         System.out.print("SQLException: ");
  65.         System.out.println(e.getMessage());}
  66.  
  67.  
  68.  
  69.     }
  70. }
  71. /*
  72. *Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.
  73.  
  74. *Register the JDBC driver: Requires that you initialize a driver so you can open a communications channel with the database.
  75.  
  76. *Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with a database server.
  77.  
  78. *Execute a query: Requires using an object of type Statement for building and submitting an SQL statement to delete records from a table. This Query makes use of the WHERE clause to delete conditional records.
  79.  
  80. *Clean up the environment: Requires explicitly closing all database resources versus relying on the JVM's garbage collection./*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement