我是 servlet 的新手。我正在尝试使用带有 JDBC 和 OJDBC 的 java 连接到数据库。我已经为此编写了一个 java 代码。现在我需要在 Tomcat 服务器上运行它。所以我选择了 servlet。我使用 Netbeans IDE 完成了这项工作,在那里我选择了 servlet,并在 web.xml 中将类名作为 servlet 名称。我不知道我在哪里做错了。所以,我发布了有效的 java 代码:
public class convert {
int i = 0, j = 0, k = 0;
Connection conn = null;
Connection connection = null;
static int count = 0;
// Following variables are required for assigning resultset values from
// excel spreadsheet
String name[] = null;
String Title[] = null;
Statement stmt1 = null;
ResultSet NumOfRows = null;
Statement stmt2 = null;
ResultSet SpreadsheetValues = null;
Statement stmt3 = null;
ResultSet rs3 = null;
int Rowcount = 0;
// this static function required to connect database
static {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(spreadsheet2db.class.getName()).log(
Level.SEVERE, null, ex);
}
}
// connect Sql database
void ConnectSqlDB() throws SQLServerException, SQLException {
// code
}
void ConnectExcelDB() throws SQLException {
conn = DriverManager.getConnection("jdbc:odbc:condb", "", "");
}
// getRowcount() will return number of rows present in spreadsheet
// Result of rowcount is used for array size
void getRowcount() throws SQLException {
// System.out.println("Number of rows in spreadsheet");
// System.out.println(Rowcount);
}
void sheetValues() throws SQLException {
stmt2 = conn.createStatement();
// ExcelQueryString2 will give values of attributes
while (SpreadsheetValues.next()) {
// Assigning Spread sheet values to String array
Cname[j] = SpreadsheetValues.getString("name");
Title[j] = SpreadsheetValues.getString("Title");
j++;
}
}
public static void main(String args[]) throws SQLServerException,
SQLException {
convert a = new convert();
a.ConnectSqlDB();
a.ConnectExcelDB();
a.getRowcount();
a.sheetValues();
}
}
我想知道如何将此代码转换为 Servlet?