Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to solve the constructor problem? " return new Accounts

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to solve the constructor problem? " return new Accounts

    package classes;
    import java.util.*;
    import java.sql.*;
     
     
    public class AccountsDB
    {
        public static String addAccounts(String nric,
                                    String lastname,
                                    String firstname,
                                    String address,
                                    long phone,
                                    String clientPIN,
                                    String emailAddress,
                                    java.sql.Date birthDate,
                                    String password,
                                    byte gender
                                    )
        {
            Connection conn = null;
            Statement stmt = null;
     
            try
            {
                 // 1. initialize database driver
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
                // 2. Create a connection
                String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
                                  + "DBQ=MovieAccountDB.mdb;";
                conn = DriverManager.getConnection(database, "", "");
     
                // 3. Create a statement.
                stmt = conn.createStatement();
     
                // 4. Prepare a query
                String sqlQuery = "INSERT INTO Accounts VALUES("
                                  + "'" + nric + "',"
                                  + "'" + firstname + "',"
                                  + "'" + lastname + "',"
                                  + "'" + address + "',"
                                  + phone + ","
                                  + "'" + clientPIN + "',"
                                  + "'" + emailAddress + "',"
                                  + birthDate
                                  + "'" + password + "',"
                                  + gender + ","
                                  + ")";
     
                // 5. Execute the query
                stmt.execute(sqlQuery);
     
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return ex.toString();
            }
            finally
            {
                try
                {
                    stmt.close();
                    conn.close();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
     
            return null;
        }
     
     
     
        private static HashMap<Byte,String> retrieveMap(String tableName)
        {
            Connection conn = null;
            Statement stmt = null;
     
            HashMap<Byte,String> resultMap = new HashMap<Byte,String>();
     
            try
            {
                 // 1. initialize database driver
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
                // 2. Create a connection
                String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
                                  + "DBQ=MovieAccountDB.mdb;";
                conn = DriverManager.getConnection(database, "", "");
     
                // 3. Create a statement.
                stmt = conn.createStatement();
     
                // 4. Prepare a query
                String selTable = "SELECT * FROM " + tableName;
     
                // 5. Execute the query and get result
                stmt.execute(selTable);
                ResultSet rs =  stmt.getResultSet();
     
                while ((rs != null) && (rs.next()))
                {
                    byte sn = rs.getByte(1);
                    String gender = rs.getString(2);
     
                    resultMap.put(new Byte(sn), gender);
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            finally
            {
                try
                {
                    stmt.close();
                    conn.close();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
     
            return resultMap;
        }
     
        public static HashMap<Byte,String> retrieveGender()
        {
            return retrieveMap("Gender");
        }
     
        public static Accounts validateClient(String nric, String userPIN)
        {
            Connection conn = null;
            Statement stmt = null;
     
            try
            {
                // 1. Initialize the database driver
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
                // 2. Create a connection
                String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
                                  + "DBQ=MovieAccountDB.mdb;";
                conn = DriverManager.getConnection(database, "", "");
     
                // 3. Create a statement.
                stmt = conn.createStatement();
     
                // 4. Prepare a query
                String selQuery = "SELECT * FROM Accounts" +
                                  " WHERE nric = '" + nric + "'" +
                                  " AND clientPIN = '" + userPIN + "'";
     
                // 5. Execute the query and get result
                stmt.execute(selQuery);
                ResultSet rs =  stmt.getResultSet();
     
                if ((rs != null) && (rs.next()))
                {
                   String ic = rs.getString("nric");
                   String lName = rs.getString("lastname");
                   String fName = rs.getString("firstname");
                   String add = rs.getString("address");
                   long Phone = rs.getLong("Phone");
                   String cPIN = rs.getString("PinNumber");
                   String eMail = rs.getString("Email");
                   java.sql.Date bDate = rs.getDate("birthDate");
                   String password = rs.getString("Password");
                   byte gen = rs.getByte("gender");
     
     
     
                   return new Accounts(ic,
                                         lName,
                                         fName,
                                         add,
                                         Phone,
                                         cPIN,
                                         eMail,
                                         bDate,
                                         password,
                                         gen
                                         );
                }
            }
            catch (Exception ex)
            {
     
            }
            finally
            {
                try
                {
                    stmt.close();
                    conn.close();
                }
                catch (Exception ex)
                {
     
                }
     
            }
     
            return null;
        }
     
     
    }
    Last edited by helloworld922; May 10th, 2011 at 11:50 AM.


  2. #2
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: how to solve the constructor problem? " return new Accounts

    Can you edit your post and put your code into a set of highlight bracket please. It makes code much more readable.

    ["highlight=java"] code goes here ["/highlight"]

    Just remove the quotes...
    Also, what exactly is the problem?
    while(true)
    {
            codeInJava();
    }

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to solve the constructor problem? " return new Accounts

    package classes;
    import java.util.*;
    import java.sql.*;
     
     
    public class AccountsDB
    {
    public static String addAccounts(String nric,
    String lastname,
    String firstname,
    String address,
    long phone,
    String clientPIN,
    String emailAddress,
    java.sql.Date birthDate,
    String password,
    byte gender
    )
    {
    Connection conn = null;
    Statement stmt = null;
     
    try
    {
    // 1. initialize database driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
    // 2. Create a connection
    String database = "jdbcdbcriver={Microsoft Access Driver (*.mdb)};"
    + "DBQ=MovieAccountDB.mdb;";
    conn = DriverManager.getConnection(database, "", "");
     
    // 3. Create a statement.
    stmt = conn.createStatement();
     
    // 4. Prepare a query
    String sqlQuery = "INSERT INTO Accounts VALUES("
    + "'" + nric + "',"
    + "'" + firstname + "',"
    + "'" + lastname + "',"
    + "'" + address + "',"
    + phone + ","
    + "'" + clientPIN + "',"
    + "'" + emailAddress + "',"
    + birthDate
    + "'" + password + "',"
    + gender + ","
    + ")";
     
    // 5. Execute the query
    stmt.execute(sqlQuery);
     
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    return ex.toString();
    }
    finally
    {
    try
    {
    stmt.close();
    conn.close();
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    }
     
    return null;
    }
     
     
     
    private static HashMap<Byte,String> retrieveMap(String tableName)
    {
    Connection conn = null;
    Statement stmt = null;
     
    HashMap<Byte,String> resultMap = new HashMap<Byte,String>();
     
    try
    {
    // 1. initialize database driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
    // 2. Create a connection
    String database = "jdbcdbcriver={Microsoft Access Driver (*.mdb)};"
    + "DBQ=MovieAccountDB.mdb;";
    conn = DriverManager.getConnection(database, "", "");
     
    // 3. Create a statement.
    stmt = conn.createStatement();
     
    // 4. Prepare a query
    String selTable = "SELECT * FROM " + tableName;
     
    // 5. Execute the query and get result
    stmt.execute(selTable);
    ResultSet rs = stmt.getResultSet();
     
    while ((rs != null) && (rs.next()))
    {
    byte sn = rs.getByte(1);
    String gender = rs.getString(2);
     
    resultMap.put(new Byte(sn), gender);
    }
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    try
    {
    stmt.close();
    conn.close();
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    }
     
    return resultMap;
    }
     
    public static HashMap<Byte,String> retrieveGender()
    {
    return retrieveMap("Gender");
    }
     
    public static Accounts validateClient(String nric, String userPIN)
    {
    Connection conn = null;
    Statement stmt = null;
     
    try
    {
    // 1. Initialize the database driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
    // 2. Create a connection
    String database = "jdbcdbcriver={Microsoft Access Driver (*.mdb)};"
    + "DBQ=MovieAccountDB.mdb;";
    conn = DriverManager.getConnection(database, "", "");
     
    // 3. Create a statement.
    stmt = conn.createStatement();
     
    // 4. Prepare a query
    String selQuery = "SELECT * FROM Accounts" +
    " WHERE nric = '" + nric + "'" +
    " AND clientPIN = '" + userPIN + "'";
     
    // 5. Execute the query and get result
    stmt.execute(selQuery);
    ResultSet rs = stmt.getResultSet();
     
    if ((rs != null) && (rs.next()))
    {
    String ic = rs.getString("nric");
    String lName = rs.getString("lastname");
    String fName = rs.getString("firstname");
    String add = rs.getString("address");
    long Phone = rs.getLong("Phone");
    String cPIN = rs.getString("PinNumber");
    String eMail = rs.getString("Email");
    java.sql.Date bDate = rs.getDate("birthDate");
    String password = rs.getString("Password");
    byte gen = rs.getByte("gender");
     
     
     
    return new Accounts(ic,
    lName,
    fName,
    add,
    Phone,
    cPIN,
    eMail,
    bDate,
    password,
    gen
    );
    }
    }
    catch (Exception ex)
    {
     
    }
    finally
    {
    try
    {
    stmt.close();
    conn.close();
    }
    catch (Exception ex)
    {
     
    }
     
    }
     
    return null;
    }
     
     
    }




    this is where the problem starts at in reference to the above mentioned code "Accounts" is underlined with the red line
    return new Accounts        (ic,
                                         lName,
                                         fName,
                                         add,
                                         Phone,
                                         cPIN,
                                         eMail,
                                         bDate,
                                         password,
                                         gen
                                         );

    this was the so called hint which i cant understand
    cant find symbol
    symbol : constructor Accounts (java.lang.string ...... etc)
    location: classclasses.Account
    Last edited by defyzer; May 10th, 2011 at 10:12 AM.

  4. #4
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: how to solve the constructor problem? " return new Accounts

    Someone with more experience can correct me on this but I would like to hazard a guess that its because you have no actual constructor made in your AccountsDB class. "return new Account (....) " is not recognized because you haven't declared a constructor that takes all those parameters. You have an addAccounts() method that returns a string but your return statement in the validate method is looking for a constructor. Also, I think its called wrong. It should be "return new AccountsDB(...)". My advice, but again, I could be wrong, is to do the following:

    Declare the variables you have in addAccounts() as instance variables for the class. Then create a constructor to initialize those variables.
    AccountsDB(String lName, String fName, String addr, long pNum, String cPin,
                                   String eMail, java.sql.Date bDate, String pass, byte gend)
    {
            this.lName = lName;
            this.fName = fName;
            this.address = addr;
            this.phone = pNum;     
            this.emailAddress = eMail;
            this.clientPin = cPin;
            this.birthDate = bDate;   
            this.password = pass;
            this.gender = gend;
    }

    After that, then the following should work. Again, I would like someone more experienced to correct me if I'm wrong.

    return new AccountsDB(ic, lName, fName, add, Phone, cPIN, eMail, bDate, password, gen);

    Let me know how it goes
    Last edited by TopdeK; May 11th, 2011 at 05:40 PM.
    while(true)
    {
            codeInJava();
    }

Similar Threads

  1. help me to solve my problem
    By miszIna in forum Object Oriented Programming
    Replies: 3
    Last Post: February 14th, 2011, 09:40 AM
  2. Plz solve the problem
    By rasheedmgs in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 14th, 2010, 11:59 AM
  3. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM
  4. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM