// import database packages
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;

public class Case1 {

	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver"); // load driver

		String db = "ensmartdb";	// stores database name
		String host = "localhost";	// stores name of server where database is located
		String user = "guest";		// stores database username
		String passwd = "guest";	// stores database password 

		StringBuffer conString = new StringBuffer();
		conString.append("jdbc:mysql://");
		conString.append(host);
		conString.append("/");
		conString.append(db);
		conString.append("?user=");
		conString.append(user);
		conString.append("&password=");
		conString.append(passwd);

		Connection con = null;
        	PreparedStatement pstmt = null;
        	ResultSet rs = null;	

		try {
			// now connect to the database
			con = DriverManager.getConnection(conString.toString());
			pstmt = con.prepareStatement("SELECT common_name, species FROM organism");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String common_name = rs.getString("common_name");
				String species = rs.getString("species");
				StringBuffer line = new StringBuffer(common_name);
				line.append(":");
				line.append(species);
				System.out.println(line.toString());
			}
		} finally {
			if (con != null) {
				con.close();
			}
			if (pstmt != null) {
				pstmt.close();
			}
			if (rs != null) {
				rs.close();	
			}
		}
	}
}

