问题描述
我试图让我的程序能够运行多次.这意味着如果我在我的程序中输入一些东西然后关闭它,当我启动它时,在 MySQL 的帮助下,该信息仍然存在.我对 Java 和 MySQL 还很陌生,但这非常重要.我的程序运行良好,但我不知道如何让 MySQL 保存数组列表.我已经建立了包含我需要的一切的数据库.到目前为止,这是我的 MySQL 桥:
I am trying to get my program to be able to run over multiple runs. Meaning if I input something in my program and then shut it down, when I start it back up that info will still be there with the help of MySQL. I am fairly new to Java and MySQL but this is very important.My program works perfectly but I have no idea how to get MySQL to hold the arraylist. I already have the database set up with everything I need. This is my MySQL bridge so far:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SQL {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://instance23389.db.xeround.com:15296/Inventory","user","password");
PreparedStatement Statement = con.prepareStatement("Select * from name");
ResultSet result = Statement.executeQuery();
{
while(result.next())
{
}
}
}
还有这一行:
Class.forName("com.mysql.jdbc.Driver");
throws "Syntax error on token ""com.mysql.jdbc.Driver"", delete this token"
而且我已经为桥导入了正确的文件.
and I have already imported the correct files for the bridge.
我也尝试了以下代码.它运行时没有错误,但它不会从数据库中导入信息.我从那里去哪里?
I also tried the following code. It runs with no errors but it isn't importing the info from the database. Where do I go from there?
public static void main(String[] args) throws SQLException
{
Connection con = DriverManager.getConnection("jdbc:mysql://instance23389.db.xeround.com:15296/Inventory","user","password");
PreparedStatement Statement = con.prepareStatement("Select * from inventory");
ResultSet result = Statement.executeQuery();
while(result.next()) {
// do something with the row
}
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int x=0;
int choice;
do{
do
{
System.out.println("Please select what you would like to do:\n1 = Add New\n2 = Update Inventory\n3 = Lookup Inventory\n4 = Make Changes\n5 = Totals");
choice=input.nextInt();
if(choice<1 || choice>5)
{
System.out.println("Please enter a number 1-5.");
}
else
{
}
}while(choice<1 || choice>5);
if(choice==1)
{
Product_Chart.newInv();
}
else if(choice==2)
{
Product_Chart.updateInv();
}
else if(choice==3)
{
Product_Chart.lookupInv();
}
else if(choice==4)
{
Product_Chart.MakeChanges();
}
else if(choice==5)
{
Product_Chart.Totals();// TODO does not call Product_Chart.Totals();
}
else
{
System.out.println("Error 101");
}
}while(x==0);
}
}
推荐答案
本声明:
Class.forName("com.mysql.jdbc.Driver");
位于类的顶层 - 您通常会在其中拥有变量声明、方法或构造函数.你可能想把它放在一个静态初始化器中:
is at the top level of the class - where you'd normally have variable declarations, methods, or constructors. You might want to put it in a static initializer:
static {
Class.forName(...);
}
...虽然我认为这一步已经有一段时间没有必要了.(基本上,驱动程序发现过程得到了改进.)
... although I thought that this step had been unnecessary for quite some time. (The driver discovery process has improved, basically.)
至于让 MySQL 持有 ArrayList" - 您需要插入、更新或删除数据库的内容以匹配您的数据.这是一个过于广泛的主题,无法在一个答案中涵盖,但您应该阅读有关如何修改数据以及仅选择数据的教程.
As for getting MySQL to "hold the ArrayList" - you need to insert, update or delete the contents of the database to match your data. It's too broad a topic to cover in a single answer, but you should read tutorials about how to modify data as well as just selecting it.
这篇关于Java MySQL 与 ArrayLists 的集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!