具有多种方法的 Java try/case 语句

发布于 2025-01-10 17:11:04 字数 4978 浏览 1 评论 0原文

我试图将 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()
    {


    }

}

Im trying to add try/catch statements to this code. I would like to use use the try/catch statement in order to encapsulate connecting the database but also use them within each method below? While I understand the concept of how to use them, I am new to using try/catch statements and a little confused on how to use them with multiple methods?

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()
    {


    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2025-01-17 17:11:04

在您的类中实现以下两个方法如何?

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);
    }
    
}

How about implementing the two following methods in your class

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();
    }
}

Then implement these methods into each method where you perform SQL operations. For example:

private static void insertInfo(){

    Connection conn = initDatabaseConection(DB_URL, USER, PASS);
    
    if(conn != null){
        //Do database operations
        closeDatabaseConnection(conn);
    }
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文