Search code examples
javajspjsp-tags

Run multiple function using JSP


i am new to web programming. i am trying to connect database using jsp with odbc. i have already written a java code for this. now i need to run on Tomcat server. so i choose JSP to do this job.But this is showing me void type not allowed here and erors. how to run this code using jsp. what are my mistakes. please help me to solve in this. now this is my code

 <%@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()%>

Solution

  • Do not use <%= %> (Expression) to invoke void methods and you should have to avoid Java code in JSP (read 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);
     }
    %>