2011-12-27 94 views
0

我是网络编程的新手。我正尝试使用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()%> 
+0

是''从[Sheet1 $]选择计数(*)“;'一个有效的SQL查询?我想你需要在那里使用绑定变量。 – Jayan 2011-12-27 06:37:01

回答

0

不要使用<%= %>(表达)来调用void的方法,你应该有避免在JSP中的Java代码(读SO thread)。

<% 
    ConnectSqlDB(); 
    ConnectExcelDB(); 
    getRowcount(); 
    getExcelValues(); 
%> 

<p>Total Records : <%=RowCount%> 
<p>Array element at 0 index <%=name[0]%> 

<% 
for(String v:name) 
{ 
    out.println("<br/>" + v); 
} 
%> 
+0

正在空白页 – user1074824 2011-12-27 04:53:15

+0

@ user1074824 - 您可以使用表达式从变量中写入值<%= var_or_expression%> – adatapost 2011-12-27 05:02:10

+0

如何输出数组结果。假设我为名称[j]分配了“名称”值,如何打印? – user1074824 2011-12-27 06:40:32

1

当您在JSP Expression中使用方法时,那么您的方法应该返回一个值。我看到你所有的方法都有返回类型void,即什么都不返回。改变你的方法来返回适当的值。

注意

我知道你正在学习JSP等,但请记住,在视图技术,如JSP编写contorl /应用逻辑是一种不好的做法。尝试阅读MVC pattern