java导入本地资源

tomcat的conf文件夹,在server.xml中的标签内加入

<Context path="/dataResourceImages" docBase="D:\go" crossContext="true" reloadable="false" debug="0"/>  

然后引用

request.setAttribute("mUrl","/dataResour/sqdw2.mp4");
可以使用 Apache POI 库来读取本地 Excel 文件,然后使用 JDBC 连接数据库并将数据插入到数据库中。以下是一个示例代码: ```java import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImport { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "root"; String excelFilePath = "path/to/excel/file.xlsx"; try (Connection conn = DriverManager.getConnection(url, username, password); FileInputStream inputStream = new FileInputStream(excelFilePath); Workbook workbook = new XSSFWorkbook(inputStream)) { Sheet sheet = workbook.getSheetAt(0); String sql = "INSERT INTO mytable (column1, column2, column3) VALUES (?, ?, ?)"; PreparedStatement statement = conn.prepareStatement(sql); int rowNumber = 0; for (Row row : sheet) { if (rowNumber == 0) { rowNumber++; continue; } Cell cell1 = row.getCell(0); String column1 = cell1.getStringCellValue(); Cell cell2 = row.getCell(1); String column2 = cell2.getStringCellValue(); Cell cell3 = row.getCell(2); double column3 = cell3.getNumericCellValue(); statement.setString(1, column1); statement.setString(2, column2); statement.setDouble(3, column3); statement.executeUpdate(); } System.out.println("Data imported successfully!"); } catch (SQLException | IOException e) { e.printStackTrace(); } } } ``` 在这个示例代码中,我们首先建立了数据库连接,并使用 `FileInputStream` 读取了本地 Excel 文件。然后我们使用 `XSSFWorkbook` 类来解析 Excel 文件,并获取第一个工作表。接着,我们使用 `PreparedStatement` 来执行插入数据的 SQL 语句,并遍历 Excel 文件中的每一行,将数据插入到数据库中。最后,我们关闭了所有的资源,并在控制台输出了导入数据成功的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值