2011-12-27 43 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