How do I use enum in switch statement?

This example show you how to use enumeration or enum type in a switch statement.

package org.kodejava.example.fundamental;

enum RainbowColor {
    RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
}

public class EnumSwitch {
    public static void main(String[] args) {
        RainbowColor color = RainbowColor.INDIGO;

        EnumSwitch es = new EnumSwitch();
        String colorCode = es.getColorCode(color);
        System.out.println("ColorCode = #" + colorCode);
    }

    public String getColorCode(RainbowColor color) {
        String colorCode = "";

        // We use the switch-case statement to get the hex color code of our
        // enum type rainbow colors. We can pass the enum type as expression
        // in the swith. In the case statement we only use the enum named
        // constant excluding its type name.
        switch (color) {
            // We use RED instead of RainbowColor.RED
            case RED:
                colorCode = "FF0000";
                break;
            case ORANGE:
                colorCode = "FFA500";
                break;
            case YELLOW:
                colorCode = "FFFF00";
                break;
            case GREEN:
                colorCode = "008000";
                break;
            case BLUE:
                colorCode = "0000FF";
                break;
            case INDIGO:
                colorCode = "4B0082";
                break;
            case VIOLET:
                colorCode = "EE82EE";
                break;
            default:
                break;
        }
        return colorCode;
    }
}

How do I insert a record into database table?

In this example you’ll learn how to create a program to insert data into a database table. To insert a data we need to get connected to a database. After a connection is obtained you can create a java.sql.Statement object from it, and using this object we can execute some query string.

package org.kodejava.example.sql;

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

public class SelectStatementSample {
    public static void main(String[] args) throws Exception {
        Connection connection = null;
        try {
            // Register MySQL JDBC driver class.
            Class.forName("com.mysql.jdbc.Driver");

            // Get a connection to database.
            String url = "jdbc:mysql://localhost/kodejava";
            String user = "kodejava";
            String password = "kodejava123";
            connection = DriverManager.getConnection(url, user, password);

            // Create a statement object.
            Statement stmt = connection.createStatement();

            String sql = "INSERT INTO m_users (username, first_name, " +
                    "last_name, address) VALUES ('fbar', 'Foo', " +
                    "'Bar', 'Sunset Road 123')";

            // Call an execute method in the statement object 
            // and pass the query.
            stmt.execute(sql);
        } catch (ClassNotFoundException e) {
            System.err.println("Could not load database driver!");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
}