我正在尝试将try/catch语句添加到此代码中。我想使用try/catch语句来封装连接数据库,但也要在下面的每个方法中使用它们?虽然我理解如何使用它们的概念,但我对使用try/catch语句还是个新手,对如何将它们与多种方法一起使用有点困惑?
public class Menu_proto
{
static boolean exit;
static final String DB_URL = "jdbc:mysql://localhost/chocolatemilk_review";
static final String USER = "root";
static final String PASS = "";
static final String QUERY = "SELECT account_id, last, first, address, city, state, email FROM Accounts";
static final String INSERT_QUERY = "Add Account";
public static void main (String [] args) {
mainMenuOptions();
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
}
catch (Exception e) {
}
}
public static void mainMenuOptions() {
Scanner input = new Scanner(System.in);
//do while didnt work here? had to put after variable line 32/33
try {
out.println("Choconnoisseur Menu");
out.println();
out.println("Enter 1 for Accounts ");
out.println("Enter 2 for Merchandise ");
out.println("Enter 3 for Merchsales ");
out.println("Enter 4 for Product Reviews ");
out.println("Enter 5 to Exit");
out.println();
out.println("Enter number here: ");
int numEntered = input.nextInt();
input.nextLine();
do {
switch(numEntered) {
case 1:
accountsTable();
out.println("Account Options");
break;
case 2:
merchandiseTable();
out.println("Merchandise Options");
break;
case 3:
merchsalesTable();
out.println("Merchsales Options");
break;
case 4:
producReviewsTable();
out.println("Product Reviews Options");
break;
case 5:
exit = true;
out.println("Have a nice day! ");
out.println();
break;
}
out.println("Choconnoisseur Menu");
out.println();
out.println("Enter 1 for Accounts ");
out.println("Enter 2 for Merchandise ");
out.println("Enter 3 for Merchsales ");
out.println("Enter 4 for Product Reviews ");
out.println("Enter 5 to Exit");
out.println();
out.println("Enter number here: ");
}
while (numEntered != 5);
}
catch (Exception SQLException) {
out.println("Invalid entry");
}
}
public static void accountsTable() {
Scanner input = new Scanner(System.in);
//do while didnt work here? had to put after variable line 32/33
out.println("Accounts Table");
out.println();
out.println("Enter 1 for Add Account");
out.println("Enter 2 for Edit Account");
out.println("Enter 3 for Remove Account");
out.println("Enter 4 for View Account");
out.println("Enter 5 back to Main Menu");
out.println();
out.println("Enter number here: ");
int accountOpts = input.nextInt();
input.nextLine();
do {
switch (accountOpts) {
case 1:
insertInfo();
out.println("Add Accounts");
break;
case 2:
deleteAccounts();
out.println("Edit Accounts");
break;
case 3:
deleteAccounts();
out.println("Remove Accounts");
break;
case 4:
selectAccounts();
out.println("View Accounts");
break;
case 5:
out.println("Back to Main Menu ");
out.println();
break;
}
out.println("Accounts Table");
out.println();
out.println("Enter 1 for Add Account");
out.println("Enter 2 for Edit Account");
out.println("Enter 3 for Remove Account");
out.println("Enter 4 for View Account");
out.println("Enter 5 back to Main Menu");
out.println();
out.println("Enter number here: ");
}
while(accountOpts != 5);
}
private static void insertInfo()
{
}
private static void deleteAccounts()
{
}
private static void selectAccounts()
{
}
private static void merchandiseTable()
{
}
private static void merchsalesTable()
{
}
private static void producReviewsTable()
{
}
}
在类中实现以下两个方法怎么样
public Connection initDatabaseConection(String dbUrl, String user, String password){
Connection conn = null;
try{
conn = DriverManager.getConnection(dbUrl, user, password);
}
catch(SQLException ex){
System.out.println(ex.getMessage());
//Do something else with the error
}
return conn;
}
public void closeDatabaseConnection(Connection conn){
if(conn.isValid(10){
conn.close();
}
}
然后将这些方法实现到执行SQL操作的每个方法中。例如:
private static void insertInfo(){
Connection conn = initDatabaseConection(DB_URL, USER, PASS);
if(conn != null){
//Do database operations
closeDatabaseConnection(conn);
}
}