Java 使用JSP运行多个函数
我是网络编程新手。我正在尝试使用jsp和odbc连接数据库。我已经为此编写了java代码。现在我需要在Tomcat服务器上运行。所以我选择JSP来做这项工作,但这显示了void类型不允许在这里和erors中使用。如何使用jsp运行此代码。我的错误是什么。请帮我解决这个问题。 这是我的密码Java 使用JSP运行多个函数,java,jsp,jsp-tags,Java,Jsp,Jsp Tags,我是网络编程新手。我正在尝试使用jsp和odbc连接数据库。我已经为此编写了java代码。现在我需要在Tomcat服务器上运行。所以我选择JSP来做这项工作,但这显示了void类型不允许在这里和erors中使用。如何使用jsp运行此代码。我的错误是什么。请帮我解决这个问题。 这是我的密码 <%@page import="java.sql.*"%> <%@page import="java.util.*" %> <%@page import="java.util
<%@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()%>
不要使用
(表达式)调用void方法,您应该避免使用JSP中的Java代码(read)
总记录:
索引为0的数组元素
在JSP表达式中使用方法时,方法应返回一个值。我发现所有方法的返回类型都是void
,即不返回任何内容。更改方法以返回适当的值
注意
我知道您目前正在学习JSP等,但请记住,在JSP等视图技术中编写控制/应用程序逻辑是一种糟糕的做法。尝试读取。@user1074824-您可以使用表达式“如何打印数组结果”从变量中写入值。假设我为name[j]指定了“name”值,如何打印它?是“从[Sheet1$]中选择count(*)”;'有效的SQL查询?我想您需要在那里使用绑定变量。
<%
ConnectSqlDB();
ConnectExcelDB();
getRowcount();
getExcelValues();
%>
<p>Total Records : <%=RowCount%>
<p>Array element at 0 index <%=name[0]%>
<%
for(String v:name)
{
out.println("<br/>" + v);
}
%>