jsp 数据饼图
引用:http://www.andjava.com/a/tab2/tab2_9/2012/0302/1628.html
<%@pagelanguage="Java"%>
<%@pageimport="java.io.OutputStream"%>
<%@pageimport="java.sql.*"%>
<%@pageimport="java.awt.*"%>
<%@pageimport="java.awt.geom.*"%>
<%@pageimport="java.awt.image.BufferedImage"%>
<%@pageimport="com.sun.image.codec.jpeg.*"%>
<%!
////////////////////////////////////////////////////////////
//PieColorsclassmanagesthecolorsusedinthepiechart
////////////////////////////////////////////////////////////
classPieColors
{
ColorpieColorArray[]={
newColor(210,60,60),newColor(60,210,60),newColor(60,60,210),
newColor(120,60,120),newColor(60,120,210),newColor(210,120,60)
};
intcurPieColor=0;
publicColorgetPieColor()
{
returnpieColorArray[curPieColor];
}
publicvoidsetNewColor()
{
curPieColor ;
if(curPieColor>=pieColorArray.length)
{curPieColor=0;}
}
}
%>
<%!Stringdriver="com.MySQL.JDBC.Driver";Stringdburl="jdbc:mysql://localhost/articles";Stringdbuid="myuid";Stringdbpwd="mypwd";
////////////////////////////////////////////////////////////
//GettheproductsfromthedatabaseasaStringarray
////////////////////////////////////////////////////////////
publicString[]getProducts()
{
String[]arr=newString[0];
Connectioncon;
Statementstmt;
ResultSetrs;
intcount=0;
Stringsql="select*fromp_productsorderbyproductID";
try
{
//LoadDriver:
Class.forName(driver);
//Connecttothedatabasewiththeurl
con=DriverManager.getConnection(dburl,dbuid,dbpwd);
stmt=con.createStatement();
//GetResultSet
rs=stmt.executeQuery(sql);
//Counttherecords
while(rs.next()){count ;
}
//Createanarrayofthecorrectsize
arr=newString[count];
//GetResultSet(themostportablewayofusingrsasecondtime)
rs=stmt.executeQuery(sql);
while(rs.next())
{
arr[rs.getInt("productID")]=rs.getString("productname");
}
stmt.close();
con.close();
}
catch(java.lang.Exceptionex)
{arr[0]=ex.toString();}
returnarr;
}
////////////////////////////////////////////////////////////
//Getthesalestotalsfromthedatabase
////////////////////////////////////////////////////////////
publicfloat[]getSales(intproducts)
{
float[]arr=newfloat[products];
Connectioncon;
Statementstmt;
ResultSetrs;
Stringsql="selectproductID,amountfromp_sales";
try{
//LoadDriver:
Class.forName(driver);
//Connecttothedatabasewiththeurl
con=DriverManager.getConnection(dburl,dbuid,dbpwd);
stmt=con.createStatement();
//GetResultSet
rs=stmt.executeQuery(sql);
while(rs.next()){intproduct=rs.getInt("productID");
//CheckthattheproductIDisvalid
if(product>=0&&product<products)
{
//Addtoproducttotal
arr[product] =rs.getFloat("amount");
}
}
stmt.close();
con.close();
}catch(java.lang.Exceptionex){arr[0]=-1.0f;}