/*
 * [ConnectJDBCPreparedStatement.java]
 *
 * Summary: Demonstrate the how to connect with JDBC and use PreparedStatement.
 *
 * Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2007-09-17
 */
package com.mindprod.example;

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

import static java.lang.System.*;

/**
 * Demonstrate the how to connect with JDBC and use PreparedStatement.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2007-09-17
 * @since 2007-09-17
 */
public class ConnectJDBCPreparedStatement
    {
    /**
     * which database
     */
    private static final String DATABASENAME = "squirrels";

    /**
     * class name of the JDBC driver
     */
    private static final String DRIVERCLASSNAME = "com.mysql.jdbc.Driver";

    /**
     * access PASSWORD
     */
    private static final String PASSWORD = "sesame";

    /**
     * login name of user
     */
    private static final String USERNAME = "charlie";

    /**
     * The connection.  Handle to the database
     */
    private static Connection conn;

    /**
     * connect to the database
     */
    private static Connection connect() throws SQLException
        {
        // get JDBC connection to MySQL database.
        // Jar containing the Connector/J JDBC driver: mysql-connector-java-5.1.10-bin.jar must be downloaded and
        // placed on the classpath.
        // You must configure MySQL to allow external TCP/IP connections or else localhost won't work either.
        // Instantiate the MySQL JDBC driver. Using ClassForName/newInstance
        // rather than new means the driver need not be present at compile time.
        // Oddly, we need not retain a reference to the Driver object,
        // or even do a newInstance(), just load the class.
        try
            {
            Class.forName( DRIVERCLASSNAME );
            }
        catch ( Exception e )
            {
            err.println( "can't load MySQL Server driver: " + e.getMessage() );
            }
        return DriverManager.getConnection( "jdbc:mysql://localhost/" + DATABASENAME,
                USERNAME,
                PASSWORD );
        }

    /**
     * initialise the database
     *
     * @param args not used
     */
    public static void main( String[] args ) throws SQLException
        {
        conn = connect();
        // using PreparedStatement to insert a record
        PreparedStatement stmt = conn.prepareStatement( "INSERT INTO nutcaches VALUES (?,?,?)" );
        stmt.setString( 1, "butternuts" /* type */ );
        stmt.setInt( 2, 40 /* count of nuts */ );
        stmt.setBoolean( 3, true /* flawless nut */ );
        stmt.executeUpdate();
        // using PreparedStatement to do a query
        PreparedStatement fetch = conn.prepareStatement( "SELECT * FROM nutcaches WHERE nutcount > ?" );
        fetch.setInt( 1, 12 /* nuts wanted */ );
        ResultSet r = fetch.executeQuery();
        while ( r.next() )
            {
            final String nutType = r.getString( 1 );
            final int nutCount = r.getInt( 2 );
            final boolean quality = r.getBoolean( 3 );
            } // end while
        conn.close();
        }
    }