
com.github.fridujo.automocker.api.jdbc.Connections Maven / Gradle / Ivy
The newest version!
package com.github.fridujo.automocker.api.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Connections {
public static List tables(Connection c) {
List tables = new ArrayList<>();
try (PreparedStatement ps = c.prepareStatement("SHOW TABLES"); ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
tables.add(rs.getString(1));
}
} catch (SQLException e) {
throw new IllegalStateException("Unable to list tables", e);
}
return tables;
}
public static void execute(Connection c, String statement) {
try (PreparedStatement p = c.prepareStatement(statement)) {
p.execute();
} catch (SQLException e) {
throw new IllegalStateException("Could not execute Statement [" + statement + "]", e);
}
}
public static void truncate(Connection c, String tableName) {
execute(c, "TRUNCATE TABLE " + tableName);
}
public static Object selectFirstLines(Connection c, String tableName, int maxLines) {
try (PreparedStatement p = c.prepareStatement("SELECT TOP " + maxLines + " * FROM " + tableName);
ResultSet rs = p.executeQuery()) {
int columns = rs.getMetaData()
.getColumnCount();
List> result = new ArrayList<>();
while (rs.next()) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy