Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Package Problem!!!!!


Message #1 by "Low Kok Leong" <lousai@t...> on Sun, 5 Nov 2000 05:56:30 -0000
Hi,
 currently I have downloaded a sample application using package to
establish a connection. But I seem to get error while compiling the codes.
Can you help me on this.
-------------------------------Start Code--------------------------------

DatabaseTransactions.java
package scart;

import java.sql.*;
import java.util.*;

public class DatabaseTransactions {

    public DatabaseTransactions() { }

    public ResultSet getBooksSortedByAuthor() throws SQLException {
        String query 
            "SELECT b.bookID, b.title, a.fname, a.lname, b.price " +
            "FROM book = b, author = a " +
            "WHERE b.authID = a.authID " +
            "ORDER BY a.lname, a.fname, b.title";

        return DatabaseConnection.executeQuery(query);
    }

    public ResultSet getBooksSortedByTitle() throws SQLException {
        String query 
            "SELECT b.bookID, b.title, a.fname, a.lname, b.price " +
            "FROM book = b, author = a " +
            "WHERE b.authID = a.authID " +
            "ORDER BY b.title, a.lname, a.fname";

        return DatabaseConnection.executeQuery(query);
    }

    public ResultSet getBookDetails(String bookID) throws SQLException {
        String query 
            "SELECT b.title, a.fname, a.lname, b.price," +
            "d.yearOfPub, d.description, d.coverImage " +
            "FROM book = b, author = a, details = d " +
            "WHERE b.authID = a.authID " +
            "AND b.bookID = d.bookID " +
            "AND b.bookID = " + bookID;

        return DatabaseConnection.executeQuery(query);
    }

}
-----------------------------------end
DatabaseTransactions.java-----------

DatabaseConnection.java(this working)
package scart;

import java.sql.*;
import java.util.*;

public class DatabaseConnection {
    static final String url = "jdbc:msql://localhost:4333/demo";
    static final String user = "demo";
    static final String pass = "";
    static final String jdbcclass = "com.imaginary.sql.msql.MsqlDriver";
    static Connection conn;

    static {
        try {
            Class.forName(jdbcclass).newInstance();
            conn = DriverManager.getConnection(url, user, pass);
        }
        catch(Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public static Statement createStatement() throws SQLException {
        if(conn != null)
            return conn.createStatement();
        else
            return null;
    }

    public static ResultSet executeQuery(String query) throws SQLException
{
        if(conn != null) {
            Statement stmt = createStatement();

            if(stmt == null) {
                return null;
            } else {
                ResultSet rs = stmt.executeQuery(query);
                stmt.close();
                return rs;
            }
        } else {
            return null;
        }
    }

    protected void finalize() throws Throwable {
        if(conn != null)
            conn.close();
    }
}
-------------------------------End Code-----------------------------------

Thank You.

  Return to Index