Get Next SQL Warning() : SQL Warning : Database SQL JDBC : Java examples (example source code) Organized by topic

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Database SQL JDBC » SQL WarningScreenshots 
Get Next SQL Warning()

import java.sql.Connection;
import java.sql.DataTruncation;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

public class DemoDataTruncation {

  public static void displayError(DataTruncation dataTruncation) {
    System.out.println("Data truncation error: ");
    System.out.println(dataTruncation.getDataSize() " bytes should have been ");
    if (dataTruncation.getRead()) {
      System.out.println("Read (Error:) ");
    else {
      System.out.println("Written (Error:) ");
    }
    System.out.println(dataTruncation.getTransferSize()
        " number of bytes of data actually transferred.");
  }

  public static void displayError(SQLWarning warning) {
    while (warning != null) {
      if (warning instanceof DataTruncation) {
        displayError((DataTruncationwarning);
      else {
        System.out.println(" Warning: " + warning.getMessage());
      }
      warning = warning.getNextWarning();
    }
  }

  public static Connection getMySQLConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }

  public static void main(String[] argsthrows Exception {
    Connection conn = getMySQLConnection();
    Statement stmt = null;
    try {
      stmt = conn.createStatement();
      stmt.executeUpdate("DELETE FROM animals_table");
      displayError(stmt.getWarnings());
      stmt.executeUpdate("INSERT INTO animals_table(id, name)"
          "VALUES(1, 'NameLongerThanColumnLengthInDatabase')");
      displayError(stmt.getWarnings());
    catch (DataTruncation dt) {
      displayError(dt);
      dt.printStackTrace();
    catch (SQLException se) {
      System.out.println("Database error message: " + se.getMessage());
    catch (Exception e) {
      e.printStackTrace();
    finally {
      stmt.close();
      conn.close();
    }
  }
}

           
       
Related examples in the same category
1. Rasing Custom Sql Warning
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.