/*Servlet load import with sql jdbc*/
import java.net.*;import java.io.*;import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;import java.sql.Statement;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/javascript");
/*Servlet utf-8 */
response.setCharacterEncoding("UTF-8"); // servlet utf8 handle
/*Servlet Parameter Handle*/
code = request.getParameter("code"); // servlet java parameter
type = request.getParameter("Type"); // servlet java parameter
String callback = request.getParameter("callback"); // for json callback
String value="1",value_2="0",value_3="0",value_4="0",value_5="0";
/* difference number parameter */
value=request.getParameter("value");
int iValue=0,iValue2=0, iValue3=0,iValue4=0, iValue5=0;
if (value!=null){
String asValues[] = value.split(",");
if (asValues.length>=1)value=asValues[0];
if (asValues.length>=2)value_2=asValues[1];
if (asValues.length>=3)value_3=asValues[2];
if (asValues.length>=4)value_2=asValues[3];
if (asValues.length>=5)value_3=asValues[4];
iValue= Integer.parseInt(value);
iValue2= Integer.parseInt(value_2);
iValue3= Integer.parseInt(value_3);
iValue4= Integer.parseInt(value_4);
iValue5= Integer.parseInt(value_5);
}
int iSignificant=4; // 4 is default
/*Servlet String handle in $ */
String ExtraSign=",,ABCD,DEFG,";
if (ExtraSign.indexOf(","+type+",")>0) // servlet string in foxpro $
{iSignificant= iValue+iValue3-1;}
else {iSignificant= iValue;}
String _return = "",result="",newString ="";
_r=callback+"([\n"; // for json callback
getSQLData();
/* create Reference Data */
if (type.equals("variableCall") || type.equals("Diff")){calTemplate(iValue,iValue2,iValue3);}
else if (type.equals("TEMPLATE")){calTemplate(iValue,iValue2,iValue3);}
else if (type.equals("TEMPLATE")){calTemplate(iValue,iValue2,iValue3);}
else if (type.equals("TEMPLATE")){calTemplate(iValue,iValue2,iValue3);}
makeJSon(iSignificant);
_r=_r+"]);";
StringBuilder sb = new StringBuilder(); sb.append(_r); _r="";
PrintWriter out = response.getWriter();
/*Send back to web page*/
out.print("hello I am tableRead 2");
out.print(sb.toString());
out.flush();
dRaws=null;
dResults=null;
}
}
static void getSQLData(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/Database?useUnicode=true&characterEncoding=UTF8","root", "root");
stmt = con.createStatement();
rs = stmt.executeQuery("select field1, UNIX_TIMESTAMP(date)*1000,field2,field3,field4 from "+table+" where key ="+code+" ;");
while (rs.next() /*skip*/)
{String id=rs.getString(1);}
rs.last(); // go bottom
int rowCount=rs.getRow();/*recno()*/
dRaws = new double[rowCount][];
rs.beforeFirst(); // go top
int iRecno=0;
while (rs.next()){
dRaws[iRecno] = new double [6+10];
for (int j=2; j<=7; j++){
try{dRaws[iRecno][j-2]=(double) rs.getDouble(j); }catch (NullPointerException e) {};
}
++iRecno;
}
con.close();
} catch (Exception e) {e.printStackTrace();}
}
// servlet make json
static void makeJSon(int Sign){
dResults = new double[dRaws.length][]; fieldCount=10;
for(int i=0;i<dRaws.length; i++){
dResults[i] = new double [fieldCount];
for (int j=0;j<=5;j++){dResults[i][j]=dRaws[i][j]; }
double close=(double) dResults[i][4];
}
String delimiter=",";
int i=dResults.length;
for(int j=0;j<i; j++){
_r=_r+"["; // Begin of Record
long _L=(long) dResults[j][0];
_r=_r+_L+",";
if (j+1<Sign){_r=_r+""+",";} else {
Double _d2=Math.round(dResults[j][1]*10000)/10000.000;
_r=_r+Double.toString(_d2)+",";}
if (fieldCount>=6){
//_r=_r+((j<iSignificant2?)="":Double.toString(dResults[j][2]))+",";
_r=_r+Double.toString(dResults[j][2])+",";
_r=_r+Double.toString(dResults[j][3])+",";
_r=_r+Double.toString(dResults[j][4])+",";
_L=(long) dResults[j][5];_r=_r+_L+",";
}
for (int k=7; k<fieldCount; k++){
if (j<iSignificant2){_r=_r+",";} else {
_r=_r+Double.toString(dResults[j][k])+",";
}
}
_r=_r+(j+1) ;
if (j>=i-1) {delimiter="";}
_r=_r+"]"+delimiter+"\n";
}
}
沒有留言:
張貼留言