Thursday, November 15, 2012

Product Review - Ikea Sunnan Solar Desk Lamp

Much like Harbor Freight Tools, Ikea is a business that I have mixed feelings about.  Both sell products at affordable prices.  Sometimes the quality is good enough to get the job done, sometimes it's surprisingly high, and sometimes you end up with the piece of shit you paid for.  Either way, they both fit into an important niche in the economy.
Recently Ikea has begun to sell some solar desk lamps called the Sunnan.  I received two of them for my birthday.  Incidentally - if you buy a Sunnan, another is donated to Unicef.  The idea behind this is that people in developing countries that don't have electricity can use their solar lamps instead of candles and oil lamps, thereby leading toward fewer house fires.
I'd say the build quality is on the high end of average for Ikea stuff.  It's made of your standard molded plastic, but it is pretty thick.  Since it is a desk lamp, this should be more than adequate.  The part that actually emits the light is mounted on a flexible stalk.

As shown below the light output from the thing is far from fantastic:

Given that the thing runs off three AA batteries, this is to be expected.  I'm no electrical engineer, but I can tell you that emitting more light would use more power, and power is a product of voltage and current.  AA batteries can only move so much current for so long.

The charging system for the lamp is pretty novel.  The battery pack + solar panel is a module that sits in the base of the lamp.  It can be removed and placed in a sunny spot to charge.

Given that I live in Oregon and it is November, "sunny" is a relative term.  Regardless, after spending the day in the window the battery packs charged enough to run the lights again for some time.

The Sunnan is an interesting product.  It isn't going to come close to replacing most of the lighting in my house, even my non-solar desk lamp.  I'm probably going to keep using it when I'm goofing around on the computer at night.  It almost feels like a proof of concept - maybe a subsequent version will be made that uses C or D cells and is brighter. 

There's also some immediately obvious hackability.  The way that the battery pack mounts into the base looks compatible with the blade terminal connectors often used in automotive wiring.  It'd be pretty easy to remotely mount the battery pack, or repurpose it for something else.

I guess you could say I'm glad I have these lamps, but I'm glad I didn't buy them either.  For every day use, they just aren't quite there.  With that said, if I was subject to an extended power outage or lived in a flavela with no electricity I would probably feel very different.

Sunday, November 11, 2012

Getting Java to talk to MySQL

Java is a very powerful,  popular, annoyingly verbose object oriented programming language. 

I don't use Java at work or for personal programming (LAMP stuff does what I need there) but I'm slowly chipping away at my CS degree, and I found myself in the middle of a Java project for which a database was the only non-stupid solution.  Figuring out how to get it to talk to MySQL was a pretty easy process.  I'm documenting my results here.

Importing the Libraries
The needed libraries are in the java.sql.* part of the class heirarchy.  You'll need to simply put:
import java.sql*;
at the top of your code.  You'll also need to either have mysql.jar in your CLASSPATH or add it to your build path.  Eclipse can handle this automatically for you, and I'm willing to bet most other IDEs can do the same.

Connecting to the Database

Creating a connection to the database is quite simple.  You simply need to know the host on which MySQL is running, the name of the database, and the username and password.  The example below illustrates this:

try {
            String host = "";
            String dbName = "baseOfData";
            String username = "sqlUsername";
            String password = "sqlPassword"; 
            String hostUrl = "jdbc:mysql://" + host + '/' + dbName;
            dbConn = DriverManager.getConnection(hostUrl, username, password );       
        } catch (SQLException ex) {
            //Handle the errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());

If connecting too the database fails, it will throw an SQLException.  The code in the catch block should print out enough information for you to figure out what went wrong.

Executing Queries
You can prepare and execute queries from Java, just like any other programming language.    Simply building and executing a query looks like this:

PreparedStatement sqlStatement = dbConn.prepareStatement(
                    "select,, p.address,, p.state,, sum(s.cost) " +
                    "from services_provided sp join providers p on = sp.provider_id " +
                    "join services s on sp.service_id = group by"
ResultSet results = sqlStatement.executeQuery();

Please note that this (and all examples) should be done in a try block.

Java supports bind variables as well.  To use bind variables you build the query with wildcards, set them, then run the query as shown below:

PreparedStatement sqlStatement = dbConn.prepareStatement(
                "update services set name = ?, cost = ? where id = ?"   
sqlStatement.setString(1, serviceName);
sqlStatement.setFloat(3, id);

Getting Data Back
Getting data out of the database is quite straight forward as well.   You create a result set from the query output, which you can iterate through.  The results object will have a variety of get methods (getInt, getString, etc) for extracting the needed data as shown below:

PreparedStatement sqlStatement = dbConn.prepareStatement("select * from services");
ResultSet results = sqlStatement.executeQuery();
while ( {
                Map <String,String> service = new HashMap<String,String>();
                service.put("id", Integer.toString(results.getInt(1)));
                service.put("name", results.getString(2));
                service.put("cost", Float.toString(results.getFloat(3)));

Getting data in and out of MySQL in Java is pretty much like it is everywhere else, except more heavily object oriented than in most languages I am used to dealing with.  Despite their differences, programming languages are all more or less the same.  This is why I think it is important to focus on concepts, rather than implementations.