java读取txt文件,导入数据库

package com.egf.qingbao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* @author xh
* @version $Revision: 1.1 $
* @since 0.1
*/
public class CopyOfReadTxtutil {
private static Connection conn = null;

public static void readTxtFile(String filePath) {
conn = JdbcUtil.getConnection("oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@xxx.x.x.xxx:1521:tjkf", "ypxx", "ypxx");
PreparedStatement pre = null;
String sql = "insert into yp_tldp values(SEQ_YP_TLDP.nextval,?,?,?,?,?,?,?,?,?,?,?)";
String encoding = "UTF-8";
long startTime = System.currentTimeMillis();
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(file),
encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(read); <
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java读取CSV文件并将其导入到达梦数据库的示例代码: ```java import java.io.BufferedReader;import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class CsvToDatabase { public static void main(String[] args) { String csvFile = "path/to/csv/file.csv"; String jdbcUrl = "jdbc:dm://localhost:5236"; String username = "username"; String password = "password"; String tableName = "table_name"; try { // 创建数据库连接 Connection conn = DriverManager.getConnection(jdbcUrl, username, password); // 准备SQL语句 String sql = "INSERT INTO " + tableName + " VALUES (?, ?, ?)"; PreparedStatement statement = conn.prepareStatement(sql); // 读取CSV文件 BufferedReader reader = new BufferedReader(new FileReader(csvFile)); String line; while ((line = reader.readLine()) != null) { // 解析CSV行 String[] fields = line.split(","); // 设置SQL参数 statement.setString(1, fields[0]); statement.setString(2, fields[1]); statement.setString(3, fields[2]); // 执行SQL语句 statement.executeUpdate(); } // 关闭资源 reader.close(); statement.close(); conn.close(); System.out.println("CSV文件已成功导入数据库中。"); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,您需要将示例代码中的以下变量替换为您自己的值: - csvFile:CSV文件的路径。 - jdbcUrl:达梦数据库的JDBC URL。 - username:数据库用户名。 - password:数据库密码。 - tableName:要将数据插入的表的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值