[英]check database has correct structure
我從用戶那里獲取數據庫的URL,用戶,密碼信息
Connection db = DriverManager.getConnection(url, user, pass);
稍后,我將動態地對此數據庫執行操作。 顯然,我也捕獲了錯誤,但是在我第一次連接數據庫時,我想驗證它是否具有將要使用的正確的表和列(更容易在開始時拋出錯誤“此數據庫未正確設置”)而不是做很多事情, 然后找出數據庫未正確設置)。
我該如何驗證?
例如,必須檢查其結構是否包含表名,列名和類型,例如
create table user (
username varchar(16) not null,
primary key (username)
);
create table foo (
id int unsigned auto_increment not null,
one int,
two tinytext not null,
three int
);
要使用JDBC以與數據庫無關的方式查詢表及其列,請調用conn.getMetaData()
。
然后使用getTables(String catalog, String schemaPattern, String tableNamePattern, String\\[\\] types)
查詢表信息。
使用getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
查詢列信息。
有關其他可用方法,請參見DatabaseMetaData
javadoc。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.