Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Alternative to Enum

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Alternative to Enum

    Okay, on the common library I work on I have a database system, with implementations of MySQL and SQLite.
    Because I like reusing code I use PreparedStatements like this.
    I got the Statement enum:
    public enum Statement {
        // just an example
        SELECT("SELECT '?' FROM '?';");
     
    	private String statement = null;
     
    	Statement(String statement) {
    		this.statement = statement;
    	}
     
    	public String getStatement() {
    		return statement;
    	}
    }
    And then I have the MySQL executeStatement:
    public int executeStatement(Statement statement, Object... args) {
    	try {
    		PreparedStatement ps = conn.prepareStatement(statement.getStatement());
    		statementsPrepared++;
    		Logger.log(LogLevel.DEBUG, String.format("Prepared statement(%s): %s", statementsPrepared, statement.name()));
    		for(int i = 1; i<args.length; i++)
    		{
    			ps.setObject(i, args[i]);
    		}
    		Logger.log(LogLevel.DEBUG, String.format("Set %s objects for statement(%s)", args.length, statementsPrepared));
    		Logger.log(LogLevel.DEBUG, String.format("Executing statement(%s)", statementsPrepared));
    		return ps.executeUpdate();
    	} catch(SQLException e) {
    		Logger.log(LogLevel.ERROR, e.getMessage());
    		return e.getErrorCode();
    	}
    }
    Alright, so here's the thing, as this is a library I don't want to add new statements to the enum all the time, that just won't work.
    I've seen those posts around the web with a kind off dynamic enum, like the one there you implement the values() and valueOf() methods yourself. But they won't allow direct access like a real enum does, and that's what I want. Is there any approach for doing that?

    Thanks in advance.
    Last edited by Doctor X; October 6th, 2012 at 03:50 PM. Reason: Solved


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Alternative to Enum

    Let me understand what you want:

    You want an enum which allows you to access items directly by doing something like Statement.SELECT, but not have the definition of SELECT defined in the enum class?

    It's not possible because Statement.SELECT is evaluated at compile time, and if it's not available the compile will fail.

    Why can't you do something like this? You'll need to provide an external file which you can modify without recompiling your library.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Alternative to Enum

    Thanks for the answer, I have read that thread before but it's not what I wanted to do.
    I ended up changing the Statement class to:
    public abstract class Statement {
     
    	private Object[] parameters = null;
     
    	protected Statement(Object... parameters) {
    		this.parameters = parameters;
    	}
     
    	public final Object[] getParameters() {
    		return parameters;
    	}
     
    	public abstract String getStatement();
    }

Similar Threads

  1. Need an alternative to java.awt.Robot
    By Contrary in forum Java Theory & Questions
    Replies: 11
    Last Post: March 24th, 2012, 09:19 AM
  2. Alternative to thread.sleep?
    By jm24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2012, 01:42 AM
  3. alternative to JMF
    By olimpicco in forum Java Theory & Questions
    Replies: 0
    Last Post: December 19th, 2011, 09:50 PM
  4. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  5. JNI Alternative?
    By janusmccarthy in forum Java Native Interface
    Replies: 1
    Last Post: November 14th, 2009, 12:22 PM