如何将此 java 代码转换为 servlet

发布于 2024-12-23 11:00:56 字数 2251 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云悠悠 2024-12-30 11:00:56

要接受 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 (from servlet-api.jar) and override its method of doGet() and doPost() accordingly.

Request are sent using POST method or GET method.
It is up you which method do you use.

The JDBC connection is done inside doGet() or doPost() or another overridden method init()

For this you will need to add an External Jar (servlet-api.jar from apache) to your project.

寂寞陪衬 2024-12-30 11:00:56

首先,您必须首先了解 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文