Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple uses interface

 
antony magnier
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
antony magnier
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic