Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sit322.azurestorage;
- import java.sql.*;
- import java.util.Properties;
- import java.io.IOException;
- import java.io.File;
- import java.util.Scanner;
- public class CreateTableInsertRows {
- private Connection connection = null;
- public static void main(String[] args) {
- CreateTableInsertRows ctirObj = new CreateTableInsertRows();
- try {
- //Creates a connection then reads the text file
- ctirObj.connection = ctirObj.initConnection();
- ctirObj.readData();
- ctirObj.readTable();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- try {
- ctirObj.connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- private Connection initConnection() throws ClassNotFoundException, SQLException {
- // Initialize connection variables.
- String host = "dominicclustersql.mysql.database.azure.com";
- String database = "clusterresults";
- String user = "******@dominicclustersql";
- String password = "********";
- // check that the driver is installed
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new ClassNotFoundException("MySQL JDBC driver NOT detected in library path.", e);
- }
- System.out.println("MySQL JDBC driver detected in library path.");
- Connection connection = null;
- // Initialize connection object
- try {
- String url = String.format("jdbc:mysql://%s/%s", host, database);
- // Set connection properties.
- Properties properties = new Properties();
- properties.setProperty("user", user);
- properties.setProperty("password", password);
- properties.setProperty("useSSL", "true");
- properties.setProperty("verifyServerCertificate", "true");
- properties.setProperty("requireSSL", "false");
- properties.setProperty("useTimezone", "true");
- properties.setProperty("useLegacyDatetimeCode", "false");
- properties.setProperty("serverTimezone", "UTC");
- // get connection
- connection = DriverManager.getConnection(url, properties);
- } catch (SQLException e) {
- throw new SQLException("Failed to create connection to database.", e);
- }
- if (connection != null) {
- // Perform some SQL queries over the connection.
- try {
- // Drop previous table of same name if one exists.
- Statement statement = connection.createStatement();
- statement.execute("DROP TABLE IF EXISTS wordcount;");
- System.out.println("Finished dropping table (if existed).");
- // Create table.
- statement
- .execute("CREATE TABLE wordcount (word VARCHAR(50), count INTEGER);");
- System.out.println("Created table.");
- } catch (SQLException e) {
- throw new SQLException("Encountered an error when executing given sql statement.", e);
- }
- }
- return connection;
- }
- public String word;
- public int count;
- // This function is called and reads the text file and strips out the white space and saves each word and the count
- public void readData() {
- //Counts how many times this while loop is used
- int howmany = 0;
- //uses the ouput text and reads it
- try(Scanner input = new Scanner(new File("src/50littlemen.txt")))
- {
- while(input.hasNextLine()) {
- word ="";
- String line;
- line = input.nextLine();
- try(Scanner data = new Scanner(line))
- {
- //while ther is a word next it will read it
- while(!data.hasNextInt()) {
- word += data.next()+" ";
- howmany++;
- }
- //trims out the white space
- word = word.trim();
- if(data.hasNextInt()) {
- count = data.nextInt();
- }
- }
- //calls the save to database fucntion
- savaData();
- }
- }catch(IOException e)
- {
- System.out.println(e);
- }
- System.out.println(howmany);
- }
- private void savaData() {
- if (this.connection != null) {
- // Perform some SQL queries over the connection.
- try {
- // saves teh data saved from the text file into the sql database
- PreparedStatement preparedStatement = connection
- .prepareStatement("INSERT INTO wordcount VALUES(?, ?)");
- preparedStatement.setString(1, word);
- preparedStatement.setInt(2, count);
- preparedStatement.executeUpdate();
- // NOTE No need to commit all changes to database, as auto-commit is enabled by
- // default.
- } catch (SQLException e) {
- System.out.println(e);
- }
- }
- }
- private void readTable() throws SQLException {
- System.out.println("...Reading Table...");
- if (this.connection != null) {
- // Perform some SQL queries over the connection.
- try {
- //reads teh database and prints to conssole.
- Statement statement = connection.createStatement();
- boolean hasResulSet = statement.execute("SELECT * FROM wordcount");
- if(hasResulSet)
- {
- ResultSet result = statement.getResultSet();
- ResultSetMetaData metaData = result.getMetaData();
- int columnCount = metaData.getColumnCount();
- for(int i=1; i<=columnCount; i++) {
- System.out.print(metaData.getColumnLabel(i)+"\t\t");
- }
- System.out.println();
- while(result.next()) {
- System.out.printf("%-20s%4d%4d%n", result.getString("word"), result.getInt("count"));
- }
- }
- } catch (SQLException e) {
- throw new SQLException("Encountered an error when executing given sql statement", e);
- }
- } else {
- System.out.println("Failed to create connection to database.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement