Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package no.uib.INFO233.v2017.eos005.oblig3;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class DataBaseConnection {
- /**
- * allows you to querry the database for a single value.
- *
- * @param querry
- * SQL querry
- * @return the value as a string
- */
- public static String querryTable(String querry) {
- Connection connection = null;
- java.sql.Statement statement = null;
- String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://wildboy.uib.no:3306/Khmendi?autoReconnect=true&useSSL=false";
- String user = "Khmendi";
- String pas = "sKU5(`>]4UvEW;7/";
- String x = null;
- try {
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(url, user, pas);
- statement = connection.createStatement();
- ResultSet set = statement.executeQuery(querry);
- while (set.next()) {
- x = set.getString(1);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return x;
- }
- /**
- * allows you to update the table
- *
- * @param update
- * String
- */
- public static void updateTable(String update) {
- Connection connection = null;
- java.sql.Statement statement = null;
- String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://wildboy.uib.no:3306/Khmendi?autoReconnect=true&useSSL=false";
- String user = "Khmendi";
- String pas = "sKU5(`>]4UvEW;7/";
- try {
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(url, user, pas);
- statement = connection.createStatement();
- statement.executeUpdate(update);
- }
- catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * For testing at home
- */
- public static String querryTableLocal(String querry) {
- Connection connection = null;
- java.sql.Statement statement = null;
- String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/ranking?autoReconnect=true&useSSL=false";
- String user = "root";
- String pas = "";
- String x = null;
- try {
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(url, user, pas);
- statement = connection.createStatement();
- ResultSet set = statement.executeQuery(querry);
- while (set.next()) {
- x = set.getString(1);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return x;
- }
- /**
- * for testing at home
- */
- public static void updateTableLocal(String update) {
- Connection connection = null;
- java.sql.Statement statement = null;
- String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/ranking?autoReconnect=true&useSSL=false";
- String user = "root";
- String pas = "";
- try {
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(url, user, pas);
- statement = connection.createStatement();
- statement.executeUpdate(update);
- }
- catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement