|
楼主 |
发表于 2003-7-8 14:33:43
|
显示全部楼层
我的曲线图的程序是:
import java.applet.*;
import java.awt.*;
import java.sql.*;
import org.postgresql.Driver;
import java.awt.event.*;
public class QueryStorageData extends Applet{
Rectangle m_position=new Rectangle(51,10,700,380);
int hisnum=0;
// 从数据库中取曲线图所需动态值
public float[] getcurvevalue(String dataname) {
float curveval[]=new float[30];
String query;
String query1="SELECT rvalue, TO_CHAR(rtime, 'HH24:MI:SS') FROM rsave1 WHERE rdataname=";
String query2= " AND TO_CHAR(rtime, 'DD-MM-YYYY')=";
String query3= " ORDER BY rtime";
String temptime;
int hour, minute;
int pos1,pos2;
int count;
try {
Connection conn;
Statement stmt;
ResultSet rset;
Class.forName("org.postgresql.Driver");
// 与数据库建立连接
conn= DriverManager.getConnection("jdbc:postgresql:testdb", "postgres", "");
// 创建Statement对象用于执行SQL语句,并返回结果
stmt=conn.createStatement();
// 获取系统当前时间
rset=stmt.executeQuery("SELECT TO_CHAR (sysdate, 'HH24:MI-DD-MM-YYYY') from dual");
rset.next();
String SqlTime=rset.getString(1);
pos1=SqlTime.indexOf(":");
hour=Integer.parseInt(SqlTime.substring(0,pos1));
pos2=pos1+1;
pos1=SqlTime.indexOf("-", pos2);
minute=Integer.parseInt(SqlTime.substring(pos2,pos1));
hisnum=hour*6+(int)(minute/10);
String todaytime=SqlTime.substring(pos1+1);
// 执行查询,获取结果
query=query1+dataname+query2+"'"+todaytime+ "'"+ query3;
rset=stmt.executeQuery(query); // 执行查询
temptime= "";
count=0;
while (rset.next()) {
temptime=rset.getString(2); // get R_TIME
pos1=temptime.indexOf(":");
hour=Integer.parseInt(temptime.substring(0,pos1));
pos2=pos1+1;
pos1=temptime.indexOf(":",pos2);
minute=Integer.parseInt(temptime.substring(pos2,pos1));
count=hour*6+(int)(minute/10);
curveval[count]=rset.getFloat("rvalue");
}
stmt.close(); //关闭Stmt对象
conn.close(); //关闭Conn对象
} catch (SQLException ex) {
System.out.println ("\n *** SQLException ***\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("");
}
for (int j=0;j<30;j++)
curveval[j]=0;
} catch (java.lang.Exception ex) {
System.out.println ("error:"+ ex.getMessage()+ "\n");
PopupFrame msbox=new PopupFrame();
msbox.setSize(200,100);
msbox.add("Center", new Label ("database connection failed", Label.CENTER));
msbox.setLocation(300,250);
msbox.show();
for (int j=0;j<30;j++) {
curveval[j]=0;
}
}
return curveval;
}
public void paint(Graphics g) {
String dataname= "SY_ZP";
Font i_font=new Font("Dialog", Font.PLAIN, 16);
FontMetrics i_fm;
i_fm=g.getFontMetrics(i_font);
int maxInt=500;
int minInt=0;
float hisval[]=new float[30];
int h_distance=m_position.height/10;
int w_distance=(m_position.width-i_fm.stringWidth("500"))/24;
int rectwidth=24*w_distance;
g.setFont(i_font);
g.drawString("0",m_position.x-3-i_fm.stringWidth("0"), m_position.y+ m_position.height+i_fm.getHeight()/2);
float interval=(float)(maxInt-minInt)/10;
for(int i=1;i<=10;i++) {
String num=new String(Float.toString(minInt+i*interval));
g.drawString(num,m_position.x-3-i_fm.stringWidth(num),m_position.y+ m_position.height-i*h_distance+i_font.getSize()/2);
}
for( int j=1;j<=24;j++) {
g.drawString(Integer.toString(j),m_position.x+j*w_distance- i_font.getSize()/2,m_position.y+m_position.height+i_fm.getHeight());
}
// 画曲线
hisval=getcurvevalue(dataname); // 从数据库取历史值
float showval[]=new float[30];
// 把实际的数据转化为坐标上的值
for(int i=0; i<30; i++) {
showval=(hisval-minInt)/(maxInt-minInt);
}
for(int i=0; i<hisnum-1; i++) {
g.drawLine(m_position.x+i*rectwidth/30, m_position.y+ m_position.height-
(new Float(showval*m_position.height)).intValue(),m_position.x+
(i+1)*rectwidth/30, m_position.y+m_position.height-
(new Float(showval[i+1]*m_position.height)).intValue());
}
// 画坐标轴
g.fillRect(m_position.x, m_position.y+m_position.height, rectwidth,2);
g.fillRect(m_position.x, m_position.y, 2, m_position.height);
g.drawLine(m_position.x, m_position.y, m_position.x-3- i_fm.stringWidth("0"),m_position.y+8);
g.drawLine(m_position.x+1, m_position.y, m_position.x-2- i_fm.stringWidth("0"), m_position.y+8);
g.drawLine(m_position.x+rectwidth,m_position.y+m_position.height+1, m_position.x+rectwidth-8,m_position.y+m_position.height+5);
g.drawLine(m_position.x+rectwidth-1,m_position.y+ m_position.height+1, m_position.x+rectwidth-9,m_position.y+m_position.height+4);
}
}
// 提示窗口
class PopupFrame extends Frame {
public boolean handleEvent(Event evt) {
if (evt.id==Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(evt);
// return super.handleEvent(evt);
}
}
老说我的error org.postgresql.Driver;我是在我自己的机子上访问我自己建的数据库,谢谢各位了,请各位多多指教! |
|