import java.sql.*;
public class MariaDbGetColNameBug {
    static String url=null;
    static String driverClass = "org.mariadb.jdbc.Driver";
    static String userId=null;
    static String passwd=null;
    static int nArgs = 0;
    
    public static void main(String args[]) {
    	nArgs = args.length;
    	if (nArgs != 3) {
    		System.out.println("Usage: Java MariaDbGetColName <url> <user id> <password> ");
    		return;
    	}
        try{
        	url = args[0];
        	userId = args[1];
        	passwd = args[2];
        	Class.forName(driverClass);
            Connection conn = DriverManager.getConnection(url, userId, passwd);
            System.out.println("Connected to " + url +
                    " with uid=" + userId +
                    " password=" + passwd);
            
            // Create table for testing
            Statement stmt = conn.createStatement();
            System.out.println("Dropping test table tbl_getcolbug...");
            stmt.execute("DROP TABLE IF EXISTS tbl_getcolbug");
            System.out.println("Dropped the test table.");
            System.out.println("Creating test table tbl_getcolbug(c1 INTEGER)...");
            stmt.execute("CREATE TABLE tbl_getcolbug(c1 INTEGER)");
            System.out.println("Created the test table.");
            System.out.println("Inserting value 10 into column c1 of test table tbl_getcolbug...");
            stmt.execute("INSERT INTO tbl_getcolbug VALUES(10)");
            System.out.println("Inserted the value.");
            stmt.close();
            PreparedStatement pStmt = conn.prepareStatement("SELECT c1 FROM tbl_getcolbug");
            System.out.println("Executing SELECT to get value from coolumn c1 of test table tbl_getcolbug...");
            ResultSet rs = pStmt.executeQuery();
            System.out.println("Executed the SELECT statement...");
            while (rs.next()) {
                System.out.println("Retrieving column value using getInt(\"c1\")...");
            	System.out.println("Retrieved value=" + rs.getInt("c1"));
                System.out.println("Pass test if retrieved value is 10");            	
            }
            rs.close();
            rs = pStmt.executeQuery();
            while (rs.next()) {
                System.out.println("Retrieving value from invalid column using getInt(\"col_xyz\")...");
            	System.out.println("Retrieving Inserted value=" + rs.getInt("col_xyz"));
            }
            rs.close();
        } catch (ClassNotFoundException cne) {
             System.out.println("Error:" + driverClass + ":"
                        + cne.toString());
        } catch (SQLException se) {
             System.out.println("***Error***: Vendor code=" + se.getErrorCode()
             		+ ",SQL State=" + se.getSQLState()
                    + ", error message=" + se.getMessage());
             System.out.println("Pass test if vendor code != 0 and SQL State != null.");
        } catch (Exception e) {
            System.out.println("Error:" + e.getMessage());
        }
    }
}
