Java SQL Type deleteData(Connection conn)

Here you can find the source of deleteData(Connection conn)

Description

Delete records from tables ISSUE_TYPES, DUMMYUSER and ISSUES.

License

Open Source License

Parameter

Parameter Description
conn the connection established to the database.

Exception

Parameter Description
SQLException if any error occurs while executing the SQL.
IOException if any io exception occurs

Declaration

public static void deleteData(Connection conn) throws SQLException, IOException 

Method Source Code


//package com.java2s;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import java.sql.Connection;

import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /**/* w w  w. j a  v a  2s  .  co m*/
     * Represents the database configure file path.
     */
    private static final String DELETE_SQL_FILE_PATH = "./test_files/delete.sql";

    /**
     * Delete records from tables ISSUE_TYPES, DUMMYUSER and ISSUES.
     *
     * @param conn the connection established to the database.
     * @throws SQLException if any error occurs while executing the SQL.
     * @throws IOException if any io exception occurs
     */
    public static void deleteData(Connection conn) throws SQLException, IOException {
        executeFileSQL(conn, DELETE_SQL_FILE_PATH);
    }

    /**
     * Executes the specified SQL read from file in database. Empty lines will be ignored.
     *
     * @param connection the connection established to the database
     * @param file the file to be read from
     * @throws IOException if any error occurs during reading
     * @throws SQLException if any error occurs while executing the SQL
     */
    private static void executeFileSQL(Connection connection, String file) throws IOException, SQLException {
        String[] values = readFile(file).split(";");

        Statement statement = connection.createStatement();
        try {
            for (int i = 0; i < values.length; i++) {
                String sql = values[i].trim();
                if (sql.length() != 0) {
                    statement.executeUpdate(sql);
                }
            }
        } finally {
            statement.close();
        }
    }

    /**
     * Reads the content of a given file.
     *
     * @param fileName the name of the file to read.
     * @return a string represents the content.
     * @throws IOException if any error occurs during reading.
     */
    private static String readFile(String fileName) throws IOException {
        Reader reader = new FileReader(fileName);

        try {
            // Create a StringBuilder instance
            StringBuilder sb = new StringBuilder();

            // Buffer for reading
            char[] buffer = new char[1024];

            // Number of read chars
            int k = 0;

            // Read characters and append to string builder
            while ((k = reader.read(buffer)) != -1) {
                sb.append(buffer, 0, k);
            }

            // Return read content
            return sb.toString().replace("\r\n", "\n");
        } finally {
            reader.close();
        }
    }
}

Related

  1. createSignature(Method m)
  2. createSqlDataType(@Nonnull final String javaDataType)
  3. createTable2()
  4. createTestData()
  5. decodeJavaType(Object pType)
  6. deleteFromAllTables(Connection con)
  7. deserializeSQLTypesToJava(String sqltypename, String value)
  8. dumpTypes(Connection pConnection)
  9. extractConstant(String typeName)