aspose file tools*
The moose likes JDBC and the fly likes Jenny JDBC Code Generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Jenny JDBC Code Generation" Watch "Jenny JDBC Code Generation" New topic
Author

Jenny JDBC Code Generation

Ron Zavner
Greenhorn

Joined: Nov 28, 2010
Posts: 20
Hi,

I installed jenny and run it on my DB...it generated the code!, nice!
The problem is that it doesnt compile due to an error on the beginning of each file...on a part which supposed to be for test purposes?
/** Exposed for unit testing purposes only! */
Implementation()
{
super( DBFacade.getInstance() , tableName );
}

The super call doesn't compile. Any idea?

Thanks,
Ron.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Can you provide the compiler error message?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Ron Zavner
Greenhorn

Joined: Nov 28, 2010
Posts: 20
The constructor TableFacade(DBFacade.Implementation, String) is undefined.
I included the jr.jar in the classpath and it recognize the TableFacade so the problem is only with the constructor.

Thanks,
Ron.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Hmmm, I'm wondering if you should be including jenny-prod.jar instead.

Checking ....
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Yep, you are grabbing the wrong jar file.

Take a look at your generated source file. It should have an import similar to:


However the jr.jar file just happens to have a TableFacade in a different package:


It is the jenny-prod.jar file that has it in the correct package for what you need:

You should have the jenny-prod.jar file from when you downloaded the complete jenny package.
Ron Zavner
Greenhorn

Joined: Nov 28, 2010
Posts: 20
Ok thanks...downloaded this prod file but now it doesnt recognize TableFacade at all.
I tried also to re-genrate but it didn't work...any idea?

Thanks.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

This sounds like a classpath issue.

How are you compiling the code? From the command line ("javac -cp ..."), using a build tool (ant or maven), using an IDE (Eclipse, NetBeans, ...)?

Assuming you are using the command line, what is the command you are typing in?
Ron Zavner
Greenhorn

Joined: Nov 28, 2010
Posts: 20
I'm using Eclipse...
Right click on the project...build path...Library...add Jar - added both of them (prod and dev).
Thanks.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

OK, but do you still have jr.jar in your classpath? (And you will probably want to add your database connector at some point at some point).

I just created a dummy project to see how it would look for me, and my Package Explorer window looks like:



To get that, I ran the following on the command line from the base directory of my project:

