Skip to content

Instantly share code, notes, and snippets.

@jthoenes
Created March 13, 2011 16:24
Show Gist options
  • Save jthoenes/868226 to your computer and use it in GitHub Desktop.
Save jthoenes/868226 to your computer and use it in GitHub Desktop.
finally {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException ex) {
logger.error("Error in Database", ex);
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException ex) {
logger.error("Error in Database", ex);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
logger.error("Error in Database", ex);
}
}
}
public void call() throws ReflectiveOperationException, IOException {
try {
callWithReflection(arg);
} catch (final Exception e) {
logger.trace("Exception in reflection", e);
throw e;
}
}
List<Integer>[] matrix = new List[23];
List<integer>[] matrix = new List[23];
List[] nonGenericMatrix = matrix;
// No exception
nonGenericMatrix[0] = Arrays.asList("String1", "String1");
// java.lang.Integer cannot be cast to java.lang.String
List</integer><integer> vectorList = matrix[0];
Integer[] vector = new Integer[3];
Object[] vectorObjects = vector;
// java.lang.ArrayStoreException
vectorObjects[0] = "String"
try {
callWithReflection(arg);
} catch (final ReflectiveOperationException | IOException e) {
throw new RuntimeException(e);
}
public void readData() {
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
try {
conn = DriverManager.getConnection(CONNECTION_STRING);
statement = conn.createStatement();
resultSet = statement.executeQuery("SELECT patient, medication_day, medication_desc FROM medications");
while (resultSet.next()) {
String patient = resultSet.getString("patient");
LocalDate medication_day = LocalDate.fromDateFields(resultSet.getDate("medication_day"));
String medication_desc = resultSet.getString("medication_desc");
System.out.println(String.format("'%s','%s','%s'", patient, medication_day, medication_desc));
}
} catch (SQLException ex) {
logger.error("Error in Database", ex);
} finally {
try {
resultSet.close();
statement.close();
conn.close();
} catch (SQLException ex) {
logger.error("Error in Database", ex);
}
}
}
try {
callWithReflection(arg);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
callWithReflection(arg);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
try (Connection conn = DriverManager.getConnection(CONNECTION_STRING);
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT patient, medication_day, medication_desc FROM medications");
BufferedWriter output = Files.newBufferedWriter(target, Charset.forName("utf-8"))) {
while (resultSet.next()) {
String patient = resultSet.getString("patient");
LocalDate medication_day = LocalDate.fromDateFields(resultSet.getDate("medication_day"));
String medication_desc = resultSet.getString("medication_desc");
output.write(String.format("'%s','%s','%s'\n", patient, medication_day, medication_desc));
}
} catch (SQLException ex) {
logger.error("Error in Database", ex);
} catch (IOException ex) {
logger.error("Error in File Writing", ex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment