log.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,143 行 · 第 1/3 页
JAVA
1,143 行
package shezhi;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.Date;
import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.ObjectOutput;
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("com.ashna.jturbo.driver.Driver");
//con = DriverManager.getConnection("jdbc:JTurbo://localhost:1433/"+data_source+"/charset=gb2312", "sa", "");
// 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=ndate.toString();
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;
}
//
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) 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 Change(String Unit){
int i=0;
//Date ndate = new Date();
//String logtime=ndate.toString();
//System.out.println("record is: "+record);
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update MonitoredValueInfo set MonitoredValue="+record);
pstm=con.prepareStatement("Update MonitoredValueInfo set Change="+0+"where MonitoredValueID="+1+"and UnitName="+"'"+Unit+"'");
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 Change1(String Unit){
int i=0;
//Date ndate = new Date();
//String logtime=ndate.toString();
//System.out.println("record is: "+record);
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update MonitoredValueInfo set MonitoredValue="+record);
pstm=con.prepareStatement("Update MonitoredValueInfo set Change="+0+"where MonitoredValueID="+2+"and UnitName="+"'"+Unit+"'");
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 Change13(String Unit){
int i=0;
//Date ndate = new Date();
//String logtime=ndate.toString();
//System.out.println("record is: "+record);
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update MonitoredValueInfo set MonitoredValue="+record);
pstm=con.prepareStatement("Update MonitoredValueInfo set Change="+0+"where MonitoredValueID="+3+"and UnitName="+"'"+Unit+"'");
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 Change14(String Unit){
int i=0;
//Date ndate = new Date();
//String logtime=ndate.toString();
//System.out.println("record is: "+record);
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update MonitoredValueInfo set MonitoredValue="+record);
pstm=con.prepareStatement("Update MonitoredValueInfo set Change="+0+"where MonitoredValueID="+4+"and UnitName="+"'"+Unit+"'");
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 Change15(String Unit){
int i=0;
//Date ndate = new Date();
//String logtime=ndate.toString();
//System.out.println("record is: "+record);
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update MonitoredValueInfo set MonitoredValue="+record);
pstm=con.prepareStatement("Update MonitoredValueInfo set Change="+0+"where MonitoredValueID="+5+"and UnitName="+"'"+Unit+"'");
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 Change16(String Unit){
int i=0;
//Date ndate = new Date();
//String logtime=ndate.toString();
//System.out.println("record is: "+record);
PreparedStatement pstm;
try{
// pstm=con.prepareStatement("Update MonitoredValueInfo set MonitoredValue="+record);
pstm=con.prepareStatement("Update MonitoredValueInfo set Change="+0+"where MonitoredValueID="+6+"and UnitName="+"'"+Unit+"'");
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;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?