Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从菜单执行SQL语句_Java_Sql - Fatal编程技术网

Java 从菜单执行SQL语句

Java 从菜单执行SQL语句,java,sql,Java,Sql,Java新手- 尝试编写一个程序,在其中选择执行SQL语句的选项 因此,在菜单中输入1将运行 从钓鱼数据语句中选择* 但是,当我运行此命令时,SQL语句会在菜单打印之前运行。我尝试过将菜单放在许多不同的位置,甚至尝试过清理eclipse中的文件夹 任何帮助都会很好。谢谢 package testingSQLstatements; import java.util.*; import java.sql.*; public class Fishdata { public static Scann

Java新手-

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

因此,在菜单中输入1将运行 从钓鱼数据语句中选择*

但是,当我运行此命令时,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();
    }
}

}

实际上,您并没有在代码中的任何地方调用menu方法。下面的代码是运行应用程序的主代码的示例。请注意,使用枚举可能比使用像这样的幻数更好,但这只是一个示例

public static void main (String[] args) {
    Fishdata fishdata = new Fishdata();
    int choice = menu();
    switch (choice) {
    case 1:
        System.out.println(fishdata.getAllFish()); // or whatever you want to do with the list
        break;
    case 2:
        // etc.
    default:
        break;
    }
}

我一点也没有出错。它只是直接连接到数据库并执行SELECT语句。我试图让它在执行任何操作之前等待我从菜单中的输入。你的公共静态无效字符串[]args代码在哪里?你如何调用这个类?