-1

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?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
user998461
  • 73
  • 1
  • 4
  • 13
  • You should read more about servlets at http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html and then convert the code *on your own* – Manish Dec 27 '11 at 06:39
  • You don't need to convert it (apart from several bugfixes in the class; you've there some pretty major bugs which makes it threadunsafe and resource-leaking). You just let your servlet call it the usual Java way. – BalusC Dec 27 '11 at 13:40

2 Answers2

2

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.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
1

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.

Swagatika
  • 3,376
  • 6
  • 30
  • 39