Advertisement
Guest User

Untitled

a guest
May 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package nieminen_aj.database;
  2.  
  3. import java.sql.*;
  4.  
  5. import nieminen_aj.main.PizzApp;
  6.  
  7. public class WriteDB {
  8.     /**
  9.      * contains name of this class
  10.      */
  11.     private String id = "WriteDB.";
  12.     /**
  13.      * contains JDBC driver name for MySQLcreaty
  14.      */
  15.     private static String driverName = "com.mysql.jdbc.Driver";
  16.     /**
  17.      * contains database username
  18.      */
  19.     private static String login = "";
  20.     /**
  21.      * contains database password
  22.      */
  23.     private static String pass = "";
  24.     /**
  25.      * contains database name
  26.      */
  27.     private static String db = "";
  28.     /**
  29.      * contains address of database
  30.      */
  31.     private static String host = "mydb..fi";
  32.     /**
  33.      * contains url for jdbc to use
  34.      */
  35.     private static String url = "jdbc:mysql://"
  36.         + host
  37.         + "/"
  38.         + db
  39.         + "?user="
  40.         + login
  41.         + "&password="
  42.         + pass;
  43.  
  44.     /**
  45.      * Puts SQL quotes around string.
  46.      *
  47.      * @param value String that needs to be quoted.
  48.      * @return returns Quoted String
  49.      */
  50.     public static String sqlQuote(String value){
  51.         String quoteValue = "'" + value + "'";
  52.         return quoteValue;
  53.     }
  54.  
  55.     public void createQueryCustomer(String [] array) {
  56.         String sql = "INSERT INTO customers ("
  57.             + "number"
  58.             + ", name"
  59.             + ", address"
  60.             + ", date"
  61.             + ", phone"
  62.             + ") VALUES ("
  63.             + array[0] + ", "
  64.             + sqlQuote(array[1]) + ", "
  65.             + sqlQuote(array[2]) + ", "
  66.             + sqlQuote(array[3]) + ", "
  67.             + sqlQuote(array[4]) + ")";
  68.         this.writeQuery(sql);
  69.     }
  70.  
  71.     public void createQueryOrderRow(String orderId, String [][] array) {
  72.         int rows = array.length;
  73.         String sql = "INSERT INTO ordered_pizzas ("
  74.             //+ "id"
  75.             + "pizza"
  76.             + ", ingredients"
  77.             + ", size"
  78.             + ", prize"
  79.             + ", order_id"
  80.             + ") VALUES ";
  81.  
  82.         if (rows == 1) {
  83.             sql += "("
  84.                 //+ array[0][0] + ", "
  85.                 + array[0][1] + ", "
  86.                 + sqlQuote(array[0][2]) + ", "
  87.                 + sqlQuote(array[0][3]) + ", "
  88.                 + array[0][4] + ", "
  89.                 + orderId + ")";
  90.         } else if(rows > 1) {
  91.             for (int i = 0; i < rows; i++) {
  92.                 sql += "("
  93.                     //+ array[i][0] + ", "
  94.                     + array[i][1] + ", "
  95.                     + sqlQuote(array[i][2]) + ", "
  96.                     + sqlQuote(array[i][3]) + ", "
  97.                     + array[i][4] + ", "
  98.                     + orderId + ")";
  99.                 // line is not last, we add comma
  100.                 if (i + 1 != rows) {
  101.                     sql += ", ";
  102.                 }
  103.             }
  104.         }
  105.         this.writeQuery(sql);
  106.     }
  107.  
  108.     public void createQueryOrder(String orderId
  109.             , int customerNumber
  110.             , int deliveryType
  111.             , double prize
  112.             , String date
  113.             , String time) {
  114.         String sql = "INSERT INTO orders ("
  115.             + "id"
  116.             + ", ordered_by"
  117.             + ", delivery_type"
  118.             + ", prize"
  119.             + ", date"
  120.             + ", time"
  121.             + ") VALUES ("
  122.             + orderId + ", "
  123.             + customerNumber + ", "
  124.             + deliveryType + ", "
  125.             + prize + ", "
  126.             + sqlQuote(date) + ", "
  127.             + sqlQuote(time) + ")";
  128.         this.writeQuery(sql);
  129.     }
  130.    
  131.     /**
  132.      * wroteQuery method of WriteDB writes given information to the
  133.      * database
  134.      * @param table used table in String
  135.      * @param array written information in 2d array
  136.      * first row of array consists name of columns
  137.      * second row of array consists values
  138.      */
  139.     public void writeQuery(String sql) {
  140.         // name of the method
  141.         String id = "writeQuery()";
  142.         // debugging url
  143.         PizzApp.debug(this.id + id + url);
  144.  
  145.         try {
  146.  
  147.             PizzApp.debug(this.id + id + "Loading driver: " + driverName);
  148.             Class.forName(driverName);
  149.             PizzApp.debug(this.id + id + "Driver loaded ok");
  150.  
  151.             PizzApp.debug(this.id + id + "Connectign database: " + url);
  152.             Connection con = DriverManager.getConnection(url);
  153.             PizzApp.debug(this.id + id + "Connected ok");
  154.  
  155.             PizzApp.debug(this.id + id + "Making statement");
  156.             Statement stmt = con.createStatement();
  157.             PizzApp.debug(this.id + id + "Statement ok");
  158.  
  159.             PizzApp.debug(this.id + id + "Sending SQL: " + sql);
  160.  
  161.             int count = 0;
  162.  
  163.             try {
  164.                 count = stmt.executeUpdate(sql);
  165.             }catch(Exception error){
  166.                 String str = error.getMessage();
  167.  
  168.                 if (str.contains("Duplicate")){
  169.                     System.err.println("Already inserted");
  170.                 } else {
  171.  
  172.                     System.err.println(error.getMessage());
  173.                     error.printStackTrace();
  174.                     System.exit(1);
  175.  
  176.                 }
  177.             }
  178.  
  179.             PizzApp.debug(this.id + id + "Insertet rows: " + count);
  180.  
  181.         }catch(Exception error){
  182.  
  183.             System.err.println(error.getMessage());
  184.             error.printStackTrace();
  185.             System.exit(1);
  186.  
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement