Results 1 to 5 of 5
  1. #1
    simontkk2005 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default Database declaration

    hi..i `m still new to database connection

    i would like to know,is there any way to declare the connection of database in a class.

    when i need the connection in others class,then i might be able to call it out.and without redeclare it again in new class.

    thx

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Google DAO layer.

  3. #3
    roy.jaffray is offline Member
    Join Date
    Dec 2009
    Location
    Edmonton, Alberta, Canada
    Posts
    1
    Rep Power
    0

    Default These 3 classes from book JDBC: Practical Guide for Java Programmers

    Book is by Gregory D. Speegle .. very good .. very flexible



    import java.sql.*;

    public class ConnectionJDBC {

    public Connection makeConnection()
    throws SQLException {
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (ClassNotFoundException e) {
    throw new SQLException("Unable to load driver class");
    }
    return DriverManager.getConnection("jdbc:odbc:jdbc_book") ;
    }

    public Connection makeConnection(String URL)
    throws SQLException {
    return DriverManager.getConnection(URL);
    }

    public Connection makeConnection(String DriverName, String URL)
    throws SQLException {
    try {
    Class.forName(DriverName);
    } catch (ClassNotFoundException e) {
    throw new SQLException("Unable to load driver class");
    }
    return DriverManager.getConnection(URL);
    }

    public Connection makeConnection(String URL, String username,
    String password)
    throws SQLException {
    return DriverManager.getConnection(URL,username,password) ;
    }

    public Connection makeConnection(String DriverName, String URL,
    String username, String password)
    throws SQLException {
    try {
    Class.forName(DriverName);
    } catch (ClassNotFoundException e) {
    throw new SQLException("Unable to load driver class");
    }
    return DriverManager.getConnection(URL,username,password) ;
    }

    public void closeConnection(Connection c, Statement s)
    {
    try {
    if (s != null) s.close();
    if (c != null) c.close();
    } catch (SQLException sqlex) {}
    }

    public static void main(String args[]) {
    ConnectionJDBC CJ = new ConnectionJDBC();
    Connection dbConnect = null;
    Statement dbStatement = null;
    try {
    switch (args.length) {
    case 0 : dbConnect = CJ.makeConnection();
    break;
    case 1 : dbConnect = CJ.makeConnection(args[0]);
    break;
    case 2 : dbConnect = CJ.makeConnection(args[0],args[1]);
    break;
    case 3 : dbConnect = CJ.makeConnection(args[0],args[1],args[2]);
    break;
    case 4 : dbConnect = CJ.makeConnection(args[0],args[1],args[2],
    args[3]);
    break;
    default :
    System.out.println("Using the default driver");
    dbConnect = CJ.makeConnection();
    }
    System.out.println("Made a connection!");
    dbStatement = dbConnect.createStatement();
    System.out.println("Made a statement!");
    } catch (SQLException sqlex) {
    System.out.println(sqlex.getMessage());
    }
    finally {
    CJ.closeConnection(dbConnect,dbStatement);
    System.out.println("Closed the connection.");
    }
    }
    }


    import java.sql.*;

    public class SimpleJDBC extends ConnectionJDBC {
    public static void main(String args[]) {
    if (args.length != 1) {
    System.out.println("Usage: java SimpleJDBC custid");
    System.exit(1);
    }

    String query = "Select Title, Type " +
    "from Orders O, Titles T, Tapes V " +
    "where V.TapeId=O.TapeId and T.TitleId=V.TitleId and " +
    "Status = 'O' and " +
    "O.CustomerID= " + args[0];

    SimpleJDBC J = new SimpleJDBC();
    Connection dbConnect = null;
    Statement dbStatement = null;
    ResultSet dbRS = null;
    try {
    dbConnect = J.makeConnection(); //from ConnectionJDBC
    dbStatement = dbConnect.createStatement();
    dbRS = dbStatement.executeQuery(query);
    J.presentResultSet(dbRS);
    } catch (SQLException sqlex) {
    System.out.println(sqlex.getMessage());
    } finally {
    J.closeConnection(dbConnect,dbStatement); //from ConnectionJDBC
    }
    }

    public void presentResultSet(ResultSet rs)
    throws SQLException {
    if (!rs.next()) System.out.println("No records for customer");
    else {
    do {
    System.out.println(rs.getString("Title") + ": " +
    rs.getString("Type"));
    } while (rs.next());
    }
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default

    Not terribly impressed.

    He doesn't use bind variables (ie a PreparedStatement), and even worse he doesn't close the result set in the finally block. Assuming the result set will be closed on closing the statement is poor practice.

  5. #5
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    Hi.
    Try to Google "Database Connection Classes".
    RAQ Report: free Java reporting tool.

Similar Threads

  1. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 05:38 AM
  2. public declaration ('undeclaring')
    By Jadellll in forum New To Java
    Replies: 2
    Last Post: 03-17-2008, 08:50 PM
  3. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 10:37 PM
  4. JSP Declaration Directive
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05:42 PM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •