我正在尝试使用Derby中制作的SQL表创建一个简单的生日跟踪程序。它可以在Eclipse中完美运行,但是当我尝试通过可执行JAR运行它时却失败了,但出现错误。我不知道是否无法将数据库包含在JAR中,或者它只是无法访问它,但是我不确定如何创建可运行的Runnable Jar。任何帮助表示感谢,谢谢!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class DatabaseQuery
{
public static final String SQL_STATEMENT = "select * from birthday order by month asc, day asc";
public static final String NAMES_STATEMENT = "select * from birthday order by firstname asc, lastname asc";
public static final String JDBC_URL = "jdbc:derby:practiceDB";
public static final String INSERT_STATEMENT = "insert into birthday values(?,?,?,?)";
public static final String REMOVE_STATEMENT = "delete from birthday where firstname = ? and lastname = ?";
public static final String SEARCH_STATEMENT = "select * from birthday where firstname = ? and lastname = ?";
public static final String EDIT_NAME_STATEMENT = "update birthday set firstname = ?, lastname = ? where firstname = ? and lastname = ?";
public static final String EDIT_BIRTHDAY_STATEMENT = "update birthday set month = ?, day = ? where firstname = ? and lastname = ?";
public static LocalDate localDate = LocalDate.now();
public static DateTimeFormatter df = DateTimeFormatter.ofPattern("MM/dd");
public static String currentDate = localDate.format(df);
public static void main(String[] args) throws SQLException
{
Connection connection = DriverManager.getConnection(JDBC_URL);
int menu;
do
{
menu = menu(connection);
switch (menu)
{
case 0:
printFullList(connection);
break;
case 1:
addRow(connection);
break;
case 2:
removeRow(connection);
break;
case 3:
searchByName(connection);
break;
case 4:
upcomingBirthdays(connection);
break;
case 5:
updateRow(connection);
break;
}
} while (menu != -1);
connection.close();
}
public static void addRow(Connection connection) throws SQLException
{
try
{
String firstName = returnString("Enter First Name: ");
String lastName = returnString("Enter Last Name: ");
int month = returnMonth("Select Month");
int day = returnDay("Select Day", month);
PreparedStatement stat = connection.prepareStatement(INSERT_STATEMENT);
stat.setString(1, firstName);
stat.setString(2, lastName);
stat.setInt(3, month);
stat.setInt(4, day);
stat.executeUpdate();
} catch (Exception e)
{}
}
public static void printFullList(Connection connection) throws SQLException
{
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_STATEMENT);
String input = " All Birthdays\n" + " Today's Date: " + currentDate + "\n ----------------------------------\n";
while (resultSet.next())
{
input += " " + resultSet.getString(1) + " " + resultSet.getString(2)
+ " - " + resultSet.getString(3) + "/" + resultSet.getString(4) + "\n";
}
JOptionPane.showMessageDialog(null, input, "All Birthdays", JOptionPane.PLAIN_MESSAGE);
}
public static void removeRow(Connection connection) throws SQLException
{
String selected = returnName(connection);
String firstname = "";
String lastname = "";
PreparedStatement stat = connection.prepareStatement(NAMES_STATEMENT);
ResultSet checkResultSet = stat.executeQuery();
try
{
while (checkResultSet.next())
{
if (selected.equals(checkResultSet.getString(1) + " " + checkResultSet.getString(2)))
{
firstname = checkResultSet.getString(1);
lastname = checkResultSet.getString(2);
}
}
PreparedStatement dStat = connection.prepareStatement(REMOVE_STATEMENT);
dStat.setString(1, firstname);
dStat.setString(2, lastname);
dStat.executeUpdate();
} catch (Exception e)
{}
}
public static int menu(Connection connection) throws SQLException
{
String[] options = { "List All Birthdays", "Add a Birthday", "Delete a Birthday",
"Check Specific Birthday", "Check Next 3 Birthdays", "Edit an Entry" };
int menu = -1;
String selected = (String) JOptionPane.showInputDialog(null, " ~~~Select an Action~~~", "Birthday Dictionary", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
try
{
for (int n = 0; n < options.length; n++)
if (selected.equals(options[n]))
menu = n;
} catch (Exception e)
{
connection.close();
}
return menu;
}
public static void searchByName(Connection connection) throws SQLException
{
PreparedStatement stat = connection.prepareStatement(NAMES_STATEMENT);
ResultSet checkResultSet = stat.executeQuery();
String selected = returnName(connection);
String firstname = "";
String lastname = "";
try
{
while (checkResultSet.next())
{
if (selected.equals(checkResultSet.getString(1) + " " + checkResultSet.getString(2)))
{
firstname = checkResultSet.getString(1);
lastname = checkResultSet.getString(2);
}
}
PreparedStatement searchStat = connection.prepareStatement(SEARCH_STATEMENT);
searchStat.setString(1, firstname);
searchStat.setString(2, lastname);
ResultSet searchResultSet = searchStat.executeQuery();
ResultSetMetaData searchResultSetMetaData = searchResultSet.getMetaData();
int columnCount = searchResultSetMetaData.getColumnCount();
String output = "";
while (searchResultSet.next())
{
for (int x = 1; x <= columnCount; x++)
{
output += searchResultSet.getString(x) + (x == 3 ? "/" : " ");
}
}
JOptionPane.showMessageDialog(null, output, null, JOptionPane.PLAIN_MESSAGE);
} catch (Exception e)
{}
}
public static void upcomingBirthdays(Connection connection) throws SQLException
{
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_STATEMENT);
String input = " Next 3 Birthdays\n"
+ " Today's Date: " + currentDate + "\n ------------------------------\n";
int count = 0;
while (resultSet.next())
{
if (resultSet.getInt(3) >= localDate.getMonthValue() && count < 3)
{
if (resultSet.getInt(4) >= localDate.getDayOfMonth() || resultSet.getInt(3) > localDate.getMonthValue())
{
input += " " + resultSet.getString(1) + " " + resultSet.getString(2)
+ " - " + resultSet.getString(3) + "/" + resultSet.getString(4) + "\n";
count++;
}
}
}
JOptionPane.showMessageDialog(null, input, "Next 3 Birthdays", JOptionPane.PLAIN_MESSAGE, null);
}
public static void updateRow(Connection connection) throws SQLException
{
String selected = returnName(connection);
PreparedStatement allStat = connection.prepareStatement(NAMES_STATEMENT);
ResultSet checkResultSet = allStat.executeQuery();
String firstname = "";
String lastname = "";
while (checkResultSet.next())
{
if (selected.equals(checkResultSet.getString(1) + " " + checkResultSet.getString(2)))
{
firstname = checkResultSet.getString(1);
lastname = checkResultSet.getString(2);
}
}
String[] choices = { "Name", "Birthday" };
int choice = JOptionPane.showOptionDialog(null, "What would you like to edit?", "", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choices, JOptionPane.DEFAULT_OPTION);
try
{
switch (choice)
{
case 0:
String newFirst = returnString("New First Name: ");
String newLast = returnString("New Last Name: ");
PreparedStatement stat = connection.prepareStatement(EDIT_NAME_STATEMENT);
stat.setString(1, newFirst);
stat.setString(2, newLast);
stat.setString(3, firstname);
stat.setString(4, lastname);
stat.executeUpdate();
break;
case 1:
int month = returnMonth("Updated Month");
int day = returnDay("Updated Day", month);
PreparedStatement bStat = connection.prepareStatement(EDIT_BIRTHDAY_STATEMENT);
bStat.setInt(1, month);
bStat.setInt(2, day);
bStat.setString(3, firstname);
bStat.setString(4, lastname);
bStat.executeUpdate();
break;
}
} catch (Exception e)
{}
}
public static String returnString(String message)
{
return JOptionPane.showInputDialog(null, message, "", JOptionPane.PLAIN_MESSAGE).toLowerCase();
}
public static int returnMonth(String message)
{
String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
String selected = (String) JOptionPane.showInputDialog(null, message, "", JOptionPane.PLAIN_MESSAGE, null, months, JOptionPane.DEFAULT_OPTION);
int month = -3;
for (int n = 0; n < months.length; n++)
if (selected.equals(months[n]))
month = n + 1;
return month;
}
public static int returnDay(String message, int month)
{
int numDays;
switch (month)
{
case 2:
numDays = 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
default:
numDays = 30;
break;
}
String selected;
int selectedNum = -5;
try
{
String[] days = new String[numDays];
for (int i = 0; i < days.length; i++)
days[i] = (i + 1) + "";
selected = (String) JOptionPane.showInputDialog(null, message, "", JOptionPane.PLAIN_MESSAGE, null, days, JOptionPane.DEFAULT_OPTION);
selectedNum = Integer.parseInt(selected);
} catch (Exception e)
{}
return selectedNum;
}
public static String[] returnAllNames(Connection connection) throws SQLException
{
ArrayList<String> namesSetter = new ArrayList<>();
PreparedStatement stat = connection.prepareStatement(NAMES_STATEMENT);
ResultSet fullResultSet = stat.executeQuery();
while (fullResultSet.next())
namesSetter.add(fullResultSet.getString(1) + " " + fullResultSet.getString(2));
String[] names = new String[namesSetter.size()];
for (int i = 0; i < names.length; i++)
names[i] = namesSetter.get(i);
return names;
}
public static String returnName(Connection connection) throws SQLException
{
String[] names = returnAllNames(connection);
String selected = (String) JOptionPane.showInputDialog(null, "Select an Entry", "", JOptionPane.PLAIN_MESSAGE, null, names, JOptionPane.DEFAULT_OPTION);
return selected;
}
}