Here's code :

        public Connection m_con = null;

        // Name of the DSN
        public final String DSN_NAME = "data_source_name";

        // Userid for the connection
        // public final String UID = "user_name";

        // Password for the connection
        // public final String PASSWORD = "";

        // Statement
        Statement stmt = null;
/**
 *
 * Connect to the DataBase.
 *
 * @param    strDsnName - the input DSN name string.  Cannot be 0-length.
 * @return   <b>void</b>.
 * @exception UnavailableException. non-recoverable.
 *
 */

        public void connect(String strDsnName) throws UnavailableException {
                try {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  // Load the 
jdbc-odbc
bridge
                        String strDbName = "jdbc:odbc:" + strDsnName;   // the jdbc 
database name
                        m_con = (Connection)DriverManager.getConnection(strDbName, 
"sa", "");
                } catch(ClassNotFoundException cnFex) { // Catch the class not found
exception
                        throw new UnavailableException(this, "Couldn't find class");
                } catch (SQLException sqle) { // Catch the SQLException
                        throw new UnavailableException(this, "Couldn't connect to DB" 
);
                }
        }


/**
 *
 * Close DataBase Connection.
 *
 * @param    <b>None</b>.
 * @return   <b>void</b>.
 * @exception Throws SQLException.
 *
 */

        public void conClose() throws SQLException { // function to close connection
                //method to close the connection
                if (m_con != null && m_con.isClosed()) {
                        m_con.close();
                        System.out.println("Connection closed..!!  :-)");
                }
        }

/**
 * This function executes a query and prints the query results.
 *
 * @param    aSource - the input source string.  Cannot be 0-length.
 * @return   Possible values are 1..n.
 * @exception ResourceNotFoundException. recoverable, try another resource
 *
 */

        public ResultSet doQuery(String strQuery) {
                ResultSet rs = null;    // Resultset object
                try {
                        stmt = m_con.createStatement(); // Create the Statement
                        rs = stmt.executeQuery(strQuery);       // Execute query
                } catch(SQLException w) { // Catch sql exceptions
                        w.printStackTrace();
                }
                return rs;
        }



Hope it is selfexplanatory,
Pankaj
"Marvin B. Garcia" <[EMAIL PROTECTED]> wrote:
Hello!!!

Is any out there could help me on how to make an servlet that access the
MS-SQL server.
Or any open source code that could lead me on how to develop an application
that utilized the database.
I'm just download the sample program in the sun homepage n' regading the
Duke's bookstore. I'd like to modify it in a way that
it uses a database. Pls! help me on how could i develop it...  honestly, i
was very new in the database programming and i really
like to learn it.. :)

thanks :)

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html


____________________________________________________________________
Get free email and a permanent address at http://www.netaddress.com/?N=1

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to