java 代码实现将字符串保存到word中。 java 实现读取sql 文件批量插入

本文介绍了如何使用Java编程语言从文本文件中读取SQL语句,然后通过Oracle数据库连接执行批量插入。主要展示了outputString2方法用于处理UTF-8编码的字符串,并在main方法中实现了文件读取和数据库操作的整合。
摘要由CSDN通过智能技术生成
	public static void main(String[] args) throws ParseException {
     
        outputString1(  "123", new File("C:/Users/mz/Desktop/test.doc"));

	}
	
	
	public static void outputString1(String str, File file) {
		try {
		PrintWriter out = new PrintWriter(file);
		byte bytes[] = str.getBytes();
		int i = 0;
		while ((bytes.length - i) != 0) {
		out.write(bytes[i]);
		i++;
		}
		out.close();
		} catch (FileNotFoundException e) {
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
	

  

  //中文不会乱码

	public static void outputString2(String str, File file) {
		try {
    	FileOutputStream out = new FileOutputStream(file);
		out.write(str.getBytes("UTF-8") );
		out.close();
		} catch (FileNotFoundException e) {
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}

 java 实现读取sql 文件批量插入

 package com.kingdee.eas.custom.xzkingdeemz.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.kingdee.bos.BOSException;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fm.common.FMIsqlFacadeFactory;
import com.kingdee.eas.fm.common.IFMIsqlFacade;
 
public class OracleConnection {
	private static final String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
 	private static String DATABASE_URL = "jdbc:oracle:thin:@192.168.91.135:1521:myora";;
	private static String DATABASE_USER = "kingdee";
	private static String DATABASE_PASSWORD = "125937";
	 
	
//	private static String DATABASE_URL = "jdbc:oracle:thin:@10.123.69.161:1521:KDDB1";;
//	private static String DATABASE_USER = "kingdee";
//	private static String DATABASE_PASSWORD = "kingdee";
 
	public static Connection getConn() {
		try {
			 Class.forName(DRIVER_CLASS);
			 Connection dbConnection = DriverManager.getConnection(
					DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);
			 return dbConnection;
		} catch (SQLException e) {
			 e.printStackTrace();
		} catch (ClassNotFoundException e) {
		 e.printStackTrace();
		}
	     return null;
	}

	public static void closeAll(Connection conn, PreparedStatement pstmt,
			ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pstmt != null) {
				pstmt.close();
			}
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	public static void main(String[] args) throws FileNotFoundException {
	       	 Connection conn = null;
		     PreparedStatement ps = null;
	         conn = getConn();
	        // String fileName="C:/Users/mz/Desktop/环保集团数据迁移/新盛-环保资料/供应商基本资料分类明细.txt" ;
	         String fileName="C:/Users/mz/Desktop/test.txt" ;
	         //读取文件
	         BufferedReader br = null;
	         StringBuffer sb = null;
	         try {
	            br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); //这里可以控制编码
	            sb = new StringBuffer();
	            String line = null;
	            while((line = br.readLine()) != null) {
	                sb.append(line);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                br.close();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }   
	        }
	        String data = new String(sb);  
	        String[] split = data.split(";");
	     
		try {
		for (int i = 0; i < split.length; i++) {
			 conn.setAutoCommit(false);
			 String sql=  split[i].toString() ;
			 if(sql.contains("insert")){
				 System.out.println(i+sql);
				 ps = conn.prepareStatement( sql);
			     boolean execute = ps.execute() ;
			     ps.close(); 
			 }
		}	
		conn.commit();
		conn.close();
		} catch (SQLException e) {
			 e.printStackTrace();
		}
	  System.out.println("导入结束。。。");
	 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值