MySQL: access denied for user: (Using password: YES) June 30, 2005
Posted by javafoo in javanotes, jdbc, mysql.13 comments
More experimenting with struts. So I got ambitious, downloaded MySQL and tried to wire a database into my simple struts app. Setting up MySQL was a breeze, although it was my first time, except that some time was wasted googling for user account creation etc.
So, I set up a datasource in Tomcat, which could be looked up by JNDI. I added this in server.xml.
But was getting errors with Context.lookup, indicating there was no “java:comp/env/jdbc” in the Context. Realized I forgot to add the resource-ref in my web descriptor.
This done, I was getting a different error.
Access denied for user ‘blah’: (Using password: YES).
What the …, I didn’t set the password as YES, what the heck was going on?
So googled again (one of these days, when someone asks me what tools, IDE I use for development, I will be sure to add ‘Google’ in there, in fact this should come integrated with the IDE, any Exception or Error message should be linked to Google search), anyway I digress.
So this seems to be a common problem. I tweak the user account a bit in MySQL, apparently the host shouldn’t be set as ‘%’, you are better off setting it to ‘localhost’. I also deleted the default user account ”, which is an empty string. So either of these fixed it for me and I am on my way.
Since I am seeing a lot of searches to this post and one of the commentors (Tyler) rightfully indicates that: The “YES” doesn’t mean that you are using the password “Yes” it simply means “Yes” you are using a password. If the password was blank it would have said “No”
That’s what I thought the issue was to start with (that somehow the password was set to ‘Yes’) or atleast that’s what the MySQL message led me to believe, but later when I tweaked the User Account to set the host, I realized what it was trying to tell me.
Struts action, parameter attribute June 29, 2005
Posted by javafoo in javanotes, struts basic.3 comments
My gripe about this generic attribute called ‘parameter’ in the struts action config.
So I have something like:
path=”/logonProcess”
type=”naive.actions.LogonAction”
name=”logonForm”
parameter=”/pages/Logon.jsp”
validate=”true”
Now someone pray tell me what this ‘paramter’ is supposed to do.
I looked up on the Apache site and it says:
“action general purpose configuration parameter”.
Alright, but what is that supposed to mean. Well it means different things to different action type classes, if it is a ForwardAction it can be the url you want the action to be forwarded to. For DispatchAction it is the key to the different method values. Also you can pass your own parameters that can be available to the action.
Playing with Struts Day 1 June 27, 2005
Posted by javafoo in javanotes, struts basic.add a comment
Ok, so finally after looking at much examples, I thought I had a basic idea and this was too trivial to actually go about trying. But today out of sheer boredem, I exported the struts-blank.war into Tomcat and got hacking at it. So here goes my tryst with struts:
- 2:15 PM Ok then, I edit struts-config.xml: create a simple logonForm extending DynActionForm, add action logon, map the action to logonForm and a LogonAction class, that I will code in a bit.
- 2:30 PM I create the jsps using the tags.
- 2:40 I get interrupted, they want me to do some work actually.
- 3:30 PM Ok where was I? Right the jsps; I write a simple action class: LogonAction, in which I check the username and password against hard-coded values and if they match, forward the page to success, if not forward to failure.
- 3:35 PM I am all compiled and ready to go, I fire up Tomcat and go to the logon page.
- 3:40PM Hurrah! I can actually see the logon page, I hit submit, the page goes to logon.do, but empty page.
- 3:45 PM I think there might be something wrong in the Action class, so I comment the very cryptic username, password checking logic and just forward the page to success. I fire up Tomcat again.
- Fast Forward: a few iterations, debugs, logs and System.out.println later:
- 4:30 PM Still no luck, also anything I log or write to sysout does not show up in the logs, so probably the action calss is not even getting invoked.
- 4:45 PM I tear out my hair, google for “struts action empty page”, there is something about previous Action API requiring you to implement perform and current ones needing you to implement execute.
- 5:00 PM So, sure enough I am impelmenting perform, I just change this to execute and try again, Hallelujah!, I am strutting away to glory.
Moral of the story when in “Action”, “execute”, do not “perform”.
So much for struts.
Inversion of control/Dependency Injection June 24, 2005
Posted by javafoo in IOC/Dependency Injection, Spring, javanotes.add a comment
A very interesting way of looking at Inversion of Control by Craig Walls in his Spring in Action Book:
“You can think of IoC as JNDI in reverse— instead of an object looking up dependencies from a container, the container gives the dependencies to the object at instantiation without waiting
to be asked.”
Article by Martin Fowler.
This is from an article on Spring Framework by Benoy Jose, for quick reference.
There are three types of Dependency Injections.
- Type 1 IOC also called Interface Injection In Type 1 IOC the injection is done though an interface. The interface will define the injection method and the implementation class has to implement this interface and provide concrete implementation for the injection method. Pico Container uses this.
- Type 2 IOC also called Setter Injection In Type 2 IOC the injection is done via a setter method. Type 2 IOC uses setter methods to get the dependent classes it needs. Spring uses this.
- Type 3 IOC also called Constructor Injection. In Type 3 IOC implementing class defines a constructor to get all its dependents. The dependent classes are defined in the constructor arguments. Avalon uses this.
Byte Streams Character Streams June 23, 2005
Posted by javafoo in Java IO, javanotes.add a comment
I can’t believe how people have programmed in Java, myself included, without really understanding how Byte Streams are different from Character Streams. There is an excellent article here
Some pointers:
- If you see
ReaderorWriterthink character stream and if you seeInputStreamandOutputStream think bytes. - Thumb rule: use byte stream for binary data and character stream for everything else.
EJB, again, best practices, patterns June 23, 2005
Posted by javafoo in ejb, javanotes.add a comment
Session Facade
Consider two Entity Beans and a client for ex: Employee and Paycheck. So you find the Employee, create a new Paycheck, populate some of the employee details or based on some values in Employee, you set some values on Paycheck. So you have all this business logic noise all over the client and the overhead of managing the entity beans. Enter Session Facade: You have a Session bean, that has a single method called managePaycheck, all the client has to do is call this method and all the noise moves to the Session bean.
Value Object/Data Transfer Object
Value objects are used to transfer bulk data between tiers. So if you have a remote object (entity bean) and you are calling a multitude of get methods on that object. It makes sense to create a single ValueObject containing all the data needed. So now you are just calling getValueObject on that Entity bean (provided ofcourse the entity bean populates all the fields in the ValueObject first).
Use of dirty flags in the EntityBean
Use dirty flags in ejbStore, basically init this as false in ejbLoad and set it to true in the setter. In ejbStore check this flag and only go ahead if it is true.
More to come…
EJB in two minutes: a refresher June 23, 2005
Posted by javafoo in ejb, javanotes.add a comment
- More detail here
- You have your EJB server, container, EJBs and clients.
- Entity Beans (think persistence), Session Beans (stateful/stateless), Mesage Driven Beans (needs no interface)
EJBs have:
- Home Interface: provides methods for locating, creating, and removing instances of EJB classes, home object is implementation
- Remote Interface: is the client facing interface, it has all the business methods.
- Enterprise Java Bean: Is in the container result of deploying home and remote interface.
- Create home interface by extending EJBHome
- Create Remote interface by extending EJBObject
- Define primary key object.
- Create your Entity Bean implementing EntityBean
Typical lifecycle:
- Locate home object, create or find remote object from home object, apply business methods on remote object
What do you look for in Performance June 23, 2005
Posted by javafoo in java interview, javanotes.add a comment
- When selecting rows from database, is filtering being done by SQL or by java.
- Referencing and de-referencing objects, especially in recursion.
- File processing, are you buffering your reads or reading the entire file into a glob of String.
- Cache read-only data.
- See where the code is spending more amount of time
- More to come…
What do you look for in Code reviews? June 23, 2005
Posted by javafoo in java interview, javanotes.add a comment
We do a lot of this unconsciously during code reviews, but when asked we turn a blank face.
- First the usual suspects, is the code doing what is functionally required, business logic, especially if the team member is new
- See that there are no hard-codes, standards are being followed.
- Is there chance to make something configurable, like lookups. See that these are being read from properties files or the database, that way any new additions don’t need a code release.
- Are connections, jndi lookups being cached.
- Code re-use, both classes and methods.
- Kludge.
- Performance considerations, StringBuffer vs String and so on.
Pass by value vs. Pass by reference June 23, 2005
Posted by javafoo in OOAD, java interview, javanotes.1 comment so far
Always pass by value, if you ever say pass by reference, you will be rejected for sure. (in the interview that is)
You don’t believe me, this guy explains it far more better than I ever can.