File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes multiple uses interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "multiple uses interface" Watch "multiple uses interface" New topic
Author

multiple uses interface

antony magnier
Greenhorn

Joined: Jul 10, 2001
Posts: 11
I am quite new to JDBC and was just wondering how i would create an iterface where the user decides what he/she wants to do for example i have the following code which just simply updates a database but i have more of these programs that do different things such as delete from a table my question is how do i create a interface which lets the user chose what they want to do:
import java.sql.*;
public class Database6 {
Connection con;
// load driver and connect to database
void connectToDB2() {
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
String url = "jdbc b2:SYSCMB11";
String userid = "youuserid";
String password = "yourpassword";
con = DriverManager.getConnection(url, userid, password);
System.out.println("Connected ...");
} catch( Exception e ) {
e.printStackTrace();
}
}

// get some details of a specific employee
void viewEmp(String theEmpId){
try {
System.out.println("... viewing");
Statement stmt = con.createStatement();
String SQLStatement = "SELECT name, hourly_rate FROM xyz "
+ "WHERE emp_id = " + theEmpId;
ResultSet rs = stmt.executeQuery(SQLStatement);
rs.next();
String theName = rs.getString("name");
// note decimal type retrived as a string!
String theHourlyRate = rs.getString("hourly_rate");
System.out.print("the name is : " + theName);
System.out.println(", the rate is : " + theHourlyRate);
rs.close();
stmt.close();
} catch( Exception e ) {
e.printStackTrace();
}
}

// update the hourly rate of a specific employee
void updateEmp(String theEmpId, String theHourlyRate){
try {
System.out.println("... updating");
Statement stmt = con.createStatement();
String SQLStatement = "UPDATE xyz SET hourly_rate = "
+ theHourlyRate + " WHERE emp_id = " + theEmpId;
int rowcount = stmt.executeUpdate(SQLStatement);
System.out.println("... " + rowcount + " updates done");
stmt.close();
} catch( Exception e ) {
e.printStackTrace();
}
}

// disconnect from database
void disconnect(){
try {
con.close();
System.out.println("... disconnecting");
} catch( Exception e ) {
e.printStackTrace();
}
}
}
And...
import java.io.*;
class Program6 {
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
public static void main(String argv[])throws IOException {
Database6 db6 = new Database6();

db6.connectToDB2();
String theEmpId = getParameter1();
String theHourlyRate = getParameter2();
db6.viewEmp(theEmpId);
db6.updateEmp(theEmpId, theHourlyRate);
db6.viewEmp(theEmpId);
db6.disconnect();
}

static String getParameter1() throws IOException {
String theEmpId;
System.out.print("Give employee ID : ");
theEmpId = cin.readLine();
return theEmpId;
}
static String getParameter2()throws IOException {
String theHourlyRate;
System.out.print("Give New hourly rate : ");
theHourlyRate = cin.readLine();
return theHourlyRate;
}
}
Sorry for it being so long but any help would be appreciated.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

There are a couple of different ways. One way would be simply to create a menu in your shell that allows the user numbered choices of the options.
And of course the other way is to create a GUI using Swing or AWT (prefereably SWING in my opinion). At least that's what I did for the HelpDesk Software I created.


GenRocket - A Test Data Generation Platform
antony magnier
Greenhorn

Joined: Jul 10, 2001
Posts: 11
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple uses interface
 
Similar Threads
Hi,I have a problem in database connection
Compile error when trying to call method from another class
load: GetMenuGui.class is not public or has no public constructor.
SQL query not giving any result
simple menu for jdbc