Is the Database Connection Read-Only? in JDBC

In GUI database applications, before letting the user insert or update records, you need to make sure that the given Connection object is updatable (which means that records can be inserted or updated). To check for this, you can use the DatabaseMetaData.isReadOnly() method. This method returns true if the associated database is in read-only mode (which means that inserts or updates are not allowed). The following snippet shows how to use this method:

import java.sql.Connection;
import java.sql.DatabaseMetaData;
...
Connection conn = null;
DatabaseMetaData dbMetaData = null;
try {
conn = getConnection(); // get a valid database connection
dbMetaData = conn.getMetaData();
if (dbMetaData == null) {
// database metadata is NOT supported
}
else {
// database metadata is supported and you can invoke
// over 100 methods defined in DatabaseMetaData
// check to see if the database is read-only
boolean readOnly = dbMetaData.isReadOnly();
if (readOnly) {
// insert/updates are not allowed
}
else {
// insert/updates are allowed
}
...
}
}
catch(SQLException e) {
// deal and handle the exception
...
}
finally {
// close resources
}


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics