分类: Java
2005-06-23 08:52:09
一个程序
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.Runtime;
import java.util.*;
import java.util.regex.*;
import java.text.*;
class chat extends JPanel
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("找不到驱动程序");
}
final JFrame frame = new JFrame("数据控制系统");
Container hu=frame.getContentPane();
//
final TextArea hu1=new TextArea("",20,40);
hu1.setEditable(false);
hu1.setText("欢迎您进入内部数据控制系统!!请选择相应命令进行操作
键入help寻求帮助");
final JTextField hu2=new JTextField(10);
hu.setLayout(new BorderLayout());
hu.add(hu1,BorderLayout.CENTER);
hu.add(hu2,BorderLayout.SOUTH);
//
hu2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String msg=hu2.getText();
if(msg.equals("help"))
{
hu1.setText("help -> to find help
view all -> to view all
view visit -> to view visit
view ip -> to view ip
view user $user -> to view infomation of a user
Compare $user1 $user2 $dis -> to compare the change of $user1 and $user2,delay $dis miliseconds per one information
Allcompare amount dis or Allcompare rate dis or Allcompare all dis
listen visit -> to listen visit change
listen data -> to listen data change
backup -> to backup database
copyright -> to view copyright
you can also input select Query to select information from job database
the below list is aviliable sql:
select,update,delete,drop,create,alter,optimize,lock,unlock,use,describe,repair
");
}
if(msg.equals("view all"))
{
hu1.setText("NOW,the brief information of this system:
");
try
{
double[] hn=new double[20];
int all=0;
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String query="select count(*) as s from job";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("Totally infomation: "+rs.getInt("s")+"
");
query="select count(*) as s from job where user>0";
rs=stmt.executeQuery(query);
while(rs.next())
{
all=rs.getInt("s");
hu1.append("User infomation: "+all+"
");
}
query="select avg(user) as s from job where user>0";
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("User avg: "+rs.getDouble("s")+"
");
query="select count(*) from job where to_days(antime)-to_days(now())>-30";
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("the amount of laterest month:"+rs.getInt(1)+"
");
query="select count(*) from job where to_days(antime)-to_days(now())>-7";
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("the amount of laterest week:"+rs.getInt(1)+"
");
query="select count(*) from job where to_days(antime)-to_days(now())=0";
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("the amount of today:"+rs.getInt(1)+"
");
hu1.append("
------list of user information/all-------
");
query="select count(*) as s,user from job where user>0 group by user order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
int user=rs.getInt("user");
int amount=rs.getInt("s");
double amou=rs.getDouble("s");
double amou1=(amou/all)*100;
hn[user]=amou1;
hu1.append(user+" "+amount+" "+amou1+"%
");
}
hu1.append("
------list of user information/laterest month-------
");
query="select count(*) as s from job where to_days(antime)-to_days(now())>-30";
rs=stmt.executeQuery(query);
while(rs.next())
all=rs.getInt("s");
query="select count(*) as s,user from job where to_days(antime)-to_days(now())>-30 group by user order by s desc";
rs=stmt.executeQuery(query);
String res;
while(rs.next())
{
int user=rs.getInt("user");
int amount=rs.getInt("s");
double amou=rs.getDouble("s");
double amou1=(amou/all)*100;
if(hn[user]
else if(hn[user]==amou1)
res="balance";
else
res="down";
hu1.append(user+" "+amount+" "+amou1+"% "+res+"
");
}
hu1.append("
------list of user information/laterest week-------
");
query="select count(*) as s from job where to_days(antime)-to_days(now())>-7";
rs=stmt.executeQuery(query);
while(rs.next())
all=rs.getInt("s");
query="select count(*) as s,user from job where to_days(antime)-to_days(now())>-7 group by user order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
int user=rs.getInt("user");
int amount=rs.getInt("s");
double amou=rs.getDouble("s");
double amou1=(amou/all)*100;
if(hn[user]
else if(hn[user]==amou1)
res="balance";
else
res="down";
hu1.append(user+" "+amount+" "+amou1+"% "+res+"
");
}
hu1.append("
------list of user information/today-------
");
query="select count(*) as s from job where to_days(antime)-to_days(now())=0";
rs=stmt.executeQuery(query);
while(rs.next())
all=rs.getInt("s");
query="select count(*) as s,user from job where to_days(antime)-to_days(now())=0 group by user order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
int user=rs.getInt("user");
int amount=rs.getInt("s");
double amou=rs.getDouble("s");
double amou1=(amou/all)*100;
if(hn[user]
else if(hn[user]==amou1)
res="balance";
else
res="down";
hu1.append(user+" "+amount+" "+amou1+"% "+res+"
");
}
stmt.close();
conn.close();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
if(msg.equals("view visit"))
{
try
{
int all,allm,allw,alld;
all=allm=allw=alld=0;
hu1.setText("NOW,The brief information of visit database
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String query="select count(*) as s from visit";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
all=rs.getInt("s");
hu1.append("The amount of visit :"+all+"
");
}
query="select count(*) as s from visit where to_days(time)-to_days(now())>-30";
rs=stmt.executeQuery(query);
while(rs.next())
{
allm=rs.getInt("s");
hu1.append("The amount of laterest month:"+allm+"
");
}
query="select count(*) as s from visit where to_days(time)-to_days(now())>-7";
rs=stmt.executeQuery(query);
while(rs.next())
{
allw=rs.getInt("s");
hu1.append("The amount of laterest week:"+allw+"
");
}
query="select count(*) as s from visit where to_days(time)-to_days(now())=0";
rs=stmt.executeQuery(query);
while(rs.next())
{
alld=rs.getInt("s");
hu1.append("The amount of today:"+alld+"
");
}
query="select max(time) as s from visit";
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("The laterest click:"+rs.getString("s")+"
");
hu1.append("
------the list of visit/all------
");
query="select count(*) as s,visit from visit group by visit order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/all)*100;
hu1.append(rs.getString("visit")+" "+rs.getInt("s")+" "+v1+"%
");
}
hu1.append("
------the list of visit/the laterest month------
");
query="select count(*) as s,visit from visit where to_days(time)-to_days(now())>-30 group by visit order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/allm)*100;
hu1.append(rs.getString("visit")+" "+rs.getInt("s")+" "+v1+"%
");
}
hu1.append("
------the list of visit/the laterest week------
");
query="select count(*) as s,visit from visit where to_days(time)-to_days(now())>-7 group by visit order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/allw)*100;
hu1.append(rs.getString("visit")+" "+rs.getInt("s")+" "+v1+"%
");
}
hu1.append("
------the list of visit/today------
");
query="select count(*) as s,visit from visit where to_days(time)-to_days(now())=0 group by visit order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/alld)*100;
hu1.append(rs.getString("visit")+" "+rs.getInt("s")+" "+v1+"%
");
}
stmt.close();
conn.close();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
if(msg.equals("view ip"))
{
try
{
int all,allm,allw,alld;
all=allm=allw=alld=0;
hu1.setText("NOW,The brief information of ip database
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String query="select count(*) as s from visit";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
all=rs.getInt("s");
hu1.append("The amount of visit :"+all+"
");
}
query="select count(*) as s from visit where to_days(time)-to_days(now())>-30";
rs=stmt.executeQuery(query);
while(rs.next())
{
allm=rs.getInt("s");
hu1.append("The amount of laterest month:"+allm+"
");
}
query="select count(*) as s from visit where to_days(time)-to_days(now())>-7";
rs=stmt.executeQuery(query);
while(rs.next())
{
allw=rs.getInt("s");
hu1.append("The amount of laterest week:"+allw+"
");
}
query="select count(*) as s from visit where to_days(time)-to_days(now())=0";
rs=stmt.executeQuery(query);
while(rs.next())
{
alld=rs.getInt("s");
hu1.append("The amount of today:"+alld+"
");
}
query="select max(time) as s from visit";
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append("The laterest click:"+rs.getString("s")+"
");
hu1.append("
------the list of ip/all------
");
query="select count(*) as s,ip from visit group by ip order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/all)*100;
hu1.append(rs.getString("ip")+" "+rs.getInt("s")+" "+v1+"%
");
}
hu1.append("
------the list of ip/the laterest month------
");
query="select count(*) as s,ip from visit where to_days(time)-to_days(now())>-30 group by ip order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/allm)*100;
hu1.append(rs.getString("ip")+" "+rs.getInt("s")+" "+v1+"%
");
}
hu1.append("
------the list of ip/the laterest week------
");
query="select count(*) as s,ip from visit where to_days(time)-to_days(now())>-7 group by ip order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/allw)*100;
hu1.append(rs.getString("ip")+" "+rs.getInt("s")+" "+v1+"%
");
}
hu1.append("
------the list of visit/today------
");
query="select count(*) as s,ip from visit where to_days(time)-to_days(now())=0 group by ip order by s desc";
rs=stmt.executeQuery(query);
while(rs.next())
{
double v=rs.getDouble("s");
double v1=(v/alld)*100;
hu1.append(rs.getString("ip")+" "+rs.getInt("s")+" "+v1+"%
");
}
stmt.close();
conn.close();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
if(msg.equals("listen visit"))
{
try
{
hu1.setText("Listen visit start......
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
int ori_amount=0;
int now_amount=0;
String query;
ResultSet rs;
for(;;)
{
try
{
Thread.sleep(1000);
query="select count(*) as s from visit";
rs=stmt.executeQuery(query);
while(rs.next())
now_amount=rs.getInt("s");
if(now_amount!=ori_amount)
{
int dis_amount=now_amount-ori_amount;
int now1_amount=now_amount-1;
query="select * from visit limit "+now1_amount+","+dis_amount;
rs=stmt.executeQuery(query);
while(rs.next())
hu1.append(rs.getInt("id")+" "+rs.getString("time")+" "+rs.getString("visit")+" "+rs.getString("ip")+"
");
ori_amount=now_amount;
}
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
if(msg.equals("listen data"))
{
try
{
hu1.setText("Listen data start......
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
int ori_amount=0;
int now_amount=0;
String query;
ResultSet rs;
for(;;)
{
try
{
Thread.sleep(1000);
query="select count(*) as s from job";
rs=stmt.executeQuery(query);
while(rs.next())
now_amount=rs.getInt("s");
if(now_amount!=ori_amount)
{
ori_amount=now_amount;
query="select count(*) as s,user from job where user>0 group by user order by user";
rs=stmt.executeQuery(query);
StringBuffer ra=new StringBuffer("");
int v1=now_amount-50896;
ra.append(v1+" ");
while(rs.next())
{
int v2=rs.getInt("s");
ra.append(v2+" ");
}
hu1.append(ra.toString()+"
");
}
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
if(msg.startsWith("s")||msg.startsWith("use")||msg.startsWith("o")||msg.startsWith("describe")||msg.startsWith("c")||msg.startsWith("r"))
{
try
{
int pi=-1;
String msg_sql=hu2.getText();
hu1.setText("the Query you input is
"+msg_sql+"
");
hu1.append("The result from database reflection:
--->
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";//&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery(msg_sql);
int cn=0;
while(rs.next())
{
ResultSetMetaData rsmd=rs.getMetaData();
cn=rsmd.getColumnCount();
for(int ik=1;ik<=rsmd.getColumnCount();ik++)
{
hu1.append(rsmd.getColumnName(ik)+" ");//+rsmd.getColumnTypeName(ik)+" "+rsmd.getColumnType(ik)+"
");
}
hu1.append("
");
break;
}
rs.beforeFirst();
while(rs.next())
{
if(++pi>300)
break;
for(int is=1;is<=cn;is++)
{
try
{
String vs=rs.getString(is);
String newstring=new String(vs.getBytes("ISO8859_1"),"gb2312");
hu1.append(newstring+" ");
}
catch(Exception es)
{
es.printStackTrace();
}
}
hu1.append("
");
}
hu1.append("
");
hu1.append(++pi+" lines affected
");
stmt.close();
conn.close();
}
catch(SQLException e1)
{
String error="
"+e1;
hu1.append("sql error happend:
"+error);
//e1.printStackTrace();
}
catch(Exception ev)
{
ev.printStackTrace();
}
}
if(msg.startsWith("insert")||msg.startsWith("delete")||msg.startsWith("update")||msg.startsWith("create")||msg.startsWith("drop")||msg.startsWith("alter"))//以下为executeUpdate部分
{
try
{
String msg_sql=hu2.getText();
hu1.setText("the Query you input is
"+msg_sql+"
");
hu1.append("The result from database reflection:
--->
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";//&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.executeUpdate(msg_sql);
hu1.append("run successfully");
stmt.close();
conn.close();
}
catch(SQLException e1)
{
String error="
"+e1;
hu1.append("sql error happend:"+error);
}
}
if(msg.startsWith("lock")||msg.startsWith("unlock"))//以下为execute部分
{
try
{
String msg_sql=hu2.getText();
hu1.setText("the Query you input is
"+msg_sql+"
");
hu1.append("The result from database reflection:
--->
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";//&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.execute(msg_sql);
hu1.append("run successfully");
stmt.close();
conn.close();
}
catch(SQLException e1)
{
String error="
"+e1;
hu1.append("sql error happend:"+error);
}
}
if(msg.equals("backup"))//以下部分为数据库备份部分!!
{
try
{
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
String url1="jdbc:mysql://192.168.0.101:3306/";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String query="delete from jobbak";
hu1.setText("execute delete:"+query+".....
");
stmt.executeUpdate(query);
hu1.append("execute delete:succeed!
");
query="insert into jobbak select * from job";
hu1.append("execute insert:"+query+".....
");
stmt.executeUpdate(query);
hu1.append("execute insert:succeed!
");
query="select * from job into outfile 'f:/job.txt' fields terminated by '*&^%'";
hu1.append("
lanback start!!
");
hu1.append("
delete file f:/job.txt .....
");
File china=new File("f:/job.txt");
int ps=0;
while(china.exists())
{
china.delete();
if(++ps>10)
break;
}
if(!china.exists())
{
hu1.append("file f:/job.txt deleted successfully!!
");
hu1.append("execute making a file:"+query+".....
");
stmt.execute(query);
hu1.append("execute making a file:succeed!
");
hu1.append("backup to 192.168.0.101 start!!
");
Runtime rt=Runtime.getRuntime();
//rt.exec("c:\mysql\bin\mysqlimport -u huyang -p -h 192.168.0.101 -d --fields-terminated-by="*&^%" huyang f:\job.txt");
String cmd1="c:\mysql\bin\mysqlimport -u huyang -ppassword -h 192.168.0.101 -d --fields-terminated-by=\"*&^%\" huyang f:\job.txt";
rt.exec(cmd1);
hu1.append("backup to 192.168.0.101 succeed!
");
}
else
{
hu1.append("file f:/job.txt can not be deleted,lanback end!!
");
}
stmt.close();
conn.close();
}
catch(SQLException e1)
{
String error="
"+e1;
hu1.append("sql error happend:
"+error);
//e1.printStackTrace();
}
catch(Exception e0)
{
String error="
"+e0;
hu1.append("error happend:
"+error);
}
}
if(msg.startsWith("view user"))
{
try
{
String[] ss=msg.split("");
int user=Integer.parseInt(ss[2]);
hu1.setText("the information of user "+user+" is :
");
String url = "jdbc:mysql://localhost:3306/job?user=root&password=";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String query="select count(*) as s from job where user="+user;
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
hu1.append("The total information: "+rs.getInt("s")+"
");
}
query="select max(antime),min(antime) from job where user="+user;
rs=stmt.executeQuery(query);
while(rs.next())
{
hu1.append("The earlist click was at "+rs.getString(2)+"
");
hu1.append("The laterest click was at "+rs.getString(1)+"
");
}
hu1.append("
----the change of rate----
&qu