This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Using a java program as a command line... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Using a java program as a command line..." Watch "Using a java program as a command line..." New topic

Using a java program as a command line...

Keyser Logue

Joined: Mar 08, 2006
Posts: 8
Hello, I have written a program that will access a postgres database using jdbc.

It has a default query of Select * From Members, so that when it runs it will select everything from the members table and display it in the java screen that appears

My problem is, I then want this to work with a MYSQL database as well. Will I need to install a different JDBC or is there something I can change in the code

Basically, it is to produce one java program that can query both postgres and mysql, does anyone have any tips and/or help
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63195

You just need to load the approriate driver, and make sure that the SQL syntax you use is common to both databases.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Keyser Logue

Joined: Mar 08, 2006
Posts: 8
Excellent, thankyou...

The only other point is, once I have typed in the command (for example Select * From FirstNames) I want to have a submit button which will process that command.

How do I set it so that the query is passed to the database, and also is there a way of setting it so that if it is an invalid query, an error message is returned?
Rachil Chandran
Ranch Hand

Joined: Mar 05, 2006
Posts: 67
u could catch possible SQLExceptions that are thrown when your query is passed and display some message. Hope i got ur questions right

SCJP2 96%
Keyser Logue

Joined: Mar 08, 2006
Posts: 8
Thanks for the replies, one last thing:

The main thing I want is a log in screen, you know something like this:

Driver name (may use a drop down for this)

Database URL: (Text field??)



Once they are filled in, I want it to somehow process it when I press a submit button. How should I do that
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Not really database specific, but you could pass the information as command line parameters or provide a simple Dialog window using Swing.

Check out the Swing Tutorial, which should give you and idea of what you want to do. In particular look at JFrame and JDialog.

Swing Tutorial

Dialog Window Tutorial


Have you checked out Aspose?
subject: Using a java program as a command line...
It's not a secret anymore!