view example/SQLImport.java @ 931:df18aa5c8a61

Add test for MonetDriver.getPropertyInfo(url, props). The implementation is moved to Parameter.java which contains the list of connection parameters. It currently only returns the mandatory connection parameters.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 24 Oct 2024 19:10:06 +0200 (6 months ago)
parents e890195256ac
children d416e9b6b3d0
line wrap: on
line source
/*
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 2024 MonetDB Foundation;
 * Copyright August 2008 - 2023 MonetDB B.V.;
 * Copyright 1997 - July 2008 CWI.
 */

import java.sql.*;
import java.io.*;

/**
 * This simple example somewhat emulates the mclient command. However in
 * it's simpleness it only supports SQL queries which entirely are on one line.
 *
 * This program reads a file line by line, and feeds the line into a running
 * Mserver5 on the localhost. Upon error, the error is reported and the program
 * continues reading and executing lines.
 * A very lousy way of implementing options is used to somewhat configure the
 * behaviour of the program in order to be a bit more verbose or commit after
 * each (sucessfully) executed line.
 *
 * The program uses a debuglog in which the exact conversation between the
 * JDBC driver and Mserver is reported. The log file is put in the current
 * working directory and names like monet_[unix timestamp].log
 *
 * @author Fabian Groffen
 */

public class SQLImport {
	public static void main(String[] args) throws Exception {
		if (args.length < 1) {
			System.out.println("synopsis: java SQLImport filename [autocommit] [verbose]");
			System.exit(-1);
		}

		// open the file
		BufferedReader fr = new BufferedReader(new FileReader(args[0]));

		// request a connection suitable for MonetDB from the driver manager
		// note that the database specifier is currently not implemented, for
		// MonetDB itself can't access multiple databases.
		// turn on debugging
		Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/demo?debug=true", "monetdb", "monetdb");

		boolean beVerbose = false;
		if (args.length == 3) {
			// turn on verbose mode
			beVerbose = true;
		}
		if (args.length < 2) {
			// disable auto commit using the driver
			con.setAutoCommit(false);
		}

		// get a statement to execute on
		Statement stmt = con.createStatement();

		String query;
		for (int i = 1; (query = fr.readLine()) != null; i++) {
			if (beVerbose)
				System.out.println(query);
			try {
				// execute the query, no matter what it is
				stmt.execute(query);
			} catch (SQLException e) {
				System.out.println("Error on line " + i + ": " + e.getMessage());
				if (!beVerbose)
					System.out.println(query);
			}
		}

		// free resources, close the statement
		stmt.close();
		// close the connection with the database
		con.close();
		// close the file
		fr.close();
	}
}