A dirsi è facile...è a farsi che è un pò più difficile....
Questo dovrebbe essere il codice per connettersi al mio database (cambi a seconda del tipo di database che hai, io ho sql server 2005)
codice:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.*;
import java.io.*;
import java.util.*;
import org.jfree.*;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
public class Select {
public static void main(String args[]) {
// Indirizzo in cui si trova il database
String url = "jdbc:jtds:sqlserver://localhost:1433/DbCelle";
Connection con = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
}
catch (Exception e) {
System.out.println("Fallito il caricamento dei driver SQL.");
return;
}
try {
// Query SQL
String SQL= "SELECT * FROM Cella1";
// Connessione col database in cui vengono specificati url, nome utente, password
con = DriverManager.getConnection(url, "****", "****");
Statement select = con.createStatement();
//caricamento query SQL
ResultSet result = select.executeQuery(SQL);
System.out.println("Risultati:");
while (result.next()) {
int id = result.getInt(1);
String tempo = result.getString(2);
String temp_a = result.getString(3);
String temp_p = result.getString(4);
String temp_s_a = result.getString(5);
String temp_s_b = result.getString(6);
String temp_s_m = result.getString(7);
System.out.println("ID = " + id);
System.out.println("Tempo = " + tempo);
System.out.println("Temp_Ambiente = " + temp_a);
System.out.println("Temp_Prodotto = " + temp_p);
System.out.println("Temp_Set_Alta = " + temp_s_a);
System.out.println("Temp_Set_Bassa = " + temp_s_b);
System.out.println("Temp_Set_Media = " + temp_s_m);
System.out.println("");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (con != null) {
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
mentre questo è per fare un grafico con punti prefssati.....
codice:
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
public class Trend {
public static void main(String[] args) {
TimeSeriesCollection dataSet = new TimeSeriesCollection();
Day day = new Day();
TimeSeries data = new TimeSeries("Trend", day.getClass());
// XXX add real data here
Random r = new Random();
Calendar c = Calendar.getInstance();
for(int i = 0; i < 100; i++) {
int val = r.nextInt(100);
if(val < 50)
val += 50;
c.add(Calendar.DATE, 7);
Date date = c.getTime();
data.add(new Day(date), val);
}
dataSet.addSeries(data);
// The sparkline is created by setting a bunch of the visible properties
// on the domain, range axis and the XYPlot to false
DateAxis x = new DateAxis();
x.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1));
x.setTickLabelsVisible(true);
x.setTickMarksVisible(true);
x.setAxisLineVisible(true);
x.setNegativeArrowVisible(true);
x.setPositiveArrowVisible(true);
x.setVisible(true);
NumberAxis y = new NumberAxis();
y.setTickLabelsVisible(true);
y.setTickMarksVisible(true);
y.setAxisLineVisible(true);
y.setNegativeArrowVisible(true);
y.setPositiveArrowVisible(true);
y.setVisible(true);
XYPlot plot = new XYPlot();
plot.setInsets(new RectangleInsets(0, 0, 0, 0));
plot.setDataset(dataSet);
plot.setDomainAxis(x);
plot.setDomainGridlinesVisible(true);
plot.setDomainCrosshairVisible(true);
plot.setRangeGridlinesVisible(true);
plot.setRangeCrosshairVisible(true);
plot.setRangeAxis(y);
plot.setRenderer(new StandardXYItemRenderer(StandardXYItemRenderer.LINES));
JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBorderVisible(true);
try {
ChartUtilities.saveChartAsJPEG(new File("linea.jpeg"), chart,1400, 900);
}
catch(IOException e) {
System.err.println("Failed to render chart as png: "+ e.getMessage());
e.printStackTrace();
}
}
}
Ora basterebbe che qualcuno mi dia una mano ad unire le due cose ed io sarei a posto ed aiuterei anche tu....