PreparedStatement SQLWarning
In this chapter you will learn:
Check for a SQL Warning
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLWarning;
import java.sql.Statement;
/* j a v a 2 s .co m*/
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,name varchar(30));");
st.executeUpdate("create table survey (id int, myDate TIMESTAMP );");
String INSERT_RECORD = "insert into survey(id) values(?)";
PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
pstmt.setString(1, "1");
pstmt.executeUpdate();
pstmt.setInt(1, 1);
pstmt.setString(2, "name");
pstmt.executeUpdate();
// Get warnings on PreparedStatement object
SQLWarning warning = pstmt.getWarnings();
while (warning != null) {
// Process statement warnings...
String message = warning.getMessage();
String sqlState = warning.getSQLState();
int errorCode = warning.getErrorCode();
warning = warning.getNextWarning();
}
st.close();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Statements, ResultSet, Exception, Warning