diff example/MJDBCTest.java @ 226:5ea126405bac

Update java examples code
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 03 May 2018 18:44:55 +0200 (2018-05-03)
parents c38d4eaf5479
children 51e886d7f3b4 529b92d09fc6
line wrap: on
line diff
--- a/example/MJDBCTest.java
+++ b/example/MJDBCTest.java
@@ -17,15 +17,24 @@ import java.sql.*;
  */
 public class MJDBCTest {
 	public static void main(String[] args) throws Exception {
-		// make sure the driver is loaded
-		Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
-		// turn on debugging (disabled)
-		//nl.cwi.monetdb.jdbc.MonetConnection.setDebug(true);
-		Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/notused", "monetdb", "monetdb");
+		String MonetDB_JDBC_URL = "jdbc:monetdb://localhost:50000/demo";	// change host, port and databasename
+		Connection con;
+		try {
+			con = DriverManager.getConnection(MonetDB_JDBC_URL, "monetdb", "monetdb");
+		} catch (SQLException e) {
+			System.err.println("Failed to connect to MonetDB server! Message: " + e.getMessage());
+		}
+
+		if (con == null) {
+			System.err.println("Failed to create a connection object!");
+			return;
+		}
+
 		Statement st = con.createStatement();
 		ResultSet rs;
 
-		rs = st.executeQuery("SELECT a.var1, COUNT(b.id) as total FROM a, b WHERE a.var1 = b.id AND a.var1 = 'andb' GROUP BY a.var1 ORDER BY a.var1, total;");
+		String sql = "SELECT a.var1, COUNT(b.id) as total FROM a, b WHERE a.var1 = b.id AND a.var1 = 'andb' GROUP BY a.var1 ORDER BY a.var1, total;";
+		rs = st.executeQuery(sql);
 		// get meta data and print columns with their type
 		ResultSetMetaData md = rs.getMetaData();
 		for (int i = 1; i <= md.getColumnCount(); i++) {
@@ -49,39 +58,51 @@ public class MJDBCTest {
 		// we ask the database for 22 rows, while we set the JDBC driver to
 		// 5 rows, this shouldn't be a problem at all...
 		rs = st.executeQuery("select * from a limit 22");
+		int var1_cnr = rs.findColumn("var1");
+		int var2_cnr = rs.findColumn("var2");
+		int var3_cnr = rs.findColumn("var3");
+		int var4_cnr = rs.findColumn("var4");
 		// read till the driver says there are no rows left
 		for (int i = 0; rs.next(); i++) {
-			System.out.print("[" + rs.getString("var1") + "]");
-			System.out.print("[" + rs.getString("var2") + "]");
-			System.out.print("[" + rs.getInt("var3") + "]");
-			System.out.println("[" + rs.getString("var4") + "]");
+			System.out.println(
+				"[" + rs.getString(var1_cnr) + "]" +
+				"[" + rs.getString(var2_cnr) + "]" +
+				"[" + rs.getInt(var3_cnr) + "]" +
+				"[" + rs.getString(var4_cnr) + "]" );
 		}
-		
-		// this close is not needed, should be done by next execute(Query) call
+
+		// this rs.close is not needed, should be done by next execute(Query) call
 		// however if there can be some time between this point and the next
 		// execute call, it is from a resource perspective better to close it.
-		//rs.close();
+		rs.close();
 		
 		// unset the row limit; 0 means as much as the database sends us
 		st.setMaxRows(0);
 		// we only ask 10 rows
 		rs = st.executeQuery("select * from b limit 10;");
+		int rowid_cnr = rs.findColumn("rowid");
+		int id_cnr = rs.findColumn("id");
+		var1_cnr = rs.findColumn("var1");
+		var2_cnr = rs.findColumn("var2");
+		var3_cnr = rs.findColumn("var3");
+		var4_cnr = rs.findColumn("var4");
 		// and simply print them
 		while (rs.next()) {
-			System.out.print(rs.getInt("rowid") + ", ");
-			System.out.print(rs.getString("id") + ", ");
-			System.out.print(rs.getInt("var1") + ", ");
-			System.out.print(rs.getInt("var2") + ", ");
-			System.out.print(rs.getString("var3") + ", ");
-			System.out.println(rs.getString("var4"));
+			System.out.println(
+				rs.getInt(rowid_cnr) + ", " +
+				rs.getString(id_cnr) + ", " +
+				rs.getInt(var1_cnr) + ", " +
+				rs.getInt(var2_cnr) + ", " +
+				rs.getString(var3_cnr) + ", " +
+				rs.getString(var4_cnr) );
 		}
 		
-		// this close is not needed, as the Statement will close the last
+		// this close is not required, as the Statement will close the last
 		// ResultSet around when it's closed
 		// again, if that can take some time, it's nicer to close immediately
 		// the reason why these closes are commented out here, is to test if
 		// the driver really cleans up it's mess like it should
-		//rs.close();
+		rs.close();
 
 		// perform a ResultSet-less query (with no trailing ; since that should
 		// be possible as well and is JDBC standard)
@@ -90,12 +111,12 @@ public class MJDBCTest {
 		// support returning the affected rows.
 		st.executeUpdate("delete from a where var1 = 'zzzz'");
 
-		// closing the connection should take care of closing all generated
-		// statements from it...
 		// Don't forget to do it yourself if the connection is reused or much
 		// longer alive, since the Statement object contains a lot of things
 		// you probably want to reclaim if you don't need them anymore.
-		//st.close();
+		st.close();
+		// closing the connection should take care of closing all generated
+		// statements from it...
 		con.close();
 	}
 }