log.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 527 行 · 第 1/2 页
JAVA
527 行
package jianguan;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.text.*;
import java.util.Properties;
public class log{
private ResultSet rs;
private Connection con;
private Statement stmt;
public void Init(String data_source){
try{
DatabaseMetaData dma;
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con=DriverManager.getConnection("jdbc:odbc:"+data_source,"sa","");
// checkForWarning(con.getWarnings());
Properties prop = new Properties();
prop.put("user", "sa");
prop.put("password", "");
prop.put("server", "localhost");
prop.put("port", "1433");
prop.put("db", "zqjk");
prop.put("charset", "GB2312");
Class.forName("weblogic.jdbc.mssqlserver4.Driver");
con = DriverManager.getConnection("jdbc:weblogic:mssqlserver4", prop);
dma=con.getMetaData();
System.out.println("\nConnected to "+dma.getURL());
System.out.println("Driver "+dma.getDriverName());
System.out.println("Version "+dma.getDriverVersion());
System.out.println("");
}
catch(ClassNotFoundException e){
System.out.println("ClassNotFoundExeption");
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
}
////checkFor Warning
/* private static boolean checkForWarning(SQLWarning warn) throws SQLException{
boolean rc=false;
if (warn != null){
System.out.println("\n***Warning***\n");
rc=true;
while(warn != null){
System.out.println("SQLState:"+warn.getSQLState());
System.out.println("Message:"+warn.getMessage());
System.out.println("Vendor:"+warn.getErrorCode());
System.out.println("");
warn=warn.getNextWarning();
}
}
return rc;
}
*/
////oper query
public ResultSet Sql(String sql){
Statement stmt;
ResultSet rs=null;
try{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("\n***SQLException caught22222 ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return rs;
}
///add a record
public int Add(String add){
int i=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement(add);
i=pstm.executeUpdate();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
public int Add(String type,String describe){
int i=0;
Date ndate = new Date();
String logtime=DateFormat.getDateTimeInstance().format(ndate);
PreparedStatement pstm;
try{
pstm=con.prepareStatement("insert into log(type,describe,logtime) values ('"
+type+"','"+describe+"','"+logtime+"')");
i=pstm.executeUpdate();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
//add for infomation
public int Add(String DeptName,String Unit,String address,String classname,String running,String ok){
int i=0;
Date ndate = new Date();
String logtime=DateFormat.getDateTimeInstance().format(ndate);
ResultSet rs=null;
PreparedStatement pstm;
tableSql tt;
tt=new tableSql();
tt.tableInit("zqjk");
String insert="";
String DepartmentName="",UnitName="",Hostname="",AgentName="",AgentWorkStatus="",AgentConnectionStatus="";
try{
// pstm=con.prepareStatement("Update AgentStatusInfo set AgentWorkStatus="+"'"+running+"'");
// pstm=con.prepareStatement("Update AgentStatusInfo set HostName="+"'"+address+"'"+","+"AgentName="+"'"+classname+"'"+","+"AgentWorkStatus="+"'"+running+"'"+","+"AgentConnectionStatus="+"'"+ok+"'"+","+"WritingTime="+"'"+logtime+"'");
//
rs=tt.recordSql("select * from AgentStatusInfo where HostName="+"'"+address+"'");
if (!rs.next())
{
insert=insert+"'"+DeptName+"'"+","+"'"+Unit+"'"+","+"'"+address+"'"+","+"'"+classname+"'"+","+"'"+running+"'"+","+"'"+ok+"'"+","+"'"+logtime+"'"+","+'0';
System.out.println("the insert :"+insert);
tt.recordInsert("insert into AgentStatusInfo(DepartmentName,UnitName,Hostname,AgentName,AgentWorkStatus,AgentConnectionStatus,WritingTime,flag) values("+insert+")");
}
else
{
pstm=con.prepareStatement("Update AgentStatusInfo set AgentWorkStatus="+"'"+running+"'"+","+"AgentConnectionStatus="+"'"+ok+"'"+","+"flag="+0+","+"WritingTime="+"'"+logtime+"'"+"where HostName="+"'"+address+"'");
i=pstm.executeUpdate();
}
rs.close();
tt.closeConnect();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
//
public int UpdateMonitor(int UnitID,String GDDM,String GDXM,String BCRQ,float CJBH,String GSDM,float CJSL,float BCYE,String ZQDM,String SBSJ,float CJSJ,float CJJG,float CJJE,String SQBH,String BS,String MJBH,String Description,String DateTime){
int i=0;
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update AgentStatusInfo set AgentWorkStatus="+"'"+running+"'");
// pstm=con.prepareStatement("Update AgentStatusInfo set HostName="+"'"+address+"'"+","+"AgentName="+"'"+classname+"'"+","+"AgentWorkStatus="+"'"+running+"'"+","+"AgentConnectionStatus="+"'"+ok+"'"+","+"WritingTime="+"'"+logtime+"'");
pstm=con.prepareStatement("Update QBCj128 set UnitID="+UnitID+","+"GDDM="+"'"+GDDM+"'"+","+"GDXM="+"'"+GDXM+"'"+","+"BCRQ="+"'"+BCRQ+"'"+","+"CJBH="+CJBH+","+"GSDM="+"'"+GSDM+"'"+","+"CJSL="+CJSL+","+"BCYE="+BCYE+","+"ZQDM="+"'"+ZQDM+"'"+","+"SBSJ="+"'"+SBSJ+"'"+","+"CJSJ="+CJSJ+","+"CJJG="+CJJG+","+"CJJE="+CJJE+","+"SQBH="+"'"+SQBH+"'"+","+"BS="+"'"+BS+"'"+","+"MJBH="+"'"+MJBH+"'"+","+"Description="+"'"+Description+"'"+","+"DateTime="+"'"+DateTime+"'"+"where CJBH="+CJBH);
i=pstm.executeUpdate();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
//
public int Updateqsavg(String DatTime,float avgAmountValue,float avgMoneyValue){
int i=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement("Update TempTableQS set avgAmountValue="+avgAmountValue+","+"avgMoneyValue="+avgMoneyValue+"where DatTime="+"'"+DatTime+"'");
i=pstm.executeUpdate();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
//
//
public int Updatejqsavg(String DatTime,float avgAmountValue,float avgMoneyValue){
int i=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement("Update TempTableJQS set avgAmountValue="+avgAmountValue+","+"avgMoneyValue="+avgMoneyValue+"where DatTime="+"'"+DatTime+"'");
i=pstm.executeUpdate();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?