本文整理汇总了Java中java.sql.DatabaseMetaData类的典型用法代码示例。如果您正苦于以下问题:Java DatabaseMetaData类的具体用法?Java DatabaseMetaData怎么用?Java DatabaseMetaData使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DatabaseMetaData类属于java.sql包,在下文中一共展示了DatabaseMetaData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBug38367
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Tests fix for BUG#38367 - DatabaseMetaData dbMeta = this.conn.getMetaData();
* this.rs = dbMeta.getProcedureColumns("test", null, "nullableParameterTest", null);
* ...
* Short columnNullable = new Short(this.rs.getShort(12));
* assertTrue("Parameter " + columnName + " do not allow null arguments",
* columnNullable.intValue() == java.sql.DatabaseMetaData.procedureNullable);
* was failing for no good reason.
*
* @throws Exception
* if the test fails.
*/
public void testBug38367() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
try {
createProcedure("sptestBug38367",
"(OUT nfact VARCHAR(100), IN ccuenta VARCHAR(100),\nOUT ffact VARCHAR(100),\nOUT fdoc VARCHAR(100))" + "\nBEGIN\nEND");
DatabaseMetaData dbMeta = this.conn.getMetaData();
this.rs = dbMeta.getProcedureColumns(this.conn.getCatalog(), null, "sptestBug38367", null);
while (this.rs.next()) {
String columnName = this.rs.getString(4);
Short columnNullable = new Short(this.rs.getShort(12));
assertTrue("Parameter " + columnName + " is not java.sql.DatabaseMetaData.procedureNullable.",
columnNullable.intValue() == java.sql.DatabaseMetaData.procedureNullable);
}
} finally {
}
}
示例2: testBug8803
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Tests fix for BUG#8803, 'DATA_TYPE' column from
* DBMD.getBestRowIdentifier() causes ArrayIndexOutOfBoundsException when
* accessed (and in fact, didn't return any value).
*
* @throws Exception
* if the test fails.
*/
public void testBug8803() throws Exception {
String tableName = "testBug8803";
createTable(tableName, "(field1 INT NOT NULL PRIMARY KEY)");
DatabaseMetaData metadata = this.conn.getMetaData();
try {
this.rs = metadata.getBestRowIdentifier(this.conn.getCatalog(), null, tableName, DatabaseMetaData.bestRowNotPseudo, true);
assertTrue(this.rs.next());
this.rs.getInt("DATA_TYPE"); // **** Fails here *****
} finally {
if (this.rs != null) {
this.rs.close();
this.rs = null;
}
}
}
示例3: getColumns
import java.sql.DatabaseMetaData; //导入依赖的package包/类
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) {
if (catalog == null) {
if ("s1table1".equals(tableNamePattern)) {
return JDBCStubUtil.columnsResultSet(
new String[] { "S1TABLE1_INTEGER_COL", "S1TABLE1_VARCHAR_COL", "S1TABLE1_FK_COL" },
new String[] { "INTEGER", "VARCAHR", "INTEGER" },
new int[] { Types.INTEGER, Types.VARCHAR, Types.INTEGER },
new int[] { 0, 20, 0 },
new int[] { 0, 0, 0 },
new int[] { DatabaseMetaData.columnNoNulls, DatabaseMetaData.columnNullable, DatabaseMetaData.columnNullable }
);
} else if ("s1table2".equals(tableNamePattern)) {
return JDBCStubUtil.columnsResultSet(
new String[] { "S1TABLE2_INTEGER_COL" },
new String[] { "INTEGER" },
new int[] { Types.INTEGER },
new int[] { 0 },
new int[] { 0 },
new int[] { DatabaseMetaData.columnNoNulls }
);
}
}
return JDBCStubUtil.emptyResultSet();
}
示例4: testQuotedGunk
import java.sql.DatabaseMetaData; //导入依赖的package包/类
public void testQuotedGunk() throws Exception {
createTable("testQuotedGunk", "(field1 int)");
String quotedCatalog = "`" + this.conn.getCatalog() + "`";
String unquotedCatalog = this.conn.getCatalog();
DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = dbmd.getTables(quotedCatalog, null, "testQuotedGunk", new String[] { "TABLE" });
assertTrue(this.rs.next());
this.rs = dbmd.getTables(unquotedCatalog, null, "testQuotedGunk", new String[] { "TABLE" });
assertTrue(this.rs.next());
this.rs = dbmd.getColumns(quotedCatalog, null, "testQuotedGunk", "field1");
assertTrue(this.rs.next());
this.rs = dbmd.getColumns(unquotedCatalog, null, "testQuotedGunk", "field1");
assertTrue(this.rs.next());
}
示例5: testGetColumnsUsingInfoSchema
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Tests the implementation of Information Schema for columns.
*/
public void testGetColumnsUsingInfoSchema() throws Exception {
if (versionMeetsMinimum(5, 0, 7)) {
createTable("t1", "(c1 char(1))");
Properties props = new Properties();
props.put("useInformationSchema", "true");
Connection conn1 = null;
try {
conn1 = getConnectionWithProps(props);
DatabaseMetaData metaData = conn1.getMetaData();
this.rs = metaData.getColumns(null, null, "t1", null);
this.rs.next();
assertEquals("t1", this.rs.getString("TABLE_NAME"));
assertEquals("c1", this.rs.getString("COLUMN_NAME"));
assertEquals("CHAR", this.rs.getString("TYPE_NAME"));
assertEquals("1", this.rs.getString("COLUMN_SIZE"));
} finally {
if (conn1 != null) {
conn1.close();
}
}
}
}
示例6: testGetColumnsUnsigned
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Tests whether or not unsigned columns are reported correctly in
* DBMD.getColumns
*
* @throws Exception
*/
public void testGetColumnsUnsigned() throws Exception {
try {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetUnsignedCols");
this.stmt.executeUpdate("CREATE TABLE testGetUnsignedCols (field1 BIGINT, field2 BIGINT UNSIGNED)");
DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = dbmd.getColumns(this.conn.getCatalog(), null, "testGetUnsignedCols", "%");
assertTrue(this.rs.next());
// This row doesn't have 'unsigned' attribute
assertTrue(this.rs.next());
assertTrue(this.rs.getString(6).toLowerCase().indexOf("unsigned") != -1);
} finally {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetUnsignedCols");
}
}
示例7: checkFunctionColumnTypeForBug68307
import java.sql.DatabaseMetaData; //导入依赖的package包/类
private void checkFunctionColumnTypeForBug68307(String testAgainst, DatabaseMetaData testDbMetaData) throws Exception {
rs = testDbMetaData.getFunctionColumns(null, null, "testBug68307_%", "%");
while (rs.next()) {
String message = testAgainst + ", function <" + rs.getString("FUNCTION_NAME") + "." + rs.getString("COLUMN_NAME") + ">";
if (rs.getString("COLUMN_NAME") == null || rs.getString("COLUMN_NAME").length() == 0) {
assertEquals(message, DatabaseMetaData.functionReturn, rs.getShort("COLUMN_TYPE"));
} else if (rs.getString("COLUMN_NAME").endsWith("_in")) {
assertEquals(message, DatabaseMetaData.functionColumnIn, rs.getShort("COLUMN_TYPE"));
} else if (rs.getString("COLUMN_NAME").endsWith("_inout")) {
assertEquals(message, DatabaseMetaData.functionColumnInOut, rs.getShort("COLUMN_TYPE"));
} else if (rs.getString("COLUMN_NAME").endsWith("_out")) {
assertEquals(message, DatabaseMetaData.functionColumnOut, rs.getShort("COLUMN_TYPE"));
} else {
fail("Column '" + rs.getString("FUNCTION_NAME") + "." + rs.getString("COLUMN_NAME") + "' not expected within test case.");
}
}
}
示例8: showTables
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Retrieves a description of tables available for given connection.
*
* @param conn
* The connection used to list the tables.
* @throws SQLException
* if error happens while querying the data source.
*/
private static void showTables(Connection conn) throws SQLException {
assert (conn != null);
DatabaseMetaData meta = conn.getMetaData();
ResultSet result = null;
try {
result = meta.getTables(null, null, null, null);
printResultSet(result);
} finally {
if (result != null) {
result.close();
}
}
}
示例9: testBug43714
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Bug #43714 - useInformationSchema with DatabaseMetaData.getExportedKeys()
* throws exception
*/
public void testBug43714() throws Exception {
Connection c_IS = null;
try {
c_IS = getConnectionWithProps("useInformationSchema=true");
DatabaseMetaData dbmd = c_IS.getMetaData();
this.rs = dbmd.getExportedKeys("x", "y", "z");
} finally {
try {
if (c_IS != null) {
c_IS.close();
}
} catch (SQLException ex) {
}
}
}
示例10: testBug21215151
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Tests fix for BUG#21215151 - DATABASEMETADATA.GETCATALOGS() FAILS TO SORT RESULTS.
*
* DatabaseMetaData.GetCatalogs() relies on the results of 'SHOW DATABASES' which deliver a sorted list of databases except for 'information_schema' which
* is always returned in the first position.
* This test creates set of databases around the relative position of 'information_schema' and checks the ordering of the final ResultSet.
*
* @throws Exception
* if the test fails.
*/
public void testBug21215151() throws Exception {
createDatabase("z_testBug21215151");
createDatabase("j_testBug21215151");
createDatabase("h_testBug21215151");
createDatabase("i_testBug21215151");
createDatabase("a_testBug21215151");
DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = dbmd.getCatalogs();
System.out.println("Catalogs:");
System.out.println("--------------------------------------------------");
while (this.rs.next()) {
System.out.println("\t" + this.rs.getString(1));
}
this.rs.beforeFirst();
// check the relative position of each element in the result set compared to the previous element.
String previousDb = "";
while (this.rs.next()) {
assertTrue("'" + this.rs.getString(1) + "' is lexicographically lower than the previous catalog. Check the system output to see the catalogs list.",
previousDb.compareTo(this.rs.getString(1)) < 0);
previousDb = this.rs.getString(1);
}
}
示例11: metaTreeCommit
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
*
* @param dbmdJson
* @param repo
* @param tree
* @param conn
* @param clmnTree
* @return
* @throws IOException
* @throws SQLException
*/
public static ObjectId metaTreeCommit(ObjectJson dbmdJson, Repository repo, TreeFormatter tree,
Connection conn, Boolean clmnTree) throws IOException, SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
// ObjectJson dbmdJson = new ObjectJson();
String mapString = metaDbInfo(dbmd);
ObjectInserter objectInserter = repo.newObjectInserter();
ObjectId blobId = objectInserter.insert(Constants.OBJ_BLOB, mapString.getBytes());
objectInserter.flush();
tree.append(Consts.DATABASE, FileMode.REGULAR_FILE, blobId);
Utils.putTableMeta(repo, conn, dbmd, objectInserter, tree, clmnTree);
ObjectId treeId = objectInserter.insert(tree);
objectInserter.flush();
System.out.println("Tree ID: " + treeId.getName());
return treeId;
}
示例12: tableExists
import java.sql.DatabaseMetaData; //导入依赖的package包/类
private boolean tableExists(Handle h, String tableName) {
try {
String tableToCheck = tableName;
boolean caseSensitive = this.databaseKind == DatabaseKind.PostgreSQL;
if (!caseSensitive) {
tableToCheck = tableName.toUpperCase(Locale.ROOT);
}
DatabaseMetaData metaData = h.getConnection().getMetaData();
try (ResultSet rs = metaData.getTables(null, null, tableToCheck, null)) {
while (rs.next()) {
String foundTable = rs.getString("TABLE_NAME");
if (tableToCheck.equalsIgnoreCase(foundTable)) {
return true;
}
}
}
return false;
} catch (SQLException ex) {
throw IconDataAccessException.launderThrowable("Cannot check if the table " + tableName + " already exists", ex);
}
}
示例13: DatabaseIntrospector
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Instantiates a new database introspector.
*
* @param context
* the context
* @param databaseMetaData
* the database meta data
* @param javaTypeResolver
* the java type resolver
* @param warnings
* the warnings
*/
public DatabaseIntrospector(Context context,
DatabaseMetaData databaseMetaData,
JavaTypeResolver javaTypeResolver, List<String> warnings) {
super();
this.context = context;
this.databaseMetaData = databaseMetaData;
this.javaTypeResolver = javaTypeResolver;
this.warnings = warnings;
logger = LogFactory.getLog(getClass());
//获取数据库的版本信息
try {
DatabaseMetaData md = databaseMetaData.getConnection().getMetaData();
databaseProductName = md.getDatabaseProductName().toUpperCase();
} catch (SQLException se) {
warnings.add("获取数据库版本失败:" + se.getMessage());
}
}
示例14: testBug8812
import java.sql.DatabaseMetaData; //导入依赖的package包/类
/**
* Tests fix for Bug#8812, DBMD.getIndexInfo() returning inverted values for
* 'NON_UNIQUE' column.
*
* @throws Exception
* if the test fails.
*/
public void testBug8812() throws Exception {
String tableName = "testBug8812";
try {
createTable(tableName, "(field1 INT, field2 INT, INDEX(field1), UNIQUE INDEX(field2))");
DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = dbmd.getIndexInfo(this.conn.getCatalog(), null, tableName, true, false);
assertTrue(this.rs.next()); // there should be one row that meets
// this requirement
assertEquals(this.rs.getBoolean("NON_UNIQUE"), false);
this.rs = dbmd.getIndexInfo(this.conn.getCatalog(), null, tableName, false, false);
assertTrue(this.rs.next()); // there should be two rows that meets
// this requirement
assertEquals(this.rs.getBoolean("NON_UNIQUE"), false);
assertTrue(this.rs.next());
assertEquals(this.rs.getBoolean("NON_UNIQUE"), true);
} finally {
dropTable(tableName);
}
}
示例15: columnInAnyIndex
import java.sql.DatabaseMetaData; //导入依赖的package包/类
protected final boolean columnInAnyIndex(String tablename, String colname)
throws Exception {
tablename = fixIdentifier(tablename);
colname = fixIdentifier(colname);
DatabaseMetaData md = getConnection().getMetaData();
ResultSet rs = md.getIndexInfo(null, getSchema(), tablename, false, false);
// printResults(rs, "columnInIndex(" + viewname + ", " + colname +
// ", " + indexname + ")");
while ( rs.next() ) {
String ixColName = rs.getString(9);
if ( ixColName.equals(colname) ) {
return true;
}
}
return false;
}