Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Created by dulciejackson on 13/12/2017.
  7.  */
  8. public class MainProject {
  9.  
  10.     /**
  11.      * Connects to the database using the url, username and password supplied
  12.      * @param user the username to be used
  13.      * @param password the password used to access the database
  14.      * @param database the url of the database to be accessed
  15.      * @return the Connection once the database has been connected to
  16.      */
  17.     public static Connection connectToDatabase(String user, String password, String database) {
  18.         System.out.println("-------- PostgreSQL " + "JDBC Connection Testing ------------");
  19.         try {
  20.             Class.forName("org.postgresql.Driver");
  21.         } catch (ClassNotFoundException e) {
  22.             System.out.println("Where is your PostgreSQL JDBC Driver? Include in your library path!");
  23.             e.printStackTrace();
  24.         }
  25.         System.out.println("PostgreSQL JDBC Driver Registered!");
  26.  
  27.         Connection connection = null;
  28.         try {
  29.             connection = DriverManager.getConnection(database, user, password);
  30.         } catch (SQLException e) {
  31.             System.out.println("Connection Failed! Check output console");
  32.             e.printStackTrace();
  33.         }
  34.         return connection;
  35.     }
  36.  
  37.     /**
  38.      * Performs a query on the database connection provided
  39.      * @param connection database connection to be used for the query
  40.      * @param query the query to be executed on the database
  41.      * @return the ResultSet returned from the query
  42.      */
  43.     public static ResultSet executeSelect(Connection connection, String query) {
  44.         Statement st = null;
  45.         try {
  46.             st = connection.createStatement();
  47.         } catch (SQLException e) {
  48.             e.printStackTrace();
  49.             return null;
  50.         }
  51.  
  52.         ResultSet rs = null;
  53.         try {
  54.             rs = st.executeQuery(query);
  55.         } catch (SQLException e) {
  56.             e.printStackTrace();
  57.             return null;
  58.         }
  59.  
  60.         return rs;
  61.     }
  62.  
  63.     /**
  64.      * Drops a table from the database
  65.      * @param connection database connection to drop the table from
  66.      * @param table table to be dropped from the database
  67.      */
  68.     public static void dropTable(Connection connection, String table) {
  69.         Statement st = null;
  70.         try {
  71.             st = connection.createStatement();
  72.             st.execute("DROP TABLE " + table);
  73.             st.close();
  74.         } catch (SQLException e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * Creates a table in the database
  81.      * @param connection database connection to create the table in
  82.      * @param tableDescription description of the table to be created
  83.      */
  84.     public static void createTable(Connection connection, String tableDescription) {
  85.         // TODO: Add check for pre-existing table and drop it if there already
  86.         Statement st = null;
  87.         try {
  88.             st = connection.createStatement();
  89.             st.execute("CREATE TABLE " + tableDescription);
  90.             st.close();
  91.         } catch (SQLException e) {
  92.             e.printStackTrace();
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Inserts file values into a database table
  98.      * @param connection database connection to insert values in
  99.      * @param table the table to insert data into
  100.      * @param file the file to read information from
  101.      * @return the number of rows read into the table
  102.      */
  103.     public static int insertIntoTableFromFile(Connection connection, String table, String file) {
  104.  
  105.         BufferedReader br = null;
  106.         int numRows = 0;
  107.         try {
  108.             Statement st = connection.createStatement();
  109.             String sCurrentLine, brokenLine[], composedLine = "";
  110.             br = new BufferedReader(new FileReader(file));
  111.  
  112.             while ((sCurrentLine = br.readLine()) != null) {
  113.                 // Insert each line to the DB
  114.                 brokenLine = sCurrentLine.split(",");
  115.                 composedLine = "INSERT INTO customer VALUES (";
  116.                 int i;
  117.                 for (i = 0; i < brokenLine.length - 1; i++) {
  118.                     composedLine += "'" + brokenLine[i] + "',";
  119.                 }
  120.                 composedLine += "'" + brokenLine[i] + "')";
  121.                 numRows = st.executeUpdate(composedLine);
  122.             }
  123.         } catch (IOException e) {
  124.             e.printStackTrace();
  125.         } catch (SQLException e) {
  126.             e.printStackTrace();
  127.         } finally {
  128.             try {
  129.                 if (br != null)
  130.                     br.close();
  131.             } catch (IOException ex) {
  132.                 ex.printStackTrace();
  133.             }
  134.         }
  135.         return numRows;
  136.     }
  137.  
  138.     public static void readCredentialsFromUser(String user, String password, String database) {
  139.         Scanner scan = new Scanner(System.in);
  140.         System.out.println("Enter the database to be accessed");
  141.         database = scan.nextLine();
  142.         System.out.println("Enter your username");
  143.         user = scan.nextLine();
  144.         System.out.println("Enter your password");
  145.         password = scan.nextLine();
  146.         scan.close();
  147.     }
  148.  
  149.     /**
  150.      * Main method for the program to control a database
  151.      * @param argv command line arguments
  152.      * @throws SQLException
  153.      */
  154.     public static void main(String[] argv) throws SQLException {
  155.         String user = "";
  156.         String password = "";
  157.         String database = "";
  158.  
  159.         readCredentialsFromUser(user, password, database);
  160.  
  161.         Connection connection = connectToDatabase(user, password, database);
  162.  
  163.         if (connection != null) {
  164.             System.out.println("You made it, take control of your database now!");
  165.         } else {
  166.             System.out.println("Failed to make connection!");
  167.             return;
  168.         }
  169.         // Now we're ready to work on the DB
  170.  
  171. //        String query = "SELECT * FROM branch";
  172. //        ResultSet rs = executeSelect(connection, query);
  173. //        try {
  174. //            while (rs.next()) {
  175. //                System.out.print("Column 1 returned ");
  176. //                System.out.println(rs.getString(1));
  177. //            }
  178. //        } catch (SQLException e) {
  179. //            e.printStackTrace();
  180. //        }
  181. //        rs.close();
  182. //
  183. //        dropTable(connection, "customer");
  184. //        createTable(
  185. //                connection,
  186. //                "customer(id int primary key, name varchar(15), street varchar(15), city varchar(15));");
  187. //        int rows = insertIntoTableFromFile(connection, "customer",
  188. //                "src/Table.txt");
  189. //        System.out.println(rows + " rows inserted.");
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement