package com.fr.submit;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import com.fr.data.JobValue;
import com.fr.data.TotalSubmitJob;
import com.fr.script.Calculator;

public class TestSubmit extends TotalSubmitJob {

	private static final long serialVersionUID = -1L;

	public String getJobType() {
		return " ";
	}

	protected void doTotalJob(Data data, Calculator calculator) throws Exception {
		Connection conn = this.getConnection(); // 连接数据库
		try {
			conn.setAutoCommit(false);
			if (data != null && data.getRowCount() > 0) {
				System.out.println("================TestSubmit==================");
				for (int i = 0; i < data.getRowCount(); i++) {
					Object ob = data.getValueAt(i, 0);
					if (ob instanceof JobValue) {
						Statement st = conn.createStatement();
						JobValue did = (JobValue) ob;
						JobValue actQty = (JobValue) data.getValueAt(i, 1);

						if (actQty.getValueState() == JobValue.VALUE_STATE_CHANGED) { //修改更新
							String upd = "UPDATE tablename SET ActQty=" + actQty.getValue() + " WHERE ID=" + did.getValue(); //sql语句
							System.out.println("upd: " + upd);
							st.executeUpdate(upd); //执行更新
						}
						st.close();
					}
				}
				conn.commit(); // 提交
			}
		} catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
			throw new Exception(e.toString()); // 前台返回异常
		} finally {
			conn.close();
		}
	}

	public Connection getConnection() {

		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String url = "jdbc:sqlserver://localhost:1433;databaseName=db";
		String username = "sa";
		String password = "123";
		Connection con = null;
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return con;
	}
}