Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package no.uib.INFO233.v2017.eos005.oblig3;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class DataBaseConnection {
  9.    
  10.     /**
  11.      * allows you to querry the database for a single value.
  12.      *
  13.      * @param querry
  14.      *            SQL querry
  15.      * @return the value as a string
  16.      */
  17.  
  18.     public static String querryTable(String querry) {
  19.         Connection connection = null;
  20.         java.sql.Statement statement = null;
  21.         String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  22.         String url = "jdbc:mysql://wildboy.uib.no:3306/Khmendi?autoReconnect=true&useSSL=false";
  23.         String user = "Khmendi";
  24.         String pas = "sKU5(`>]4UvEW;7/";
  25.         String x = null;
  26.  
  27.         try {
  28.             Class.forName(JDBC_DRIVER);
  29.             connection = DriverManager.getConnection(url, user, pas);
  30.             statement = connection.createStatement();
  31.  
  32.             ResultSet set = statement.executeQuery(querry);
  33.  
  34.             while (set.next()) {
  35.                 x = set.getString(1);
  36.             }
  37.  
  38.         }
  39.  
  40.         catch (SQLException e) {
  41.             e.printStackTrace();
  42.         } catch (ClassNotFoundException e) {
  43.             e.printStackTrace();
  44.         }
  45.         return x;
  46.     }
  47.  
  48.     /**
  49.      * allows you to update the table
  50.      *
  51.      * @param update
  52.      *            String
  53.      */
  54.     public static void updateTable(String update) {
  55.         Connection connection = null;
  56.         java.sql.Statement statement = null;
  57.         String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  58.         String url = "jdbc:mysql://wildboy.uib.no:3306/Khmendi?autoReconnect=true&useSSL=false";
  59.         String user = "Khmendi";
  60.         String pas = "sKU5(`>]4UvEW;7/";
  61.  
  62.         try {
  63.             Class.forName(JDBC_DRIVER);
  64.             connection = DriverManager.getConnection(url, user, pas);
  65.             statement = connection.createStatement();
  66.  
  67.             statement.executeUpdate(update);
  68.         }
  69.  
  70.         catch (SQLException e) {
  71.             e.printStackTrace();
  72.         } catch (ClassNotFoundException e) {
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.    
  77.     /**
  78.      * For testing at home
  79.      */
  80.  
  81.     public static String querryTableLocal(String querry) {
  82.         Connection connection = null;
  83.         java.sql.Statement statement = null;
  84.         String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  85.         String url = "jdbc:mysql://localhost:3306/ranking?autoReconnect=true&useSSL=false";
  86.         String user = "root";
  87.         String pas = "";
  88.         String x = null;
  89.  
  90.         try {
  91.             Class.forName(JDBC_DRIVER);
  92.             connection = DriverManager.getConnection(url, user, pas);
  93.             statement = connection.createStatement();
  94.  
  95.             ResultSet set = statement.executeQuery(querry);
  96.  
  97.             while (set.next()) {
  98.                 x = set.getString(1);
  99.             }
  100.  
  101.         }
  102.  
  103.         catch (SQLException e) {
  104.             e.printStackTrace();
  105.         } catch (ClassNotFoundException e) {
  106.             e.printStackTrace();
  107.         }
  108.         return x;
  109.     }
  110.     /**
  111.     * for testing at home
  112.      */
  113.     public static void updateTableLocal(String update) {
  114.         Connection connection = null;
  115.         java.sql.Statement statement = null;
  116.         String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  117.         String url = "jdbc:mysql://localhost:3306/ranking?autoReconnect=true&useSSL=false";
  118.         String user = "root";
  119.         String pas = "";
  120.  
  121.         try {
  122.             Class.forName(JDBC_DRIVER);
  123.             connection = DriverManager.getConnection(url, user, pas);
  124.             statement = connection.createStatement();
  125.  
  126.             statement.executeUpdate(update);
  127.         }
  128.  
  129.         catch (SQLException e) {
  130.             e.printStackTrace();
  131.         } catch (ClassNotFoundException e) {
  132.             e.printStackTrace();
  133.         }
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement