<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.sql.*,java.util.*,java.lang.*;"%>
<%
String abc=request.getParameter("q");
out.println(abc);
%>
<%
public class postg
{
public void cat(){
int count=0;
// TODO Auto-generated method stub
try
{
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException cnfe)
{
// out.println("Could not find the JDBC driver!");
System.exit(1);
}
// out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost/test", "postgres","manafara");
connection.setAutoCommit(false);
Statement st1=connection.createStatement();
ResultSet rs1=st1.executeQuery("Select COUNT(*) AS TOTAL from groc1");
int n=0;
while(rs1.next())
{
n=rs1.getInt("TOTAL");
}
// out.println("Count: " +n);
rs1.close();
Double db[][]=new Double[n][2];
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from groc1 ");
for(int a=0; rs.next(); a++)
{
db[a][0]=(double)rs.getInt(4);
db[a][1]=rs.getDouble(6);
}
// out.println("data fetched: ");
// out.print(" \t cost quantity \n");
for(int i=0;i<n;i++)
{
// out.print(i+"\t");
for(int j=0;j<2;j++)
{
// out.print(db[i][j]+"\t");
}
// out.println();
}
// out.println();
Double inf=Double.POSITIVE_INFINITY;
Double[][] dist=new Double[n][n];//precomputed distance betn i and j
int[] davg=new int[n];//array of index of closest clusters for each i
int store[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
dist[i][j]=inf;
}
else dist[i][j]= distance(db[i],db[j]);
if(dist[i][j]<dist[i][davg[i]])
davg[i]=j;//store closest pairs index
}
}
// out.println("initial dist matrix");
for(int i=0;i<n;i++)
{
// out.print(i+":\t");
for(int j=0;j<=i;j++)
{
// out.print((dist[i][j])+"");
if(dist[i][j]!=inf)
// out.print("\t");
}
// out.println();
}
// out.print("\t");
for(int i=0;i<n;i++)
{
// out.print(i+"\t");
}
// out.println();
int s1;
for( s1=0;s1<n-1;s1++)
{ //update davg[]
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
dist[i][j]=inf;
if(dist[i][j]<dist[i][davg[i]])
davg[i]=j;
}
}
//find closest pair of clusters(pos, pos1)
int pos=0;
for(int b=0;b<n;b++)
{
int p=0;
while(dist[b][davg[b]]<5)
{
if(dist[b][davg[b]]<dist[pos][davg[pos]])
pos=b;
int pos1=davg[pos];
// out.println("pts "+(pos)+" and "+(pos1)+" are clustered hence new point is("+(pos)+" , "+(pos1)+")");
store[b][p]=pos1;
for(int i=0;i<n;i++)
{
// out.print(i +": ");
for(int j=0; j<n; j++)
// out.print(store[i][j]+" ");
// out.println();
}
for(int j=0;j<n;j++)
{
//if(dist[pos1][j]<dist[pos][j]) **single linkage
// dist[pos][j]=dist[j][pos]=dist[pos1][j];
//dist[pos][pos]=inf;
dist[pos][j]=dist[j][pos]=((dist[pos][j]+dist[pos1][j])/2);//average linkage
dist[pos][pos]=inf;
}
for(int i=0;i<n;i++)
{
dist[pos1][i]=dist[i][pos1]=inf;
}
for(int j=0;j<n;j++)
{
if(davg[j]==pos1)
davg[j]=pos;
if(dist[pos][j]<dist[pos][davg[pos]])
davg[pos]=j;
}
for(int i=0;i<n;i++)
{
// out.print(i+":\t");
for(int j=0;j<=i;j++)
{
// out.print((dist[i][j])+"");
if(dist[i][j]!=inf)
// out.print("\t");
}
// out.println();
}
// out.print("\t");
for(int i=0;i<n;i++)
{
// out.print(i+"\t");
}
// out.println("\n");
p++;
count++;
}//while
}//for p
}//big for
// out.println("no of clusters iterations"+count);
}//try
catch(Exception e)
{
// out.println("exception");
}
}
public Double distance(Double db1[], Double db2[])
{
double temp=(Math.pow((db2[0]-db1[0]),2)) + (Math.pow((db2[1]-db1[1]),2));
double db= Math.sqrt(temp);
return (db);
}
};//class
%>
错误 类型异常报告
消息
description服务器遇到内部错误(),导致无法完成此请求。
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 7 in the jsp file: /projedct/reco.jsp
Generated servlet error:
Syntax error on token ")", { expected after this token
An error occurred at line: 7 in the jsp file: /projedct/reco.jsp
Generated servlet error:
Syntax error on token "}", delete this token
An error occurred at line: 7 in the jsp file: /projedct/reco.jsp
Generated servlet error:
Syntax error on token ")", { expected after this token
An error occurred at line: 7 in the jsp file: /projedct/reco.jsp
Generated servlet error:
Syntax error, insert "}" to complete Block
An error occurred at line: 7 in the jsp file: /projedct/reco.jsp
Generated servlet error:
Syntax error, insert "}" to complete Block
An error occurred at line: 7 in the jsp file: /projedct/reco.jsp
Generated servlet error:
Syntax error, insert "}" to complete Block
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:409)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.
Apache Tomcat/5.5.12
答案 0 :(得分:0)
您可以在JSP中声明类但不推荐使用。
你也可以这样做,但我不建议你这样做..
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.sql.*,java.util.*,java.lang.*;"%>
<%
String abc=request.getParameter("q");
out.println(abc);
%>
<%
int count=0;
// TODO Auto-generated method stub
try
{
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException cnfe)
{
// out.println("Could not find the JDBC driver!");
System.exit(1);
}
// out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost/test", "postgres",
"manafara");
connection.setAutoCommit(false);
Statement st1=connection.createStatement();
ResultSet rs1=st1.executeQuery("Select COUNT(*) AS TOTAL from groc1");
int n=0;
while(rs1.next())
{n=rs1.getInt("TOTAL");}
// out.println("Count: " +n);
rs1.close();
Double db[][]=new Double[n][2];
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from groc1 ");
for(int a=0; rs.next(); a++)
{ db[a][0]=(double)rs.getInt(4);
db[a][1]=rs.getDouble(6);
}
// out.println("data fetched: ");
// out.print(" \t cost quantity \n");
for(int i=0;i<n;i++)
{
// out.print(i+"\t");
for(int j=0;j<2;j++)
{
// out.print(db[i][j]+"\t");
}
// out.println();
}
// out.println();
Double inf=Double.POSITIVE_INFINITY;
Double[][] dist=new Double[n][n];//precomputed distance betn i and j
int[] davg=new int[n];//array of index of closest clusters for each i
int store[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
dist[i][j]=inf;
}
else dist[i][j]= distance(db[i],db[j]);
if(dist[i][j]<dist[i][davg[i]])
davg[i]=j;//store closest pairs index
}
}
// out.println("initial dist matrix");
for(int i=0;i<n;i++)
{
// out.print(i+":\t");
for(int j=0;j<=i;j++)
{
// out.print((dist[i][j])+"");
if(dist[i][j]!=inf)
// out.print("\t");
}
// out.println();
}
// out.print("\t");
for(int i=0;i<n;i++)
{
// out.print(i+"\t");
}
// out.println();
int s1;
for( s1=0;s1<n-1;s1++)
{ //update davg[]
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
dist[i][j]=inf;
if(dist[i][j]<dist[i][davg[i]])
davg[i]=j;
}
}
//find closest pair of clusters(pos, pos1)
int pos=0;
for(int b=0;b<n;b++)
{ int p=0;
while(dist[b][davg[b]]<5)
{
if(dist[b][davg[b]]<dist[pos][davg[pos]])
pos=b;
int pos1=davg[pos];
// out.println("pts "+(pos)+" and "+(pos1)+" are clustered hence new point is("+(pos)+" , "+(pos1)+")");
store[b][p]=pos1;
for(int i=0;i<n;i++)
{// out.print(i +": ");
for(int j=0; j<n; j++)
// out.print(store[i][j]+" ");
// out.println();
}
for(int j=0;j<n;j++)
{
//if(dist[pos1][j]<dist[pos][j]) **single linkage
// dist[pos][j]=dist[j][pos]=dist[pos1][j];
//dist[pos][pos]=inf;
dist[pos][j]=dist[j][pos]=((dist[pos][j]+dist[pos1][j])/2);//average linkage
dist[pos][pos]=inf;
}
for(int i=0;i<n;i++)
{
dist[pos1][i]=dist[i][pos1]=inf;
}
for(int j=0;j<n;j++)
{
if(davg[j]==pos1)
davg[j]=pos;
if(dist[pos][j]<dist[pos][davg[pos]])
davg[pos]=j;
}
for(int i=0;i<n;i++)
{
// out.print(i+":\t");
for(int j=0;j<=i;j++)
{
// out.print((dist[i][j])+"");
if(dist[i][j]!=inf)
// out.print("\t");
}
// out.println();
}
// out.print("\t");
for(int i=0;i<n;i++)
{
// out.print(i+"\t");
}
// out.println("\n");
p++;
count++;
}//while
}//for p
}//big for
// out.println("no of clusters iterations"+count);
}//try
catch(Exception e)
{// out.println("exception");}
%>
//for methods you need to use <%! %>
<%! public Double distance(Double db1[], Double db2[])
{
double temp=(Math.pow((db2[0]-db1[0]),2)) + (Math.pow((db2[1]-db1[1]),2));
double db= Math.sqrt(temp);
return (db);
}
%>
答案 1 :(得分:0)
错误在那里:
if(dist[i][j]!=inf)
// out.print("\t");
}
然后:
if(dist[i][j]!=inf)
// out.print("\t");
}