In JDBC getWarnings() method is used get the warnings reported by the call of Connection, ResultSet, and Statement objects.
clearWarrning() method is used to clear all the warning reported by the above object.
These methods are called along with object.
Example-
connection.getWarnings();
connection.clearWarnings();
An example of these method is given below.
JDBCWarningsExample.java
package roseindia.net;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
public class JDBCWarningsExample {
Connection connection = null;
public JDBCWarningsExample() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
}
public Connection createConnection() {
Connection con = null;
if (connection != null) {
System.out.println("Cant create a connection");
} else {
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student", "root",
"root");
System.out.println("Connection created Successfully");
DatabaseMetaData dbMetaData = con.getMetaData();
ResultSet dbInfo = dbMetaData.getCatalogs();
System.out.println("Getting Concurrency of MetaData");
System.out.println(dbInfo.getConcurrency());
} catch (SQLException e) {
System.out.println(e.toString());
}
}
return con;
}
public static void main(String[] args) throws SQLException {
JDBCWarningsExample jdbccOnnectionExample = new JDBCWarningsExample();
Statement statement = null;
ResultSet resultSet = null;
Connection conn = jdbccOnnectionExample.createConnection();
try {
statement = conn.createStatement();
resultSet = statement.executeQuery("SELECT * FROM student");
} catch (Exception e) {
System.out.println(e.toString());
}
SQLWarning conWarning = conn.getWarnings();
System.out.println("Connection warning " + conWarning);
conn.clearWarnings();// clearing warning
SQLWarning stmtWarning = statement.getWarnings();
System.out.println("Statement warning " + stmtWarning);
SQLWarning resWarning = resultSet.getWarnings();
System.out.println("Result Set warningd " + resWarning);
statement.close();
resultSet.close();
conn.close();// closing a connection
}
}
When you run this application it will display message as shown below:| Connection created Successfully Getting Concurrency of MetaData 1007 Connection warning null Statement warning null Result Set warningd null |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.