import java.sql.*; //Import Connection, Statement, ResultSet, etc...

public class JDBCTest
{
	//Define DB Property Constants
	public static final String DB_DRIVER="com.mysql.jdbc.Driver";
	public static final String DB_URL="jdbc:mysql://127.0.0.1:3306/test";
	public static final String DB_USERNAME="root";
	public static final String DB_PASSWORD="abcd1234";

	public static void main(String[] args)
	{
		Connection conn=null;
		Statement st=null;
		ResultSet rs;
		int id, rowCnt;
		String name;

		try
		{
			//Initialize the MySQL JDBC Driver
			Class.forName(DB_DRIVER);

			//Create connection to the database
			conn=DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);

			//Create a statement object to use
			//to execute sql statements on the DB.
			st=conn.createStatement();

			//Let's delete ALL records
			//from the test table.
			rowCnt=st.executeUpdate("DELETE FROM test.mytable");

			System.out.println("Rows Deleted: "+rowCnt);

			//Insert some test rows
			st.executeUpdate("insert into test.mytable(id, name) values(1, 'Robert')");
			st.executeUpdate("insert into test.mytable(id, name) values(2, 'Paula')");


			//Select out all rows from the table and print
			rs=st.executeQuery("select id, name from test.mytable");

			rowCnt=1; //Set row count to 1 initially

			while (rs.next())
			{
				id=rs.getInt(1); //index 1 for column 1 which is id
				name=rs.getString(2); //index 2 for column 2 which is name

				System.out.println("Row "+rowCnt+" : ID="+id+", NAME="+name);

				rowCnt++; //Increment row count
			}
		} //End try block
		catch(Exception e)
		{
			//Log any errors...
			e.printStackTrace();
		}
		finally
		{
			if (st!=null)
			{
				try
				{
					st.close();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			if (conn!=null)
			{
				try
				{
					conn.close();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}
