Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 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=;";
  10.   //  Database credentials
  11.         Connection dbcon ;
  12.         Statement stmt;
  13.         ResultSet rs;
  14.         int cr_number;
  15.         int card_for_delete;
  16.         int cust_code=0;
  17.         System.out.println("Δώσε τον αριθμό της πιστωτικής κάρτας που θέλεις να διαγράψεις:");
  18.         Scanner input = new Scanner(System.in);
  19.         card_for_delete = input.nextInt();
  20.  
  21.                                   /* declare ODBC conectivity */
  22. // STEP 2: REGISTER JDBC DRIVER
  23.         try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  } //dynamically load the driver's class file into memory, which automatically registers it
  24.         catch(java.lang.ClassNotFoundException e)
  25.         {System.out.print("ClassNotFoundException: ");
  26.         System.out.println(e.getMessage());}
  27.             /* execute SQL statements */
  28.         try {
  29. //STEP 3: OPEN A CONNECTION
  30.                 dbcon = DriverManager.getConnection(url); //establish a connection using the DriverManager.getConnection() method
  31.             stmt = dbcon.createStatement(); /*Creating Statement Objectdefine the methods and properties that enable
  32.             you to send SQL or PL/SQL commands and receive data from your database.*/
  33.             rs = stmt.executeQuery("SELECT card_number, customer_code FROM credit_card");/*The SQL statements that read data from a database query,
  34.             return the data in a result set. --When you perform a query against the database you get back a ResultSet.
  35.             You can then traverse this ResultSet to read the result of the query.*/
  36.  
  37. //STEP 5: Extract data from result set
  38.             while (rs.next()) {  //the //apo to select from where pigene apo katw (diatreksei ton pinaka)
  39.                 cust_code = rs.getInt("customer_code");
  40.             }
  41.             rs.close();
  42. //Retrieve by column name
  43.             String sql1 = "DELETE FROM payment WHERE customer_code="+cust_code;
  44.             stmt.executeUpdate(sql1);
  45.             String sql2 = "DELETE FROM purchase WHERE card_number="+card_for_delete;
  46.             stmt.executeUpdate(sql2);
  47.             String sql3 = "DELETE FROM credit_card WHERE card_number="+card_for_delete;
  48.             stmt.executeUpdate(sql3);
  49.             stmt.close(); //Closing Statement Object
  50.             dbcon.close();
  51.             }
  52.         catch(SQLException e)
  53.         {
  54.         System.out.print("SQLException: ");
  55.         System.out.println(e.getMessage());}
  56.     }
  57. }
  58. /*
  59. *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.
  60.  
  61. *Register the JDBC driver: Requires that you initialize a driver so you can open a communications channel with the database.
  62.  
  63. *Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with a database server.
  64.  
  65. *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.
  66.  
  67. *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