use of java.sql.ResultSetMetaData in project OpenAM by OpenRock.
the class LogReadDBHandler method getTableNames.
/**
* Return table names for each logger
* @param logMgr Log Manager that is maintaing table names
* @return table names for each logger
*/
public String[][] getTableNames(java.util.logging.LogManager logMgr) {
try {
this.databaseURL = logMgr.getProperty(LogConstants.LOG_LOCATION);
this.dbDriver = logMgr.getProperty(LogConstants.DB_DRIVER);
this.dbUserName = logMgr.getProperty(LogConstants.DB_USER);
this.dbPassWord = logMgr.getProperty(LogConstants.DB_PASSWORD);
this.maxRecordsStr = logMgr.getProperty(LogConstants.MAX_RECORDS);
} catch (Exception e) {
return null;
}
try {
connectToDatabase(dbUserName, dbPassWord);
} catch (SQLException sqe) {
Debug.error("DBgetTableNames:connect:SQE:code=" + sqe.getErrorCode() + ", msg=" + sqe.getMessage());
return null;
} catch (ClassNotFoundException cnfe) {
Debug.error("DBgetTableNames:connect:CNFE: " + cnfe.getMessage());
return null;
}
isMySQL = false;
String queryString = null;
if (this.dbDriver.toLowerCase().indexOf("oracle") != -1) {
isMySQL = false;
//
// gonna be something like:
// select table_name from dba_all_tables where owner = 'AMADMIN';
//
queryString = "select table_name from dba_all_tables where owner = '" + (this.dbUserName).toUpperCase() + "'";
} else if (this.dbDriver.toLowerCase().indexOf("mysql") != -1) {
isMySQL = true;
//
// gonna be:
// show tables
//
queryString = "show tables";
}
Statement stmt = null;
ResultSet rs = null;
int numberOfColumns = 0;
ResultSetMetaData rsmd = null;
String[][] tableResults = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
if (Debug.messageEnabled()) {
Debug.message("DBgetTableNames:about to execute: " + queryString);
}
rs = stmt.executeQuery(queryString);
rsmd = rs.getMetaData();
numberOfColumns = rsmd.getColumnCount();
if (Debug.messageEnabled()) {
Debug.message("DBgetTableNames:#columns = " + numberOfColumns);
}
String[] spltHdrStr = new String[numberOfColumns];
for (int i = 1; i <= numberOfColumns; i++) {
String tempstr = rsmd.getColumnName(i);
spltHdrStr[i - 1] = tempstr;
}
// have to figure out #rows
int numberOfRows = 0;
while (rs.next()) {
numberOfRows++;
}
if (Debug.messageEnabled()) {
Debug.message("DBgetTableNames:#rows = " + numberOfRows);
}
//
// reset to the beginning
//
boolean isFirst = rs.first();
if (isFirst == false) {
Debug.error("DBgetTableNames:first() is false!");
}
String result = null;
tableResults = new String[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfColumns; i++) {
result = rs.getString(i + 1);
tableResults[0][i] = result;
}
int rowCount = 1;
while (rs.next()) {
for (int i = 0; i < numberOfColumns; i++) {
result = rs.getString(i + 1);
tableResults[rowCount][i] = result;
rowCount++;
}
}
stmt.close();
} catch (SQLException se) {
Debug.error("DBgetTableNames:query:SQE:code=" + se.getErrorCode() + ", msg=" + se.getMessage());
return null;
}
try {
conn.close();
} catch (SQLException ex) {
//
// might not care about this too much...?
//
Debug.error("DBgetTableNames:conn.close (" + ex.getErrorCode() + "): " + ex.getMessage());
}
return tableResults;
}
use of java.sql.ResultSetMetaData in project Info-Evaluation by TechnionYP5777.
the class Row method formTable.
public static void formTable(ResultSet s, List<Row> table) throws SQLException {
if (s == null)
return;
ResultSetMetaData rsmd;
try {
rsmd = s.getMetaData();
for (int NumOfCol = rsmd.getColumnCount(); s.next(); ) {
Row current_row = new Row();
for (int ¢ = 1; ¢ <= NumOfCol; ++¢) current_row.add(s.getObject(¢), rsmd.getColumnTypeName(¢));
table.add(current_row);
}
} catch (SQLException e) {
throw e;
}
}
use of java.sql.ResultSetMetaData in project voltdb by VoltDB.
the class ResultSetHelperService method getColumnValues.
@Override
public String[] getColumnValues(ResultSet rs) throws SQLException, IOException {
List<String> values = new ArrayList<String>();
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
values.add(getColumnValue(rs, metadata.getColumnType(i + 1), i + 1));
}
String[] valueArray = new String[values.size()];
return values.toArray(valueArray);
}
use of java.sql.ResultSetMetaData in project voltdb by VoltDB.
the class NonVoltDBBackend method runDML.
public VoltTable runDML(String dml) {
dml = dml.trim();
String indicator = dml.substring(0, 1).toLowerCase();
if (// "s" is for "select ..."
indicator.equals("s") || indicator.equals("(")) {
// "(" is for "(select ... UNION ...)" et. al.
try {
Statement stmt = dbconn.createStatement();
sqlLog.l7dlog(Level.DEBUG, LogKeys.sql_Backend_ExecutingDML.name(), new Object[] { dml }, null);
sqlLog.debug("Executing " + dml);
ResultSet rs = stmt.executeQuery(dml);
ResultSetMetaData rsmd = rs.getMetaData();
// note the index values here carefully
VoltTable.ColumnInfo[] columns = new VoltTable.ColumnInfo[rsmd.getColumnCount()];
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String colname = rsmd.getColumnLabel(i);
String type = rsmd.getColumnTypeName(i);
//LOG.fine("Column type: " + type);
columns[i - 1] = getColumnInfo(type, colname);
}
VoltTable table = new VoltTable(columns);
while (rs.next()) {
Object[] row = new Object[table.getColumnCount()];
for (int i = 0; i < table.getColumnCount(); i++) {
if (table.getColumnType(i) == VoltType.STRING)
row[i] = rs.getString(i + 1);
else if (table.getColumnType(i) == VoltType.TINYINT)
row[i] = rs.getByte(i + 1);
else if (table.getColumnType(i) == VoltType.SMALLINT)
row[i] = rs.getShort(i + 1);
else if (table.getColumnType(i) == VoltType.INTEGER)
row[i] = rs.getInt(i + 1);
else if (table.getColumnType(i) == VoltType.BIGINT)
row[i] = rs.getLong(i + 1);
else if (table.getColumnType(i) == VoltType.DECIMAL)
row[i] = rs.getBigDecimal(i + 1);
else if (table.getColumnType(i) == VoltType.FLOAT)
row[i] = rs.getDouble(i + 1);
else if (table.getColumnType(i) == VoltType.VARBINARY)
row[i] = rs.getBytes(i + 1);
else if (table.getColumnType(i) == VoltType.TIMESTAMP) {
Timestamp t = rs.getTimestamp(i + 1);
if (t == null) {
row[i] = null;
} else {
// convert from millisecond to microsecond granularity
row[i] = new org.voltdb.types.TimestampType(t.getTime() * 1000);
}
} else {
throw new ExpectedProcedureException("Trying to read a (currently) unsupported type from a JDBC resultset.");
}
if (rs.wasNull()) {
// JDBC returns 0/0.0 instead of null. Put null into the row.
row[i] = null;
}
}
table.addRow(row);
}
stmt.close();
rs.close();
return table;
} catch (Exception e) {
if (e instanceof ExpectedProcedureException) {
throw (ExpectedProcedureException) e;
}
sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
throw new ExpectedProcedureException(m_database_type + " Backend DML Error ", e);
}
} else {
try {
Statement stmt = dbconn.createStatement();
sqlLog.debug("Executing: " + dml);
long ucount = stmt.executeUpdate(dml);
sqlLog.debug(" result: " + String.valueOf(ucount));
VoltTable table = new VoltTable(new VoltTable.ColumnInfo("", VoltType.BIGINT));
table.addRow(ucount);
return table;
} catch (SQLException e) {
// glorious hack to determine if the error is a constraint failure
if (e.getMessage().contains("constraint")) {
sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_ConvertingHSQLExtoCFEx.name(), e);
final byte[] messageBytes = e.getMessage().getBytes();
ByteBuffer b = ByteBuffer.allocate(100 + messageBytes.length);
b.putInt(messageBytes.length);
b.put(messageBytes);
b.put(e.getSQLState().getBytes());
// ConstraintFailure.type
b.putInt(0);
try {
FastSerializer.writeString(m_database_type, b);
} catch (IOException e1) {
e1.printStackTrace();
}
//Table size is 0
b.putInt(0);
b.rewind();
throw new ConstraintFailureException(b);
} else {
sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
throw new ExpectedProcedureException(m_database_type + " Backend DML Error ", e);
}
} catch (Exception e) {
// rethrow an expected exception
sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
throw new ExpectedProcedureException(m_database_type + " Backend DML Error ", e);
}
}
}
use of java.sql.ResultSetMetaData in project voltdb by VoltDB.
the class TestJDBC method testVarbinary.
/*public void testSimpleStuff() {
String ddl = "create table test (cash integer default 23);";
String dml = "insert into test values (123);";
String query = "select * from test;";
Connection dbconn;
try {
Class.forName("org.hsqldb_voltpatches.jdbcDriver" );
dbconn = DriverManager.getConnection("jdbc:hsqldb:mem:x1", "sa", "");
dbconn.setAutoCommit(true);
dbconn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
Statement stmt = dbconn.createStatement();
stmt.execute(ddl);
SQLWarning warn = stmt.getWarnings();
if (warn != null)
System.out.println("warn: " + warn.getMessage());
assertTrue(warn == null);
long ucount = stmt.executeUpdate(dml);
assertTrue(ucount == 1);
ResultSet rs = stmt.executeQuery(query);
assertTrue(rs != null);
ResultSetMetaData rsmd = rs.getMetaData();
assertTrue(rsmd != null);
assertTrue(rsmd.getColumnCount() == 1);
boolean success = rs.next();
assertTrue(success);
int x = rs.getInt(1);
assertTrue(x == 123);
try {
stmt.execute("SHUTDOWN;");
} catch (Exception e) {};
dbconn.close();
System.gc();
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}*/
public void testVarbinary() {
String ddl = "create table testvb (cash integer default 23, b varbinary(1024) default NULL);";
String dml = "insert into testvb values (123, 'AAAA');";
String query = "select * from testvb;";
Connection dbconn;
try {
Class.forName("org.hsqldb_voltpatches.jdbcDriver");
dbconn = DriverManager.getConnection("jdbc:hsqldb:mem:x1", "sa", "");
dbconn.setAutoCommit(true);
dbconn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
Statement stmt = dbconn.createStatement();
stmt.execute(ddl);
SQLWarning warn = stmt.getWarnings();
if (warn != null)
System.out.println("warn: " + warn.getMessage());
assertTrue(warn == null);
long ucount = stmt.executeUpdate(dml);
assertTrue(ucount == 1);
ResultSet rs = stmt.executeQuery(query);
assertTrue(rs != null);
ResultSetMetaData rsmd = rs.getMetaData();
assertTrue(rsmd != null);
assertTrue(rsmd.getColumnCount() == 2);
boolean success = rs.next();
assertTrue(success);
int x = rs.getInt(1);
assertTrue(x == 123);
try {
stmt.execute("SHUTDOWN;");
} catch (Exception e) {
}
;
dbconn.close();
System.gc();
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}
Aggregations