Changeset: 5ea126405bac for monetdb-java URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=5ea126405bac Modified Files: example/MJDBCTest.java example/PreparedExample.java example/SQLImport.java example/SQLcopyinto.java Branch: default Log Message:
Update java examples code diffs (153 lines): diff --git a/example/MJDBCTest.java b/example/MJDBCTest.java --- 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(); } } diff --git a/example/PreparedExample.java b/example/PreparedExample.java --- a/example/PreparedExample.java +++ b/example/PreparedExample.java @@ -15,8 +15,6 @@ import java.sql.*; */ public class PreparedExample { public static void main(String[] args) throws Exception { - // make sure the driver is loaded - Class.forName("nl.cwi.monetdb.jdbc.MonetDriver"); Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/notused", "monetdb", "monetdb"); PreparedStatement st = con.prepareStatement("SELECT ? AS a1, ? AS a2"); ResultSet rs; diff --git a/example/SQLImport.java b/example/SQLImport.java --- a/example/SQLImport.java +++ b/example/SQLImport.java @@ -37,8 +37,6 @@ public class SQLImport { // open the file BufferedReader fr = new BufferedReader(new FileReader(args[0])); - // make sure the driver is loaded - Class.forName("nl.cwi.monetdb.jdbc.MonetDriver"); // request a connection suitable for Monet from the driver manager // note that the database specifier is currently not implemented, for // Monet itself can't access multiple databases. diff --git a/example/SQLcopyinto.java b/example/SQLcopyinto.java --- a/example/SQLcopyinto.java +++ b/example/SQLcopyinto.java @@ -22,8 +22,6 @@ import nl.cwi.monetdb.mcl.io.*; public class SQLcopyinto { public static void main(String[] args) throws Exception { - // make sure the driver is loaded - Class.forName("nl.cwi.monetdb.jdbc.MonetDriver"); // request a connection suitable for Monet from the driver manager // note that the database specifier is currently not implemented, for // Monet itself can't access multiple databases. _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list