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 :(得分:0)
您实际上并未在代码中的任何位置调用menu
方法。下面的代码将是运行您的应用程序的主要代码的示例。请注意,使用Enum
而不是使用&#34;魔术数字&#34;可能会更好。像这样,但它只是一个例子。
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;
}
}