com.xigole.util.sql
Class Jisql
java.lang.Object
com.xigole.util.sql.Jisql
public class Jisql
- extends java.lang.Object
A simple utility to provide an interactive session with a SQL server. This
application is conceptually modeled on the Sybase 'isql' program with,
obviously, strong similarities to Microsoft SQL/Server isql and osql (as
Microsoft got SQL Server from Sybase).
The program can act in a similar way to Oracle's sqlplus and PostgreSQL's
psql.
A simple command line might look like (this should be all on one line) is:
java -classpath lib/jisql.jar:<file containing native driver>
com.xigole.util.sql.Jisql -user scott -password blah -driver postgresql
-cstring jdbc:postgresql://localhost:5432/scott -c \;
This logs into a PostgreSQL database as the user "scott", password "blah". It
connects to the database named "scott". It uses the command terminator of
";", just like psql or sqlplus (which is escaped in the example so that it
will not be interpreted by the Unix shell). If you do not use this the
default is the term "go" on a single line like Sybase's isql or MS/SQL's
isql/osql. Note that there is a dependency on JOpt Simple in for the base
configuration. Additionally, if you are using the CSVFormatter then it is
dependent on Java CSV.
Options:
- -driver This option allows you to specify the JDBC driver class
name of the driver. There are several shortcuts that can be used:
- jconnect4 - short for
com.sybase.jdbc.SybDriver
- jconnect5 - short for
com.sybase.jdbc2.jdbc.SybDriver
- jconnect6 - short for
com.sybase.jdbc3.jdbc.SybDriver
- oraclethin - short for
oracle.jdbc.driver.OracleDriver
- db2app - the DB2 "App" driver -
COM.ibm.db2.jdbc.app.DB2Driver
- db2net - the DB2 "Net" driver -
COM.ibm.db2.jdbc.net.DB2Driver
- mssql - short for
com.microsoft.jdbc.sqlserver.SQLServerDriver
- cloudscape - short for
COM.cloudscape.core.JDBCDriver
- pointbase - short for
com.pointbase.jdbc.jdbcUniversalDriver
- postgresql - short for
org.postgresql.Driver
- mysqlconj - short for
com.mysql.jdbc.Driver
- the
Connector/J driver for MySQL
- mysqlcaucho - short for
com.caucho.jdbc.mysql.Driver
-
the Caucho driver for MySQL
Alternatively, any class name can be specified here. The shortcuts only exist
for those of us who generate more typos than real text :)
- -cstring This option allows you to specify the connection string
to the database. This string is driver specific but almost always starts with
"jdbc:". Connection strings for the drivers I have tested look
like:
- jconnect4, jconnect5, jconnect6 - Sybase connection strings take
the form "jdbc:sybase:Tds:[hostname]:[port]/[db_name]"
- oraclethin - The Oracle "thin" driver connection string
looks like "jdbc:oracle:thin:@[hostname]:[port]:[oracle sid]"
- db2app - The DB2 "App" driver connection string looks
like "jdbc:db2:[db_name]"
- db2net - The DB2 "Net" driver connection string looks
like "jdbc:db2://[hostname]:[port]/[db_name]"
- mssql - The MS/SQL driver connection string looks like
"jdbc:microsoft:sqlserver://[hostname]:[port]/[db_name]"
- cloudscape - The Cloudscape driver connection string looks like
"jdbc:cloudscape:[db_name];create=true;autocommit=false"
- pointbase - The Pointbase driver connection string looks like
"jdbc:pointbase:server://[hostname]:[port]/[db_name]"
- postgresql - The PostgreSQL driver connection string looks like
"jdbc:postgresql://[hostname]:[port]/[db_name]"
- mysqlconj - The MySQL Connector/J driver connection string looks
like "jdbc:mysql://[hostname]:[port]/[db_name]"
- mysqlcaucho - The MySQL Cahcho driver connection string looks like
"jdbc:mysql-caucho://[hostname]:[port]/[db_name]"
Important - each JDBC vendor has other flags and parameters that can
be passed on the connection string. You should look at the documentation for
your JDBC driver for more information. The strings listed are just a sample
and may change with a new release of the driver. None of these strings are
coded within the application - the list is provided for reference only.
- -user or -u The user name to use to log into the database with.
- -password or -p The password to use to log into the database
with. If this option is missing then the program asks for the password.
- -c The "command terminator" to use. By default this
application uses the string "go" (case insensitive) on a line by
itself to determine when to send the string buffer to the database. You may
specify something else with the -c option. For example, users of Oracle may
prefer either the ";" (semi-colon) character or the "/"
(forwardslash) character as that is what sqlplus uses. This string may occur
as a standalone line or at the end of a particular line.
- -pf Optional file to specify the password. This prevents having to
have it visible when looking at a process status. The first line of the file
is read and used as the password. If both the command line password and this
option are specified the command line password is used.
- -input The name of a file to read commands from instead of
System.in.
- -query An optional single query to run instead of interacting
with the command line or a file. Note - the command must have a
command terminator. So, for example, your command line may be something like
"-c \; -query "select * from blah;". If you do not include the command
terminator then the command will hang, waiting for you to enter the default
"go".
- -debug This turns on some internal debugging code. Not generally
useful.
- -driverinfo Allows you to print some information that the driver
returns. Generally not very useful in all but a few cases.
- -formatter Optionally specify a class name or short cut to format
the output. There are three built in short cuts:
- csv output the data in CSV format.
- xml output the data in XML format.
- default (does not have to be specified) - output the format in the
"normal" format.
Otherwise, this is a class name that implements
com.xigole.util.sql.outputformatter.JisqlFormatter. See the code for more
information on implementing your own output formatter.
The included default formatter supports the following command line options:
- -noheader do not print the header column info.
- -spacer The character to use for "empty" space. This
defaults to the space character. From mrider - "I added the ability to
specify the spacer for columns - which used to be the single char ' '. I did
this because of brain-dead Windows' command line copy/paste. It seems that
when a line of text ends in space, copy does not copy that space. Which makes
it difficult to copy/paste into another program. This can probably be ignored
most of the time."
- -wSpecifies the maximum field width for a column. By default jisql
defaults columns to a maximum width of 2048. By specifying a value
for this jisql with truncate the output of columns that are wider than this
parameter.
- -delimiter Specify a single character delimiter for columns.
- -trim trim the spaces from columns.
- -nonull print an empty string instead of the word "NULL"
when there is a null value.
-
- -left left justify the output
- -debug print debugging information about the result set.
The include CSV formatter supports the following command line options:
- -delimiter specifies the delimiter to use. By default a comma is
used
- -colnames if included then column names are printed as the first
line of output. By default they are not included
The included XML formatter does not have any additional output options.
Copyright (C) 2004-2011 Scott Dunbar (scott@xigole.com)
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
Constructor Summary |
Jisql()
|
Method Summary |
void |
doIsql()
The main loop for the Jisql program. |
static void |
main(java.lang.String[] argv)
Runs Jisql with the command line arguments provided. |
void |
parseArgs(java.lang.String[] argv)
Parse the command line arguments. |
void |
run()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Jisql
public Jisql()
main
public static void main(java.lang.String[] argv)
- Runs Jisql with the command line arguments provided.
run
public void run()
throws java.lang.Exception
- Throws:
java.lang.Exception
doIsql
public void doIsql()
throws java.sql.SQLException
- The main loop for the Jisql program. This method handles the input from
either a command line or from a file. Output is handled through the
Formatter.
- Throws:
java.sql.SQLException
- if an exception occurs.
parseArgs
public void parseArgs(java.lang.String[] argv)
throws java.lang.Throwable
- Parse the command line arguments. This method parses what is needed for
the Jisql driver program and lets the configured formatter do the same.
- Parameters:
argv
- the command line arguments.
- Throws:
java.lang.Exception
- if there are any errors parsing the command line arguments.
java.lang.Throwable