arraylist导入mysql,与Java的的ArrayList整合的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())

{

}

}

}

and this line:

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);

}

}

解决方案

This statement:

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.)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要使用Java实现将Excel数据导入MySQL数据库,可以按照以下步骤进行: 1. 首先需要导入相关的Java库,包括Apache POI用于读取Excel文件中的内容,以及JDBC用于连接和操作MySQL数据库。 2. 使用Apache POI库读取Excel文件。可以使用HSSFWorkbook类来表示整个Excel文件,然后通过HSSFSheet和HSSFRow来遍历每个工作表和每一行。 3. 在读取Excel文件时,可以通过HSSFCell获取每个单元格的值。根据Excel文件的结构和数据需求,可以自定义解析单元格的逻辑。 4. 将读取到的数据存储在Java对象中,可以使用ArrayList或其他数据结构。 5. 建立与MySQL数据库的连接,使用JDBC的相关API。需要提供数据库的URL、用户名和密码等信息。 6. 创建数据库表,在MySQL中执行CREATE TABLE语句。可以使用PreparedStatement预编译SQL语句,确保数据的安全性和可扩展性。 7. 将读取到的数据插入到MySQL数据库中,可以使用INSERT语句。通过PreparedStatement设置参数,将Java对象中的数据传递给SQL语句。 8. 执行INSERT语句,将数据写入MySQL数据库。 9. 关闭资源,包括关闭Excel文件和数据库连接等。 以上就是使用Java实现Excel数据导入MySQL数据库的基本步骤。实际操作中需要考虑异常处理、数据校验和转换等问题,以保证数据的完整性和准确性。同时也可以根据需求进行扩展,例如支持多个工作表、数据更新和删除等操作。 ### 回答2: 要使用Java实现将Excel数据导入MySQL数据库,可以按照以下步骤进行: 1. 使用Java中的Apache POI库读取Excel文件。Apache POI是一个开源库,可以用于读取和写入Microsoft Office文档格式,包括Excel文件。通过POI库,可以获取Excel文件中的每个单元格的数据。 2. 创建一个Java对象来表示MySQL数据库连接。可以使用JDBC驱动程序来连接MySQL数据库。可以通过使用`java.sql`包中的`Connection`、`Statement`和`ResultSet`等类,来建立与数据库的连接和执行SQL语句。 3. 在Java中解析Excel文件的数据,并将其存储在适当的数据结构中。根据Excel文件的结构,可以将数据存储在Java数组、列表或实体类对象中。 4. 根据数据结构中的数据,编写SQL插入语句。可以使用Java中的字符串操作方法,或者使用SQL模板引擎来创建插入语句。 5. 在Java中执行SQL插入语句,将Excel数据插入到MySQL数据库中。可以使用`Statement`对象的`executeUpdate()`方法来执行插入语句。 6. 最后,关闭数据库连接和Excel文件,释放资源。 注意事项: - 在导入数据之前,需要确保MySQL数据库已经正确地设置和配置,并且在Java项目中添加了MySQL驱动。 - 需要处理Excel文件的格式和结构,确保文件中的表格和数据符合预期。 - 可以使用异常处理机制来处理可能发生的错误,并提供适当的错误处理和日志记录。 通过以上步骤,可以实现将Excel数据导入MySQL数据库的功能。使用Java编写代码,可以灵活地处理不同类型的Excel文件和数据库表格,提高数据导入的效率和准确性。 ### 回答3: 要通过Java实现Excel数据导入MySQL数据库,可以按照以下步骤进行: 1. 首先,需要导入相关的Java库,如Apache POI用于解析Excel文件,以及MySQL驱动程序用于连接和操作MySQL数据库。 2. 在Java代码中,使用POI库来读取Excel文件的内容。可以使用Workbook类来加载Excel文件,并通过Sheet和Row类来遍历和读取工作表和行。 3. 在读取Excel数据时,可以根据需要进行数据处理和验证。例如,可以检查数据的有效性、格式是否正确,并将数据存储在Java对象中。 4. 英九读取Excel数据后,可以使用MySQL驱动程序来连接到MySQL数据库。将数据库连接信息提供给Java代码,以建立与数据库的连接。 5. 创建适当的数据库和表以存储Excel数据。可以使用MySQL Workbench或命令行工具来创建和管理数据库和表结构。 6. 使用Java代码将Excel数据插入到MySQL数据库中。可以使用PreparedStatement来执行插入操作,并将从Excel中读取的数据绑定到查询参数中。 7. 在数据插入过程中,可以通过事务管理来确保数据的完整性和一致性。通过使用事务,可以在一次操作中插入所有数据,并在失败时回滚所有操作。 8. 最后,检查和验证数据插入是否成功。可以使用SQL查询来检查插入的数据,并与Excel文件中的数据进行对比。 通过以上步骤,可以实现将Excel数据导入MySQL数据库的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值