Я новичок в сервлете. Я пытаюсь подключиться к базе данных, используя Java с JDBC и OJDBC. Я уже написал код Java для этого. Теперь мне нужно запустить его на сервере Tomcat. Поэтому я выбрал сервлет. Я сделал это с помощью IDE Netbeans, там я выбрал сервлет и дал имя класса в качестве имени сервлета в web.xml. Я не знаю, где я сделал неправильно. Итак, я публикую рабочий код 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();
}
}
Я хочу знать, как я могу преобразовать этот код в сервлет?