可以使用Java的JDBC(Java Database Connectivity)來將數組中的數據存入數據庫。以下是一個簡單的示例代碼:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ArrayToDatabase {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "username";
String password = "password";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
int[] data = {1, 2, 3, 4, 5};
String query = "INSERT INTO mytable (column_name) VALUES (?)";
PreparedStatement preparedStatement = conn.prepareStatement(query);
for (int i = 0; i < data.length; i++) {
preparedStatement.setInt(1, data[i]);
preparedStatement.executeUpdate();
}
System.out.println("Data inserted successfully");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
```
在上面的示例代碼中,首先建立了與數據庫的連接,接著定義了一個整型數組`data`,然后通過PreparedStatement將數組中的數據逐個插入到數據庫表中。最后關閉數據庫連接。請根據實際情況修改數據庫連接信息、數據表名稱和字段名。