背景:
公司的K3系统管理员在日常操作中发现公司员工变动频繁导致他老是要在系统中为新员工开通账号,建立新员工之后,还需要去系统中将新用户添加到审批层级中去,十几个单据流程要修改,非常麻烦与耗时。博主听了之后,还是祭出了老办法,用mssql的数据跟踪工具经过几次测试,找到了K3中用户权限变更在数据库操作的规律,于是便有了下面这段简单的java代码。
这段代码最关键的还是通过跟踪数据库查到几张相关表,之后的复制操作就简单了。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
//project 中添加ojdbc6_g.jar
public class CopyOfNewUserAuthorized {
public static void main(String args[]){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL =
"jdbc:sqlserver://192.168.0.249:1433; DatabaseName= AIS20120530223839"; //连接服务器和数据库
String userName = "sa"; //默认用户名
String userPwd = "k3"; //密码
String user = "'somebody'";//somebody是用户名1
String userLike = "'anybody'";//anybody是用户名2
String FUserID = null;
Connection dbConn;
try{
Class.forName(driverName); //建表后先commit才能在其它端中调用
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println( "数据库已连接! ");
ArrayList<Integer> AuthorizationFBillType = new ArrayList<Integer>();
ArrayList<Integer> AuthorizationFCheckLevel = new ArrayList<Integer>();
//拿到参照用户的审核权限,审核单据和审核节点
Statement stmt = dbConn.createStatement(); //建表时的插入语句记得为' ',而不是" "
String strSql = "select a.FBillType,a.FCheckLevel" +
" from t_MultiLevelCheck a left join t_User b on a.FCheckMan = b.FUserID WHERE FName = "
+ userLike;//将sql中将;去掉
System.out.println(strSql);
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
System.out.println("执行到rs");//执行测试
System.out.println(rs.getString("FBillType") + " " + rs.getString("FCheckLevel"));
AuthorizationFBillType.add(rs.getInt("FBillType"));
AuthorizationFCheckLevel.add(rs.getInt("FCheckLevel"));
System.out.println(AuthorizationFBillType.size());
}
//得到新用户user的ID
Statement stmt2 = dbConn.createStatement(); //建表时的插入语句记得为' ',而不是" "
String strSql2 = "select FUserID from t_User where FName = " + user;
System.out.println(strSql2);
ResultSet rs2 = stmt2.executeQuery(strSql2);
while(rs2.next()){
System.out.println("执行到rs2");//执行测试
System.out.println(rs2.getString("FUserID"));
FUserID = rs2.getString("FUserID");
System.out.println(FUserID);
}
Statement stmt3 = dbConn.createStatement(); //建表时的插入语句记得为' ',而不是" "
String strSql3 = "select a.FBillType,a.FCheckLevel" +
" from t_MultiLevelCheck a left join t_User b on a.FCheckMan = b.FUserID WHERE FName = "
+ userLike;//将sql中将;去掉
System.out.print(strSql3);
ResultSet rs3 = stmt3.executeQuery(strSql);
for(int i = 0;i < AuthorizationFBillType.size();i++){//i是count(xmssbm)
String strIns="INSERT INTO t_MultiLevelCheck (FBillType,FCheckLevel,FCheckMan) VALUES" +
" (" +AuthorizationFBillType.get(i) + "," + AuthorizationFCheckLevel.get(i) + ","
+ FUserID + ");";
System.out.println(strIns);
stmt.execute(strIns);//项目导入完成后,去掉前面的// 然后执行即可。
}
}
catch(Exception e){
e.printStackTrace();
}
System.exit(0);
}
}