我是网络编程的新手。我正尝试使用jsp与odbc连接数据库。我已经为此写了一个java代码。现在我需要在Tomcat服务器上运行。所以我选择JSP来完成这项工作。但是这显示了我在这里不允许的void类型和erors。如何使用jsp运行此代码。我的错误是什么?请帮我解决这个问题。 现在这是我的代码使用JSP运行多个函数
<%@page import="java.sql.*"%>
<%@page import="java.util.*" %>
<%@page import="java.util.logging.Level"%>
<%@page import="java.util.logging.Logger"%>
<%!
int i=0,j=0,k=0;
Connection conn=null;
Connection connection=null;
static int count=0;
String Cname[]=null;
String Title[]=null;
Statement stmt1=null;
ResultSet NumOfRows=null;
Statement stmt2=null;
ResultSet SpreadsheetValues=null;
Statement stmt3=null;
ResultSet rs3=null;
int RowCount;
//this static function required to connect excel database
static
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Exception in connecting to DB"+e.getMessage());
}
}
// connect Sql database
void ConnectSqlDB() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:ServerDB","sa","sqladmin");
System.out.println("MSSQL connected " +"<br>");
}catch(Exception e){
e.printStackTrace();
System.out.println("Exception in connecting to DB"+e.getMessage());
}
}
void ConnectExcelDB() throws SQLException{
conn=DriverManager.getConnection("jdbc:odbc:spreadsheetdb","","");
}
void getRowcount() throws SQLException{
stmt1=conn.createStatement();
String Qs1="select count(*) from [Sheet1$]";
NumOfRows=stmt1.executeQuery(Qs1);
while(NumOfRows.next()){
Rowcount=NumOfRows.getInt(1);
}
NumOfRows.close();
}
void getExcelValues() throws SQLException{
stmt2=conn.createStatement();
String Qs2="select * from [Sheet1$]";
SpreadsheetValues=stmt2.executeQuery(Qs2);
Cname=new String[Rowcount];
Title=new String[Rowcount];
while(SpreadsheetValues.next()){
// Assigning Spread sheet values to String array
Cname[j]=SpreadsheetValues.getString("Cname");
Title[j]=SpreadsheetValues.getString("Title");
j++;
}
}
SpreadsheetValues.close();
stmt2.close();
conn.close();
SpreadsheetValues=null;
stmt2=null;
conn=null;
}
%>
<%=ConnectSqlDB()%>
<%=ConnectExcelDB()%>
<%=getRowcount()%>
<%=getExcelValues()%>
是''从[Sheet1 $]选择计数(*)“;'一个有效的SQL查询?我想你需要在那里使用绑定变量。 – Jayan 2011-12-27 06:37:01