aspose file tools*
The moose likes JDBC 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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using a java program as a command line..." Watch "Using a java program as a command line..." New topic
Author

Using a java program as a command line...

Keyser Logue
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

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
Greenhorn

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
Greenhorn

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??)

Username

Password

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

regards

Jason
 
Consider Paul's rocket mass heater.
 
subject: Using a java program as a command line...
 
Similar Threads
How to get total records
Postgres JDBC trouble
heterogeneous select???
iBatis groupBy spoils SQL order by
query consisting of if statements, looping, cases on mysql?