wood burning stoves 2.0*
The moose likes Java in General and the fly likes Basic Java Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Basic Java Help" Watch "Basic Java Help" New topic
Author

Basic Java Help

James dOnn
Greenhorn

Joined: Apr 21, 2011
Posts: 1
Hi,

I need to add a basic menu to the code so once the user logs in the get 5 choices. The code at the minute run prefectly fine with no issues but how do I break it down so its controlled by the user slecting option 1 or option 2.

I know i need to separate the code into methods but not sure how?

1 Create Table
2 Input users into new table
3 Run query1
4 query2
5 query3
6 query 4
7 1st prepared statement
8 2nd prepared statement
9 callable procedure.

Thanks in advance.

Code:

import java.sql.*;
import java.io.*;

class events_database_mysql {
public static void main (String args [])
throws SQLException, IOException {

// the following statement loads the MySQL jdbc driver

try {
Class.forName ("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println ("Could not load the driver");
}

String user, pass, host;
user = readEntry("userid : ");
pass = readEntry("password: ");
host = readEntry("hostname or ip address: ");
// userid, password and hostname are obtained from the console

Connection conn = DriverManager.getConnection
("jdbc:mysql://"+host+":3306/events_database", user, pass);

/* JDBC default is to commit each SQL statement as it is sent to the database. Setting autocommmit=false changes the default
behaviour so that transactions have to be committed explicity.
*/
conn.setAutoCommit(false);

// Creating a statement lets us issue commands against the connection.

Statement s = conn.createStatement();

// Creating and populating Actor table

s.executeUpdate("create table IF NOT EXISTS Audit( auid INT(11) PRIMARY KEY, Details CHAR(200), Recommendations CHAR(200))");
System.out.println("Created table Audit");

String query = "insert into Audit values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(query);
String Details = readEntry("Details : ");
ps.setString(1, Details);
String Recom = readEntry("Recommendations: ");
ps.setString(2, Recom);

conn.commit();
System.out.println("Inserted some actors");


ResultSet result=s.executeQuery("Select * From events WHERE events.total_occupancy >25 AND events.type = 'Round Table' ");
System.out.println("Results: ");
while(result.next()) {
System.out.println(result.getString(1) +" "+ result.getString(2) + " "+result.getString(3) +" "+ result.getString(4)+" "+ result.getString(5) );
}
ResultSet result1=s.executeQuery("Select * From stakeholder WHERE stakeholder.organisation = 'NIW' ");
System.out.println("Next Results: ");
while(result1.next()) {
System.out.println(result1.getString(1) +" "+ result1.getString(2) + " "+result1.getString(3) +" "+ result1.getString(4)+" "+ result1.getString(5) );
}
ResultSet result2=s.executeQuery("Select COUNT(*) AS total From stakeholder GROUP BY stakeholder.organisation ");
System.out.println("Next Results: ");
while(result2.next()) {
System.out.println (result2.getString(1));
}
ResultSet result3=s.executeQuery("Select * From events WHERE events.Total_Attending > 10 GROUP BY events.type ");
System.out.println("Next Results: ");
while(result3.next()) {
System.out.println(result3.getString(1) +" "+ result3.getString(2) + " "+result3.getString(3) +" "+ result3.getString(4)+" "+ result3.getString(5) );
}

{
String query1 = "UPDATE employee SET employee.department = ? WHERE employee.ename = ? ";
PreparedStatement ps1 = conn.prepareStatement(query1);
String Department = readEntry("Dep:");
ps1.setString(1, Department);
String Name = readEntry("Dep:");
ps1.setString(2, Name);
ps1.executeUpdate();
}
{
String query2 = "UPDATE events SET events.type = ? WHERE events.Event_Name = ? ";
PreparedStatement ps2 = conn.prepareStatement(query2);
String Attending = readEntry("Dep:");
ps2.setString(1, Attending);
String name = readEntry("Dep:");
ps2.setString(2, name);
ps2.executeUpdate();
}



// We end the transaction and the connection.

conn.commit();

// Calling a procedure with no procedure parameters
CallableStatement cs1 = conn.prepareCall("Call mypro()");
ResultSet res = cs1.executeQuery();
System.out.println("Results of called procedure: ");
while(res.next()) {
System.out.println(res.getString(1) +" "+ res.getString(2) + " "+res.getString(3) +" "+
res.getString(4) );
}

conn.commit( );

conn.close();
}

//readEntry function -- to read input string
static String readEntry(String prompt) {
try {
StringBuffer buffer = new StringBuffer();
System.out.print(prompt);
System.out.flush();
int c = System.in.read();
while(c != '\n' && c != -1) {
buffer.append((char)c);
c = System.in.read();
}
return buffer.toString().trim();
} catch (IOException e) {
return "";
}
}
}
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

You need to separate the code by making each operation as a method. You may consider separating your code into the following methods. You may add more methods if required.



S.D. MADHAN
Not many get the right opportunity !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic Java Help