Results 1 to 3 of 3
  1. #1
    notnumber6 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Red face Database Wrapper Questions

    Just a quicky, currently constructing a project for university and come up against a bit of a hurdle. I have a class called DatabaseConnect, now this connects to a MYSQL database, and has several methods, such as update etc... The problem comes when I call any of these methods from in another class, the class itself is public and i realise how to call a method from another class using:

    Java Code:
    DatabaseConnect.DatabaseConnect;
    I could always extends the login class, which would allow me to access all these, but has anyone a better idea?

    P.s. if relevant here is the full source code:

    Java Code:
    import java.sql.*;
    /**
     *
     * @author g9015395
     */
    public class DatabaseConnect
    {
        private Connection mysqlConnection;
        private Statement mysqlStatement;
        private ResultSet queryResultSet;
     
        public DatabaseConnect(String username, String password) throws SQLException
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                mysqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/footballLeague",
                username, password);
                mysqlStatement = mysqlConnection.createStatement();
            } catch(ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
        public String hasDatabaseConnection() throws SQLException
        {
            String databaseStatus;
            databaseStatus = "closed";
            if(mysqlConnection.isClosed()== false)
            {
                databaseStatus = "Open";
                return databaseStatus;
            }
            else
            {
                return databaseStatus;
            }
        }
        public void getDatabaseInfomation() throws SQLException
        {
            DatabaseMetaData mysqlDatabaseMetaData = mysqlConnection.getMetaData();
            System.out.println("Database: "+ mysqlDatabaseMetaData.getDatabaseProductVersion());
            System.out.println("Driver: "+ mysqlDatabaseMetaData.getDriverVersion());
            System.out.println("URL: "+mysqlDatabaseMetaData.getURL()+ "Username: "+mysqlDatabaseMetaData.getUserName());
        }
        public void runSelectQuery(String mysqlSelectUserStatement) throws SQLException
        {
            if(mysqlSelectUserStatement.isEmpty()==false)
            {
                queryResultSet = mysqlStatement.executeQuery(mysqlSelectUserStatement);
            }
            else
            {
                System.out.println("An error has occured, no SQL given");
            }
        }
        public void runDeleteQuery(String mysqlUserDeleteUserStatement) throws SQLException
        {
            if(mysqlUserDeleteUserStatement.isEmpty()==false)
            {
                mysqlStatement.executeUpdate(mysqlUserDeleteUserStatement);
            }
            else
            {
                System.out.println("An error has occured, no SQL given");
            }
        }
        public void runUpdateQuery(String mysqlUserUpdateUserStatement) throws SQLException
        {
            if(mysqlUserUpdateUserStatement.isEmpty()==false)
            {
                mysqlStatement.executeUpdate(mysqlUserUpdateUserStatement);
            }
            else
            {
                System.out.println("An error has occured, no SQL given");
            }
        }
        public void runInsertQuery(String mysqlUserInsertUserStatement) throws SQLException
        {
            if(mysqlUserInsertUserStatement.isEmpty()==false)
            {
                mysqlStatement.executeUpdate(mysqlUserInsertUserStatement);
            }
            else
            {
                System.out.println("An error has occured, no SQL given");
            }
        }
        public void closeDatabaseConnection() throws SQLException
        {
            mysqlStatement.close();
            mysqlConnection.close();
        }
    }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    Can you post the code how do you use those methods with created object in another class?

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    12

Similar Threads

  1. Yet another Wrapper Class confused guy.
    By JAdeline in forum New To Java
    Replies: 2
    Last Post: 08-15-2008, 04:04 AM
  2. Secure FTP Wrapper 3.0.3
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-23-2008, 01:56 PM
  3. Wrapper Class
    By haiforhussain in forum New To Java
    Replies: 6
    Last Post: 06-24-2008, 08:08 AM
  4. Secure FTP Wrapper 3.0.2
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-29-2008, 06:05 PM
  5. wrapper classes
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 12-11-2007, 09:45 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
  •