0
我有一個jsp頁面,我想從數據庫中放置一些信息。 它的代碼:JSP和德比數據庫類
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
import="java.util.ArrayList"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet">
<title>Lab4</title>
</head>
<body>
<div id='chekboxOfCompanies'>
<%
ArrayList<String> companiesList=DBHandler.getCompaniesList();
for (int i=0;i<companiesList.size();i++){
out.println("<input type='checkbox'>");
}
%>
</div>
</body>
</html>
DBHandler獲取數據庫公司名稱的ArrayList中。這是代碼。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class DBHandler {
public static ArrayList<String> getCompaniesList(){
Connection conn = null;
ArrayList<Statement> statements = new ArrayList<Statement>();
Statement s = null;
ResultSet rs = null;
String dbName = "Lab4DB";
ArrayList<String> returnArray=new ArrayList<String>();
try {
conn = DriverManager.getConnection(protocol + dbName);
System.out.println("Connected to database " + dbName);
conn.setAutoCommit(false);
s = conn.createStatement();
statements.add(s);
rs = s.executeQuery("SELECT company from companies");
while(rs.next()){
returnArray.add(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnArray;
}
private static String protocol = "jdbc:derby:";
}
當我嘗試在服務器上運行它時,它說DBHandler無法解析。我如何獲得這些信息?我究竟做錯了什麼?
好吧,DBHandler類在DB包中。 我應該如何導入它?進口= 「DB.DBHandler」? –
它仍然表示無法解析 –
import =「java.util.ArrayList,DB.DBHandler」。 Google搜索「JSP導入」會給你答案。包名稱應該是小寫的。 –