用java 代码,动态修改,拼接sql、执行

1、用java 代码,动态修改,拼接sql、执行


waybillNo.sql 文件中内容

425787921414,
425404118389,
425067648257,


2、链接数据库


package test;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class DBHelper {
    public static final String url = "jdbc:mysql://172.0.0.0/test";  
    public static final String name = "com.mysql.jdbc.Driver";  
    public static final String user = "root";  
    public static final String password = "test";  
  
    public Connection conn = null;  
    public PreparedStatement pst = null;  
  
    public DBHelper(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接  
            pst = (PreparedStatement) conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    public void close() {  
        try {  
            this.conn.close();  
            this.pst.close();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    } 
}

3、

/**
 * 
 */
/**
 * @author 
 *
 */
package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProducedRouter {

	static String sql = null;
	static DBHelper db1 = null;
	static ResultSet ret = null;

	public static void queryData(String sql) {// 查询数据

		db1 = new DBHelper(sql);// 创建DBHelper对象
		try {
			ret = db1.pst.executeQuery();// 执行语句,得到结果集
			while (ret.next()) {
				String uid = ret.getString(1);
				String ufname = ret.getString(2);
				String ulname = ret.getString(3);
				String udate = ret.getString(4);
				System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate);
			} // 显示数据
			ret.close();
			db1.close();// 关闭连接
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void addDataSql(String addsql) {//插入数据
		
		addsql = "INSERT INTO `ts_userName` VALUES (123508,'2019-7-04 20:13:07', 425, 'CN','425174082999','700', '', '700', '44552', '30', NULL, '2019-07-04', '10:44:00', 1, '', '', 'ASssDD', '', '', '700010A9999', '201907041650', '', '', NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', '', '', '', '', '', '', '', '', 2, '2019-07-04 20:13:08', 0, NULL, 99, NULL, '', '', '', '', '');";
		db1 = new DBHelper(addsql);// 创建DBHelper对象
		try {
			db1.pst.execute();// 执行语句,得到结果集
			//ret.close();
			db1.close();// 关闭连接
			System.out.println("已执行完sql ...");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void readWaybillFile(String path) {// 读取文件

		path = "src/waybillNo.sql";
		File file = new File(path);
		InputStream input = null;
		BufferedReader buffReader = null;

		try {
			input = new FileInputStream(file);
			InputStreamReader reader = new InputStreamReader(input);
			buffReader = new BufferedReader(reader);
			String tempLine = null;
			String[] arrStr = {};
			List result = new ArrayList();
			int id = 0;
			String dataSql;
			while ((tempLine = buffReader.readLine()) != null) {
				arrStr = tempLine.split(",");
				result.add(arrStr[0]);
				id = id + 1;
				dataSql = "INSERT INTO `ts_userName` VALUES (" + id + ", '2019-7-04 10:44:07', 425, 'CN'," + arrStr[0]+ ", '700', '', '700', '44552', '30', NULL, '2019-07-04', '10:44:00', 1, '', '', 'ABSSS', '', '', '700A010A9999', '201907041112', '', '', NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', '', '', '', '', '', '', '', '', 2, '2019-07-04 10:44:08', 0, NULL, 99, NULL, '', '', '', '', '');";
				System.out.println(dataSql);
			}
			System.out.println(result.size());
			input.close();
			reader.close();
			buffReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void writerSql() {//写入文件
		int id = 0;
		String dataSql;
		File outputFile = new File("src/outputdata.sql");
		OutputStream output = null;
		List result = new ArrayList();//运单集合
		try {
			for (String orderno : result) {
				id = id + 1;
				dataSql = "INSERT INTO `ts_userName` VALUES (" + id + ", '2019-7-04 10:44:07', 425, 'CN'," + orderno
						+ ", '700', '', '700', '44552', '30', NULL, '2019-07-04', '10:44:00', 1, '', '', 'ABSS', '', '', '700010A9999', '201907041112', '', '', NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', '', '', '', '', '', '', '', '', 2, '2019-07-04 10:44:08', 0, NULL, 99, NULL, '', '', '', '', '');";
				System.out.println(dataSql);
				output = new FileOutputStream(outputFile, true);
				byte[] b = dataSql.getBytes();
				// 将字符转换成字节输出保存
				output.write(b);
				String str = "\r\n";
				output.write(str.getBytes());
				output.flush();
				System.out.println("写入到文件中,写入内容:" + dataSql);
				output.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		sql = "SELECT * FROM test t  ORDER BY t.id DESC LIMIT 10;\r\n" + "";// SQL语句
		//queryData(sql);
		//readWaybillFile("src/data.sql");
		addDataSql("");

	}

}

4、outputdata.sql

INSERT INTO `ts_test` VALUES (2, '2019-7-04 10:44:07', 425, 'CN',425404118389, '750', '', '750', '44552', '30', NULL, '2019-07-04', '10:44:00', 1, '', '', 'System', '', '', '755A010A9999', '201907041112', '', '', NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', '', '', '', '', '', '', '', '', 2, '2019-07-04 10:44:08', 0, NULL, 99, NULL, '', '', '', '', '');

 

你可能感兴趣的:(Java,开发资料)