/*
 * Copyright (c) 2012. AquaFold, Inc.
 */

package com.aquafold.tools.db;

import java.sql.ResultSetMetaData;
import java.util.Properties;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;

public class SybaseASEUnsignedExample {

	public static void main(String[] args) {

//		try {
//			Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
//		} catch(ClassNotFoundException e) {
//			e.printStackTrace();
//		}

		Properties props = new Properties();
		props.put("user", "sa");
		props.put("password", "password");

		String host = "10.168.11.57";
		String port = "5000";
		String url = "jdbc:sybase:Tds:" + host + ":" + port + "/NORTHWIND";
		Connection conn = null;
		try {
			conn = java.sql.DriverManager.getConnection(url, props);
		} catch(SQLException e) {
			e.printStackTrace();
		}

		Statement stmt = null;
		Statement initStmt = null;
		Statement dropStmt = null;

		try {
			stmt = conn.createStatement();
		} catch(SQLException e) {
			e.printStackTrace();
		}

		try {
			initStmt = conn.createStatement();
		} catch(SQLException e) {
			e.printStackTrace();
		}

		try {
			dropStmt = conn.createStatement();
		} catch(SQLException e) {
			e.printStackTrace();
		}

		try {
			StringBuffer buff = new StringBuffer();
			buff.append("DROP TABLE test_unsigned_data");
			try {
				dropStmt.execute(buff.toString());
			} catch(Exception e) {
			} finally {
				dropStmt.close();
			}

			buff.setLength(0);
			buff.append("CREATE TABLE test_unsigned_data(col_unsigned_int UNSIGNED INT NULL) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(3134534571) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(4116534511) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(3857234011) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(3467902341) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(3789454341) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(3935684521) \n");
			buff.append("INSERT INTO test_unsigned_data(col_unsigned_int) VALUES(4196453231) \n");

			initStmt.execute(buff.toString());
			initStmt.close();

			buff.setLength(0);
			buff.append("select col_unsigned_int from test_unsigned_data \n");
			boolean hasResultset = stmt.execute(buff.toString());

			int resultsetCount = 0;
			ResultSet resultset = null;
			int recordCount = 0;
			boolean finished = false;

			do {
				if(hasResultset) {
					recordCount = stmt.getUpdateCount();
					if(recordCount == -1) {
						resultset = stmt.getResultSet();
						if(resultset != null) {
							System.out.println("");
							resultsetCount++;
							System.out.println("Resultset #" + resultsetCount);
							System.out.println("************************************************************");
							ResultSetMetaData rsmd = resultset.getMetaData();
							while(resultset.next()) {
								System.out.println("MetaData ClassName      : " + rsmd.getColumnClassName(1));
								System.out.println("MetaData ColumnTypeName : " + rsmd.getColumnTypeName(1));
								System.out.println("MetaData ColumnType     : " + rsmd.getColumnType(1));
								System.out.println("getObject Value         : " + resultset.getObject(1));
								System.out.println("getObject Class         : " + resultset.getObject(1).getClass().toString());
								System.out.println("************************************************************");
							}
							resultset.close();
						}
					}
				}
				try {
					hasResultset = stmt.getMoreResults();
				} catch(SQLException e) {
					hasResultset = false;
					try {
						hasResultset = stmt.getMoreResults();
					} catch(Exception e1) {
						throw e1;
					}
					finished = false;
				}
				if(!hasResultset) {
					finished = true;
				}
			}
			while(finished == false);
		} catch(Exception e) {
			e.printStackTrace();
			System.out.println("Error : " + e.getMessage());
			System.out.flush();
		} finally {
			System.out.flush();
		}
	}
}
