如何将此 java 代码转换为 servlet
我是 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?
I am new to servlet. I am trying to connect to database using java with JDBC and OJDBC. I have already written a java code for this. Now I need to run it on Tomcat server. So I have chosen servlet .I have done this using Netbeans IDE, there I chose servlet and I gave class name as servlet name in web.xml. I don't know where I did wrong.So, I am posting the working java code:
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();
}
}
I want to know how can I convert this code into Servlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要接受 servlet 请求,您需要将您的类扩展为 HttpServlet(来自 servlet-api.jar)并重写其 doGet() 方法和相应的
doPost()
。使用
POST
方法或GET
方法发送请求。使用哪种方法取决于您。
JDBC 连接是在
doGet()
或doPost()
或另一个重写方法init()
内部完成的,为此,您需要添加一个外部Jar(来自 apache 的
servlet-api.jar
)到您的项目。To entertain a servlet request you need to extend your class as
HttpServlet
(fromservlet-api.jar
) and override its method ofdoGet()
anddoPost()
accordingly.Request are sent using
POST
method orGET
method.It is up you which method do you use.
The JDBC connection is done inside
doGet()
ordoPost()
or another overridden methodinit()
For this you will need to add an External Jar (
servlet-api.jar
from apache) to your project.首先,您必须首先了解 servlet 的基础知识。也许您可以参考一些简单的 Servlet 教程,也可能是一些示例项目。
以下是 Oracle 的 Java 教程的链接:
http://docs.oracle.com/javaee/5/tutorial/doc /bnafd.html
在我看来,首先尝试使一个简单的服务工作。
可能只是打印“Hello World”。一旦您清楚了 Sevlet 的工作原理,您就可以尝试与代码的其他部分(例如 JDBC 细节)集成。
还要确保您的 JDBC 部分可以自行工作。
由于您是 Servelt 新手,因此在调试大型 Serrvlet 类时会遇到困难,因此要使其简单并尝试弄清楚基础知识。
To start with you have to understand the basics of servlet first. May be you can refer to some simple Servlet tutorials and may be some sample project.
Here is the link for Java Tutorial from Oracle :
http://docs.oracle.com/javaee/5/tutorial/doc/bnafd.html
In my opinion first try to make a simple servet work.
May be just printing a 'Hello World'. Once you are clear on how a Sevlet works, then you can try to integrate with other part of code like JDBC details.
Also make sure your JDBC part works by itself.
Since you are new to Servelt, you will have difficulties in debugging large Serrvlet classes, make it simple and try to get the basics clear.