Notes:
  • Normally this would all be entered on one line, but this is already likely to have issues with overflowing screen width, so I split it where there is whitespace
  • I use Unix pathnames referencing where I have the various libraries installed - if you are using Windows you may have to change your paths accordingly.


  • Based on what I have above, is there something that looks different in your Package Explorer?

    How are you trying to generate the files? From the command line, or from the external tools of Eclipse?
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Hi,

    I changed the command line to run also from the dev package but still it doesn't work.

    This is the command line:

    set CLASSPATH=D:\Ron\dev\projects\JennyCodeGeneration\src\jr.jar;D:\Ron\dev\projects\JennyCodeGeneration\src\jenny-dev.jar;D:\Ron\dev\projects\JennyCodeGeneration\src\jenny-prod.jar;D:\Ron\dev\projects\JennyCodeGeneration\src\mysql-connector-java-5.1.13-bin.jar;
    java com.javaranch.jenny.dev.Jenny db.properties

    Also attached screenshot of eclipse.

    Thanks again for the effort


    [Thumbnail for 1.JPG]

    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Hi!

    I think i found the problem...
    I defined in the db.properties Facade=DBFacade and it probably mixed up with another DBFacade...change it now and i don't get compilation errors...

    By the way, another question - how can I run a query with select count(*), sum...group by etc?

    Thanks a lot,
    Ron.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    Cool, glad you got it all worked out.

    For running arbitrary SQL, there is an executeQuery and executeUpdate method in your built facade. Depending on your needs, you might want to create a utility class around them. For example:

    Note that the example above is something I am using in a very specific use-case where I am getting a single value that I know always exists. As such, I have taken a large number of shortcuts that I would never put into production code.
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Ok great, thank you very much!
    Ron.
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Hi again!

    I'm trying to do an insert from a web application with jenny...using struts2 but I don't think it matters.
    When I call row.insert() it just get stuck and doesnt continue to the next row.
    O call the insert method without passing any connection...and I didn't set any connection parameters...
    Followed the example here - http://www.javaranch.com/jenny/tutorial4.jsp

    Should I pass any parameters? should i initialize a connection before? how can I check why is it stuck?
    Debugger couldn't get into the jar file.

    Thanks,
    Ron.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    You shouldn't need any connection parameters.

    I would recommend that you set up a very simple test case for yourself and see if that works, before diving into a full blown Struts / Web application. For example:

    I can verify that this code works on my computer, so I know that inserts are working. If I tried to run this in a separate application and it failed, then I would at least have some level of certainty that this code was not the problem.

    By the way - have you looked in your database logs to see whether the database is trying to do something?
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Hi,

    Right so I get this exception.

    Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.javaranch.jenny.prod.DBFacade.getConnection(DBFacade.java:213)
    at com.javaranch.jenny.prod.DBFacade.insertAndPossiblyGetNewID(DBFacade.java:960)
    at com.javaranch.jenny.prod.DBFacade.insert(DBFacade.java:1108)
    at com.javaranch.jenny.prod.TableFacade.insert(TableFacade.java:130)
    at com.db.seo.WebsitesTable$Row.insert(WebsitesTable.java:335)
    at com.db.seo.TestDB.doWork(TestDB.java:13)
    at com.db.seo.TestDB.main(TestDB.java:7)

    Any clue why is the connection not set?

    Thanks,
    Ron.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    Interesting - that very first line tells you the problem - you do not have permissions to your MySQL database without a username/password combination. Which is interesting in that it presumably worked in the past - so why is it now asking for a username and password? Have you changed systems or databases or ...?

    A really bad way to fix this would be to allow anonymous access to your database.

    The correct way to fix this is to enter a username and password for a user who has the appropriate privileges for reading and writing to the tables you are trying to access.

    I'm not sure about the following - I am 99% sure it will work, I am just not sure that it is the right thing to do. (I'll ask for more expert help.)

    Take a look at your facade (it will be in the com.db.seo package). Around about line 17 it should have a line similar to:

    I'm betting those last 2 parameters are blank on your system - if you change them to your username and password then you will have connectivity.

    This however just feels completely wrong to me. Hardcoding usernames and passwords is (IMHO) wrong. Changing generated code is (IMHO) wrong. So therefore I think that this cannot be the right way to do it. I'll see if we can get better advice.
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Ok, thanks very much. Really appreciate it.
    Actually they were just blank...they weren't there at all - just the first 2 parameters were there.
    Adding them indeed solve the problem but I agree - generated code should not be changed.

    Question on another issue - would you recommend using Jenny or Hibernate?

    Thanks,
    Ron.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    Ron Zavner wrote:Question on another issue - would you recommend using Jenny or Hibernate?

    Yes.



    Seriously, it depends on a number of factors, so there is no "one right answer". Complexity of the project, number of people maintaining the code, amount of work you would like to happen behind the scenes (which can be both good and bad), and so on. All these things can affect which data storage mechanism you use.
    Lester Burnham
    Rancher

    Joined: Oct 14, 2008
    Posts: 1337
    Ron Zavner wrote:Question on another issue - would you recommend using Jenny or Hibernate?

    I haven't used Jenny, but it sounds as if it's based on source code generation. That's really a concept that went out of fashion a long time ago, especially if you have to keep doing it for each database you might want to use. Apache Torque worked like that, and it made working with any software built on top of it -like the Apache JetSpeed 1 portal- a royal pain. These days, bytecode enhancement -as used by other persistence frameworks- obviates the need for that.

    To keep some architectural freedom, I'd opt for using the JPA API instead of Hibernate directly, though. That way you can switch between JPA providers (of which Hibernate is one of several) as necessary.
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Hi,

    Ok thanks. Actually I found another issue when integrating with Struts2 - using the ModelDriven...it works when submitting the form (it saves the values to the generated bean) but when populating it...it doesnt pass the data to the UI - maybe it's because the static class that jenny generated...
    So I think I'm going to check Hibernate - if you have any ideas I'll be glad to hear

    Thanks,
    Ron.
    Jeanne Boyarsky
    author & internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 30764
        
    156

    Ron Zavner wrote:Question on another issue - would you recommend using Jenny or Hibernate?

    Is anyone other than you going to need to maintain the software? If so, I would go with Hibernate. The reason being many people know Hibernate/JPA. A lot less people know Jenny.


    [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Ron Zavner
    Greenhorn

    Joined: Nov 28, 2010
    Posts: 20
    Right but even if it's only me who is going to maintain it - who knows what's going to be the next issue to come? and assuming i can't resolve it easily...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Jenny JDBC Code Generation