log.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 478 行
JAVA
478 行
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", "cqjk");
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+"'";
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+"'"+","+"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 UpdateBZJ(int Num,int ID,String DateTime,int CoverageMoney){
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 QBCj127 set ID="+ID+","+"DateTime="+"'"+DateTime+"'"+","+"CoverageMoney="+CoverageMoney+"where Num="+Num);
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;
}
//
//end add for information
//
public int Update(long maxnum){
int i=0;
// Date ndate = new Date();
// String logtime=ndate.toString();
System.out.println("maxnum is: "+maxnum);
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 ZTB set maxnum="+maxnum);
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;
}
//
///delete records
//
public int bzjUpdate(long maxbzjnum){
int i=0;
// Date ndate = new Date();
// String logtime=ndate.toString();
System.out.println("maxbzjnum is: "+maxbzjnum);
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 ZTB set maxbzjnum="+maxbzjnum);
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 Clear(String Del){
int j=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement(Del);
j=pstm.executeUpdate();
pstm.close();
}
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 j;
}
/////
public int ClearAll(){
int j=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement("delete from log");
j=pstm.executeUpdate();
pstm.close();
}
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 j;
}
///display records
public String[][] Save(ResultSet rs) {
int i;
String tmpstr;
int recordNumber;
String[][] recordArray=null;
try{
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
int no=0;
recordNumber=300;
recordArray=new String[recordNumber][numCols];
//display each column title
for(i=1;i<=numCols;i++){
recordArray[no][i-1]=rsmd.getColumnLabel(i);
}
while (rs.next()){
no++;
for(i=1;i<=numCols;i++){
tmpstr=rs.getString(i);
if(rs.wasNull()){
recordArray[no][i-1]="NULL";
}
else {
recordArray[no][i-1]=tmpstr;
}
}
}
}
catch(SQLException ex){
System.out.println("\n***SQLException333 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 recordArray;
}
public void display(String[][] recordArray){
int l=recordArray.length;
int ll=recordArray[1].length;
System.out.println("array lenth is :"+l);
System.out.println("record lenth is :"+ll);
for (int i=0;i<l;i++){
for (int j=0;j<ll;j++){
if (recordArray[i][j]!=null)
System.out.print(recordArray[i][j]+",");
else return;
}
System.out.println(""); ///换行显示
}
}
public void closeConnect(){
try{
con.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
}
}
public void closeResult(){
try{
rs.close();
stmt.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
}
}
public static void main(String args[])
{
log log=new log();
log.Init("Ecmdb");
log.Add("insert into log (type,describe,logtime) values ('aaaqq','bbbqqqq','cccqq')" );
log.Add("err","aaaaaaaa");
log.ClearAll();
log.Sql("select * from log");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?