Thursday, February 28, 2008

Maven Plus MySQL Plus JDBC

You can quickly verify your MySQL setup by creating a Maven project and running a small JDBC program.
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <!--
  ========================================================================
  =  This POM created with the following command:                        =
  =  mvn archetype:create -DartifactId=EzMaven -DgroupId=com.ghelton1.db =
  =                                                                      =
  =  then modified to include mysql-connector                            =
  ========================================================================
  -->

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ghelton1.db</groupId>
  <artifactId>EzMaven</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>EzMaven</name>

  <url>http://maven.apache.org
  <!-- JUNIT -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency> 

    <!--  MYSQL -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.8</version>
    </dependency>

  </dependencies>
</project>

package com.ghelton1.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyDB {
 
      public static void main(String[] args) {
            MyDB mydb = new MyDB();
            mydb.connect();
      }

      private void connect() {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {
                  Class.forName("com.mysql.jdbc.Driver");
                  conn = DriverManager.getConnection("jdbc:mysql:///pricing_test", "root", "root");
                  stmt = conn.createStatement();
                  stmt.execute("CREATE TABLE foobar (id MEDIUMINT NOT NULL AUTO_INCREMENT,
                      foo VARCHAR(32) NOT NULL, 
                      bar VARCHAR(32) NOT NULL, 
                      PRIMARY KEY (id))");
                  stmt.execute("insert into foobar (foo, bar) 
                       VALUES ('alpha', 'beta'), ('tres','quatro')");
                  rs = stmt.executeQuery("select * from foobar");
                  while(rs.next()) {
                        System.out.println(rs.getString(2) + " " + rs.getString(3));
                  }
            } catch (ClassNotFoundException exception) {
                  exception.printStackTrace();
            } catch(SQLException sqlex) {
                  sqlex.printStackTrace();
            } finally {
                  try {
                        rs.close();
                  } catch(Exception e) {}
                  try {
                        stmt.close();
                  } catch(Exception e) {}
                  try {
                        conn.close();
                  } catch(Exception e) {}
            }
      }
}
Execute with the following command -
java -classpath .;[.m2 Path\]mysql-connector-java-5.0.8.jar com.ghelton1.db.MyDB