钢铁温度监控系统
标题扯得有点大,哈哈,这是我们工程训练的课题。当然我所在小组的任务只是其中很小的一部分——数据存储,组员除了我全部要考研,没时间弄,无奈任务就交给了我。
第一次用JAVA写程序,比较难看,上程序吧。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| package com;
import java.sql.*;
/*---------------
数据库连接操作类
by:piao
http://piao2010.com
piao2010@gmail.com
2009-12-20
-----------------*/
public class DB {
Connection conn;
Statement stat;
public void openDB()
{
try {
String userName = "www.piao2010.com";
String userPwd = "www.piao2010.com";
//for sql2000
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://IP:1433;DatabaseName=zhouhui";
//for sql2005
//String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//String dbURL = "jdbc:sqlserver://IP:1433;DatabaseName=zhouhui";
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("连接数据库成功!");
stat = conn.createStatement();
} catch (ClassNotFoundException e) {
System.out.println("注册数据库驱动出错!");
e.printStackTrace();
}
catch (SQLException e) {
System.out.println("连接数据库出错!");
e.printStackTrace();
}
}
public void closeDB()
{
try {
stat.close();
conn.close();
System.out.println("关闭数据库成功!");
} catch (SQLException e) {
System.out.println("关闭数据库出错!");
e.printStackTrace();
}
}
public void execute(String sql)
{
try {
stat.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("执行execute方法出错!");
e.printStackTrace();
}
}
public ResultSet query(String sql)
{
try {
return stat.executeQuery(sql);
} catch (SQLException e) {
System.out.println("执行executeQuery方法出错!");
e.printStackTrace();
}
return null;
}
} |
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| package com;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.DB;
/*--------------------
测点类
by:piao
http://piao2010.com
piao2010@gmail.com
2009-12-20
--------------------*/
public class PT {
private int id;//生产线编号
private double temp;//温度
private Date datetime ;//时间
private double MAX,MIN,ADD,CFWD;//报警上下限温度,补偿温度,触发温度
private int err = 0;//异常
private DB db = new DB();
//初始化数据并调用报警、存储方法
public PT(int id,double temp,Date dt){
this.id = id;
this.datetime = dt;
String dosql2 = "SELECT * FROM [SCXXXB] WHERE [SCXBH_PK]="+id;//出于安全考虑可以对id数据取整操作,我这里从简没有处理,其它参数也是类似
try{
db.openDB();
System.out.println(dosql2);//调试dosql2
ResultSet rs = db.query(dosql2);
rs.next();
this.MIN = rs.getDouble("BJWDXX");
this.MAX = rs.getDouble("BJWDSX");
this.ADD = rs.getDouble("BCWD");
this.CFWD = rs.getDouble("CFWD");
if (temp >= this.CFWD){
this.temp = temp+this.ADD;
this.err = isAlert(this.temp);
if(this.err == 1)
doAlert();//报警
doInsert();//数据存储
}
else
System.out.println("检点温度低于触发温度,忽略此次检测数据!");
db.closeDB();
} catch (Exception e){
System.out.println("操作数据库出错!");
}
}
//报警检测
public int isAlert(double temp){
if (( temp < this.MIN ) || (temp > this.MAX))
return 1;
else
return 0;
}
//报警方法
public void doAlert(){
//报警相关动作:日志记录等等
System.out.println("产生一条报警数据!");
}
//存储数据
public void doInsert(){
String dt = DateToString(this.datetime);
String dosql = "INSERT INTO [WDSJB] values ("+this.id+",'"+dt+"',"+this.temp+","+this.err+")";
try{
System.out.println(dosql);//调试dosql
db.execute(dosql);
System.out.println("存储数据成功!");
} catch (Exception e){
System.out.println("存储数据出错!");
}
}
//日期时间格式转化
public String DateToString(Date date) {
String dateStr = "";
if ( date == null)
date = new Date(); //如果取不到时间,就用当前时间
try{
SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateStr = DF.format(date);
} catch (Exception e) {
System.out.println("日期时间格式转化出错!");
}
return dateStr;
}
} |
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com;
import java.util.Date;
import com.PT;
/*---------------
测试代码,此程序在XP环境
下测试通过,远程数据库
服务器采用MSSQL2k
by:piao
http://piao2010.com
piao2010@gmail.com
2009-12-20
-----------------*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sid=1;//生产线ID
double temp=600;//温度
double temp2=900;
double temp3=1200;
try{
PT pt = new PT(sid,temp,new Date());
PT pt2 = new PT(sid,temp2,new Date());
PT pt3 = new PT(sid,temp3,new Date());
} catch (Exception e)
{
System.out.println("创建对象并执行方法出错!");
}
}
} |
相关日志
Tags: java
This entry was posted
on 星期四, 12月 31st, 2009 at 23:29 and is filed under code.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.