有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从菜单执行SQL语句

Java新手-

尝试编写一个程序,在其中选择执行SQL语句的选项

因此,在菜单上输入“1”将运行 SELECT * FROM fishing data语句

但是,当我运行这个命令时,SQL语句会在菜单打印之前运行。我尝试过将菜单放在许多不同的位置,甚至尝试过在eclipse中“清理”文件夹

任何帮助都会很好。谢谢

package testingSQLstatements;

import java.util.*;
import java.sql.*;

public class Fishdata {
public static Scanner scan = new Scanner(System.in);

public static int menu() {
    System.out.println("1.Print all catchlog data.");
    System.out.println("2.Search for a trip by season");
    System.out.println("3.Print out the number of catches you have.");
    System.out.println("4.Export catch log to Excel");
    System.out.println("5.Display statistics");
    System.out.println("5.Quit.");
    System.out.println("Enter your menu choice:");
    int value = scan.nextInt();
    return value;
}

private static final String DATABASE_URL = "jdbc:mysql://localhost:8889/SQLStatementTest";
private static final String USERNAME = "root";
private static final String PASSWORD = "toor";

private Connection connection = null;;
private PreparedStatement selectAllFish = null;
private PreparedStatement selectBySeason = null;
private PreparedStatement insertNewTrip = null;
private PreparedStatement averageFishCaught = null;

public Fishdata() {
    try {
        connection = DriverManager.getConnection(DATABASE_URL, USERNAME,
                PASSWORD);
        selectAllFish = connection
                .prepareStatement("SELECT * FROM fishingdata");

        selectBySeason = connection
                .prepareStatement("SELECT * FROM fishingdata WHERE season = ? ");

        insertNewTrip = connection
                .prepareStatement("INSERT INTO fishingdata "
                        + "(season, type_of_water, type_of_bait, type_of_line, num_fish_caught ) "
                        + "VALUES (?, ?, ?, ?, ?)");
        averageFishCaught = connection
                .prepareStatement("SELECT avg(num_fish_caught) FROM fishingdata");

    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
        System.exit(1);
    }
}

public List<Fish> getAllFish() {
    List<Fish> results = null;
    ResultSet resultSet = null;

    try {
        resultSet = selectAllFish.executeQuery();
        results = new ArrayList<Fish>();

        while (resultSet.next()) {
            results.add(new Fish(resultSet.getString("season"), resultSet
                    .getString("type_of_water"), resultSet
                    .getString("type_of_bait"), resultSet
                    .getString("type_of_line"), resultSet
                    .getInt("num_fish_caught")));
        }
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    } finally {
        try {
            resultSet.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
            close();
        }
    }
    return results;
}

public List<Fish> getSelectBySeason(String name) {
    List<Fish> results = null;
    ResultSet resultSet = null;

    try {
        selectBySeason.setString(1, name);

        resultSet = selectBySeason.executeQuery();

        results = new ArrayList<Fish>();

        while (resultSet.next()) {
            results.add(new Fish(resultSet.getString("season"), resultSet
                    .getString(" type_of_water"), resultSet
                    .getString(" type_of_bait"), resultSet
                    .getString(" type_of_line"), resultSet
                    .getInt("num_fish_caught")));
        }
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    } finally {
        try {
            resultSet.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
            close();
        }
    }

    return results;
}

public int addFish(String season, String type_of_water,
        String type_of_bait, String type_of_line, int num_fish_caught) {
    int result = 0;

    try {
        insertNewTrip.setString(1, season);
        insertNewTrip.setString(2, type_of_water);
        insertNewTrip.setString(3, type_of_bait);
        insertNewTrip.setString(4, type_of_line);
        insertNewTrip.setInt(5, num_fish_caught);

        result = insertNewTrip.executeUpdate();
    }

    catch (SQLException sqlException) {
        sqlException.printStackTrace();
        close();
    }

    return result;

}

public void close() {
    try {
        connection.close();
    }

    catch (SQLException sqlException) {
        sqlException.printStackTrace();
    }
}

}

共 (0) 个答案