sql

Handle SQL warning example

In this example we shall show you how to handle SQL Warnings in Java. To handle SQL Warnings one should perform the following steps:

  • Load the JDBC driver, using the forName(String className) API method of the Class. In this example we use the Oracle JDBC driver.
  • Create a Connection to the database. Invoke the getConnection(String url, String user, String password) API method of the DriverManager to create the connection.
  • Get the SQLWarning risen while connecting to the database, using the getWarnings() API method of the Connection.
  • Check the connectionWarning, with getMessage(), getSQLState(), getErrorCode() and then get the next warning with getNextWarning() API methods of the SQLWarning.
  • Execute an SQL Statement, which returns a ResultSet object. For each row of the ResultSet get the SQLWarnings, using the getWarnings() API method of the ResultSet.
  • Check on the resultsetWarning with the getMessage(), getSQLState(), getErrorCode() and then get the next warning with getNextWarning() API methods of the ResultSet,

as described in the code snippet below.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.javacodegeeks.snippets.core;
  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
 
public class SQLWarning {
  
  public static void main(String[] args) {
 
    Connection connection = null;
    try {
 
  // Load the Oracle JDBC driver
 
  String driverName = "oracle.jdbc.driver.OracleDriver";
 
  Class.forName(driverName);
 
 
  // Create a connection to the database
 
  String serverName = "localhost";
 
  String serverPort = "1521";
 
  String sid = "mySchema";
 
  String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + sid;
 
  String username = "username";
 
  String password = "password";
 
  connection = DriverManager.getConnection(url, username, password);
 
   
 
  System.out.println("Successfully Connected to the database!");
 
   
    } catch (ClassNotFoundException e) {
 
  System.out.println("Could not find the database driver " + e.getMessage());
    } catch (SQLException e) {
 
  System.out.println("Could not connect to the database " + e.getMessage());
    }
 
    try {
 
  // Get warnings risen while connecting to the database
 
  SQLWarning connectionWarning = connection.getWarnings();
 
   
 
  while (connectionWarning != null) {
 
 
String warningMessage = connectionWarning.getMessage();
 
 
String warningSQLState = connectionWarning.getSQLState();
 
 
int warningErrorCode = connectionWarning.getErrorCode();
 
 
 
 
 
System.out.println("Connection warning : " + warningErrorCode +" Message : " + warningMessage + " SQL state " + warningSQLState);
 
 
 
 
 
connectionWarning = connectionWarning.getNextWarning();
 
  }
 
 
  // Create a statement
 
  Statement statement = connection.createStatement();
 
 
  // Use the statement...
 
 
  // Get warnings risen while using the statement
 
  SQLWarning statementWarning = statement.getWarnings();
 
  if (statementWarning != null) {
 
 
String warningMessage = statementWarning.getMessage();
 
 
String warningSQLState = statementWarning.getSQLState();
 
 
int warningErrorCode = statementWarning.getErrorCode();
 
 
 
 
 
System.out.println("Statement warning : " + warningErrorCode +" Message : " + warningMessage + " SQL state " + warningSQLState);
 
 
 
 
 
statementWarning = statementWarning.getNextWarning();
 
  }
 
 
  // Get the result set from the statement
 
  ResultSet resultSet = statement.executeQuery("SELECT * FROM test_table");
 
  while (resultSet.next()) {
 
 
     
 
 
// Use result set ...
 
 
 
// Get warnings on the current row of the result set
 
 
SQLWarning resultsetWarning = resultSet.getWarnings();
 
 
if (resultsetWarning != null) {
 
 
 
  String warningMessage = resultsetWarning.getMessage();
 
 
 
  String warningSQLState = resultsetWarning.getSQLState();
 
 
 
  int warningErrorCode = resultsetWarning.getErrorCode();
 
 
 
   
 
 
 
  System.out.println("Resultset warning : " + warningErrorCode +" Message : " + warningMessage + " SQL state " + warningSQLState);
 
 
 
   
 
 
 
  resultsetWarning = resultsetWarning.getNextWarning();
 
 
}
 
  }
 
   
    } catch (SQLException e) {
    }
 
 }
 
}

Output:

Successfully Connected to the database!

 
This was an example of how to handle SQL Warnings in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button