This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Basic Java Help

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "";
}
}
}
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic