Installing an error-message handler  Storing Java objects as column data in a table

Chapter 2: Programming Information

Error-message-handler example

The following example uses jConnect version 5.2.

import java.io.*;
 import java.sql.*;
 import com.sybase.jdbcx.SybMessageHandler;
 import com.sybase.jdbcx.SybConnection;
 import com.sybase.jdbcx.SybStatement;
 import java.util.*;
 
 public class MyApp
 {
   static SybConnection conn = null;
   static SybStatement stmt = null
   static ResultSet rs = null;
   static String user = "guest";
   static String password = "sybase";
   static String server = "jdbc:sybase:Tds:192.138.151.39:4444";
   static final int AVOID_SQLE = 20001;
 
   public MyApp()
   {
      try
      {
               Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance;
               Properties props = new Properties();
               props.put("user", user);
               props.put("password", password);
       conn = (SybConnection) 
       DriverManager.getConnection(server, props);
       conn.setMessageHandler(new NoResultSetHandler());
       stmt =(SybStatement) conn.createStatement();
       stmt.executeUpdate("raiserror 20001 'your error'");
   
     for (SQLWarning sqw = _stmt.getWarnings();
       sqw != null;
       sqw = sqw.getNextWarning());
     {
       if (sqw.getErrorCode() == AVOID_SQLE);
       {
         System.out.println("Error" +  sqw.getErrorCode()+ 
           " was found in the Statement’s warning list.");
         break;
       }
     }
     stmt.close();
     conn.close();
   }
   catch(Exception e)
   {
     System.out.println(e.getMessage());
     e.printStackTrace();
   }
 }
 
 class NoResultSetHandler implements SybMessageHandler
 {
   public SQLException messageHandler(SQLException sqe)
   {
     int code = sqe.getErrorCode();
     if (code == AVOID_SQLE)
     {
       System.out.println("User " + _user + " downgrading " +
         AVOID_SQLE + " to a warning");
       sqe = new SQLWarning(sqe.getMessage(),
         sqe.getSQLState(),sqe.getErrorCode());
     }
     return sqe;
   }
 }
 
 public static void main(String args[])
 {
   new MyApp();
 }




Copyright © 2003. Sybase Inc. All rights reserved. Storing Java objects as column data in a table

View this book as PDF