how to solve the constructor problem? " return new Accounts
Code Java:
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;
}
}
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?
Re: how to solve the constructor problem? " return new Accounts
Code java:
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
Code java:
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
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.
Code java:
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.
Code java:
return new AccountsDB(ic, lName, fName, add, Phone, cPIN, eMail, bDate, password, gen);
Let me know how it goes :)