create new object using the database - java

I want to use my database in order to do the addmodule function but it has to be in a loop and not just 1 module, i want it tot add all data from database to create the addModule function. My database has the relevant information but i dont know how to get it into addmodule function to create new object(module)
timetable.addModule(1, "cs1", "Computer Science", new int[] {1, 2});
and this is my addModule:
public void addModule(int moduleId, String moduleCode, String module, int professorIds[]) {
this.modules.put(moduleId, new Module(moduleId, moduleCode, module, professorIds));
}
What would the prepared statement be for adding module using the database? but with array so adds all of it

You can create a model class to store the database records.
public class Course {
int moduleId;
String moduleCode;
String module;
List<Integer> professorIds;
public Course() {
}
public Course(int moduleId, String moduleCode, String module, List<Integer> professorIds) {
this.moduleId = moduleId;
this.moduleCode = moduleCode;
this.module = module;
this.professorIds = professorIds;
}
public int getModuleId() {
return moduleId;
}
public void setModuleId(int moduleId) {
this.moduleId = moduleId;
}
public String getModuleCode() {
return moduleCode;
}
public void setModuleCode(String moduleCode) {
this.moduleCode = moduleCode;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public List<Integer> getProfessorIds() {
return professorIds;
}
public void setProfessorIds(List<Integer> professorIds) {
this.professorIds = professorIds;
}
}
Use the following way to instantiate your object with the database values. I assumed that you have a comma separated values list for professorIds in your database table. Also, assumed the data type of professorIds column is VARCHAR.
String query = "SELECT moduleId, moduleCode, module, professorIds from Course";
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs = null;
// initialize the conn variable before execute the query
// use a try block to handle exceptions properly
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
List<Course> courseList = new ArrayList<Course>();
while (rs.next()) {
Course course = null;
List<String> professorIdsStrList = Arrays.asList(rs.getString("professorIds").split("\\s*,\\s*"));
List<Integer> professorIdsIntList = professorIdsStrList.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
course = new Course(rs.getInt("moduleId"), rs.getString("moduleCode"), rs.getString("module"), professorIdsIntList);
courseList.add(course);
}
You can use your method in the following way to add modules.
// use this method call inside the database records reading method
addModule(rs.getInt("moduleId"), rs.getString("moduleCode"), rs.getString("module"), professorIdsIntList));
public void addModule(int moduleId, String moduleCode, String module, List<Integer> professorIds) {
this.modules.put(moduleId, new Module(moduleId, moduleCode, module, professorIds));
}
Hope this helps you!!!
Use the following to update the modules in TimeTable.
int index = 1;
while (rs.next()) {
...
timetable.modules.put(index, module);
index++;
...
}

Related

Cannot create JTable due to being unable to resolve constructor error

In my code, I have made a Rentals class as shown here:
public class Rentals {
protected int RentalID;
protected int CustomerID;
protected int MovieID;
protected Date DateRented;
protected Date DateDue;
public Rentals(int rentalID, int customerID, int movieID, Date dateRented, Date dateDue) {
RentalID = rentalID;
CustomerID = customerID;
MovieID = movieID;
DateRented = dateRented;
DateDue = dateDue;
}
public int getRentalID() {
return RentalID;
}
public void setRentalID(int rentalID) {
RentalID = rentalID;
}
public int getCustomerID() {
return CustomerID;
}
public void setCustomerID(int customerID) {
CustomerID = customerID;
}
public int getMovieID() {
return MovieID;
}
public void setMovieID(int movieID) {
MovieID = movieID;
}
public Date getDateRented() {
return DateRented;
}
public void setDateRented(Date dateRented) {
DateRented = dateRented;
}
public Date getDateDue() {
return DateDue;
}
public void setDateDue(Date dateDue) {
DateDue = dateDue;
}
}
In the main class, I create 2 array lists, RentalList and Columns, consisting of Rentals and Strings respectively. I also create a JTable called Display for later use. I then make a connection to my MS Azure SQL Database, and extract the relevant data into the two aforementioned array lists. After retrieving this data, I tried to use the array lists to add parameters to the Display JTable, following this Java Swing JTable Tutorial (Timestamped), but I kept getting this error:
java: no suitable constructor found for JTable(java.util.ArrayList<com.company.Rentals>,java.util.ArrayList<java.lang.String>)
constructor javax.swing.JTable.JTable(javax.swing.table.TableModel,javax.swing.table.TableColumnModel) is not applicable
(argument mismatch; java.util.ArrayList<com.company.Rentals> cannot be converted to javax.swing.table.TableModel)
constructor javax.swing.JTable.JTable(int,int) is not applicable
(argument mismatch; java.util.ArrayList<com.company.Rentals> cannot be converted to int)
constructor javax.swing.JTable.JTable(java.util.Vector,java.util.Vector) is not applicable
(argument mismatch; java.util.ArrayList<com.company.Rentals> cannot be converted to java.util.Vector)
constructor javax.swing.JTable.JTable(java.lang.Object[][],java.lang.Object[]) is not applicable
(argument mismatch; java.util.ArrayList<com.company.Rentals> cannot be converted to java.lang.Object[][])
for the line with Display = new JTable(RentalList, Columns);.
Main class:
public class Main {
public static JTable Display;
public static ArrayList<Rentals> RentalList = new ArrayList<Rentals>();
public static ArrayList<String> Columns = new ArrayList<String>();
public static void main(String[] args) {
String ConnectionURL = "___"; //HIDDEN, IN THE ACTUAL CODE IT DOES CONNECT
ResultSet rs;
// Open a connection
try(Connection conn = DriverManager.getConnection(ConnectionURL);
Statement stmt = conn.createStatement();
) {
rs = stmt.executeQuery("SELECT * FROM [dbo].[Rentals]");
while (rs.next()) {
Rentals rental = new Rentals(0, 0, 0, null, null);
rental.setRentalID(rs.getInt(1));
rental.setCustomerID(rs.getInt(2));
rental.setMovieID(rs.getInt(3));
rental.setDateRented(rs.getDate(4));
rental.setDateDue(rs.getDate(5));
RentalList.add(rental);
}
rs = stmt.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'Rentals'");
while (rs.next()) {
Columns.add(rs.getString(1));
}
}
catch (SQLException e) {
e.printStackTrace();
}
Display = new JTable(RentalList, Columns);
}
I have tried writing it as Display = new JTable(RentalList.toString(), Columns);, but the error remains.
Does anyone know if I have done anything wrong, or if I am trying to insert parameters that do not fit the requirements, and if so how would I correct this?
As you can see from JTable's documentation there is no constructor accepting two (Array)Lists. There's for example one that accepts a Vector of Vectors (for the data) and a Vector of arbitrary objects (for the column names).
So, either you use one of the constructors available, which means that you'd need to convert your data, or what I'd prefer, create a custom table model.
public class RentalsTableModel extends AbstractTableModel {
private List<Rentals> data;
private List<String> columnNames = List.of("Rental ID", "Customer ID", "Movie ID", "Date Rented", "Date Due");
public RentalsTableModel(Collection<? extends Rentals> rentals) {
data = new ArrayList<>(rentals);
}
#Override
public Class<?> getColumnClass(int col) {
return (col == 3 || col == 4) ? Date.class : Integer.class;
}
#Override
public int getRowCount() { return data.size(); }
#Override
public int getColumnCount() { return columnNames.size(); }
#Override
public String getColumnName(int ix) { return columnNames.get(ix); }
#Override
public Object getValueAt(int row, int col) {
Rentals rental = data.get(row);
switch (col) {
case 0: return rental.getRentalID();
case 1: return rental.getCustomerID();
case 2: return rental.getMovieID();
case 3: return rental.getDateRented();
case 4: return rental.getDateDue();
}
return null;
}
}
With such a model you'd instantiate your JTable as follows
List<Rentals> rentals = ...;
RentalsTableModel model = new RentalsTableModel(rentals);
JTable table = new JTable(model);
Be warned: all code in here is untested and can contain errors and typos. It's just a sketch to give you an idea how it could look like. Of course, you can implement a more generic TableModel, or one for SQL-Queries, too.

How to Insert ArrayList data to the DataBase

Im try to insert data into Database using ArrayList.there is a Erro msg.
That is my Custmer.class method. this is what i got from when i going to pass ArrayList into another class.
incompatible types: ArrayList<String> cannot be converted to ArrayList<Inquiries>
I want to know how to do this using correct Using OOP concept
public void passingMsg(ArrayList<Inquiries> arrlist){
try {
System.out.println("Method "+arrlist);
String sq = "INSERT INTO Inquiries (name,mail,tp,msg)VALUES(?,?,?)";
PreparedStatement pr = con.prepareStatement(sq);
for(int i=0;i<arrlist.size();i++){
pr.setString(1,arrlist.get(i).getName());
pr.setString(2,arrlist.get(i).getMail());
pr.setString(3,arrlist.get(i).getTp());
pr.setString(4,arrlist.get(i).getMsg());
}
pr.executeQuery();//executeBatch();
} catch (SQLException ex) {
}
}
and this is how i get values from user
String name = txtName.getText();
String mail = txtEmail.getText();
String tp = txtTp.getText();
String msg = txtMsg.getText();
ArrayList<String> arrInq = new ArrayList<String>();
arrInq.add(name);
arrInq.add(mail);
arrInq.add(tp);
arrInq.add(msg);
Custmer c =new Custmer();
if( c.passingMsg(arrInq)){
try {
JOptionPane.showMessageDialog(this, "Successs!!");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Unsuccesss!!");
e.printStackTrace();
}
}
and this is my Inquiries.class :
public class Inquiries {
private String name;
private String mail;
private String tp;
private String msg;
public Inquiries(String name,String mail,String tp,String msg){
this.name = name;
this.mail = mail;
this.tp = tp;
this.msg = msg;
}
//
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getTp() {
return tp;
}
public void setTp(String tp) {
this.tp = tp;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Can Some one please explain whats wrong with this. please ?
Reason For Error
This was simply telling you that your types were incompatible for the operation you were trying to perform. In your passingMsg() method, you have its header as: public void passingMsg(ArrayList<Inquiries> arrlist). However, inside your "how i get values from user" area, which I will now refer to as "2nd Snippet", you have your method call declared as: if( c.passingMsg(arrInq)). This means that you are implying that your parameter being passed, arrInq in this case, is of the type ArrayList<Inquiries>, but it's not. It's being initialized in your 2nd Snippet as: ArrayList<String> arrInq = new ArrayList<String>();
Simple Fix
I take no responsibility for this code; use at your own risk. To fix this, you would want to change that entire 2nd Snippet to something similar to the following:
String name = txtName.getText();
String mail = txtEmail.getText();
String tp = txtTp.getText();
String msg = txtMsg.getText();
ArrayList<Inquiries> arrInq = new ArrayList<Inquiries>();
arrInq.add(new Inquiries(name, mail, tp, msg));
Custmer c = new Custmer();
try {
c.passingMsg(arrInq);
JOptionPane.showMessageDialog(this, "Successs!!");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Unsuccesss!!");
e.printStackTrace();
}
You would also want to change the method header to either return a boolean, or fix it up a little bit to actually throw the exception. Such as:
public void passingMsg(ArrayList<Inquiries> arrlist) {
System.out.println("Method " + arrlist);
String sq = "INSERT INTO Inquiries(name,mail,tp,msg) VALUES(?,?,?)";
PreparedStatement pr = con.prepareStatement(sq);
for (Inquiries inquiries : arrlist) {
pr.setString(1, inquiries.getName());
pr.setString(2, inquiries.getMail());
pr.setString(3, inquiries.getTp());
pr.setString(4, inquiries.getMsg());
}
pr.executeQuery();//executeBatch();
}
Let's talk in O-O-P way.
Here Inquiries is your model, model is nothing but simple class that has instance members and public methods to get and set value of model's instance variable.
Generally we put all database related operations code in their respective models.
e.g. I have model "Model" which typically maps to database table say it as "TableModel" ,I would do something like this:
public class Model{
private int id;
private String attr;
//other properties of the model
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
//other getters and setters
//here we write methods to performs database operations
public void save(){
//use "this" to get properties of object
//logic to save to this object in database table TableModel as record
}
public void delete(int id){
//logic to delete this object i.e. from database table TableModel
}
public Model get(int id){
//retrieve record from table TableModel with this id
}
//other methods to get data from database.
}
Now question is how I can use this in some another class. Let's say I have list of Model objects and I wish to insert them in to database.I will do it something like this:
public class AnotherClass{
public void someMethod(){
//create list of models objects e.g. get them from user interface
ArrayList<Model> models=new ArrayList<>();
for(int i=0;i<3;i++){
Model model=new Model();
model.setId(i);
model.setAttr("attr"+i);
models.add(model);
}
SomeOtherClass obj=new SomeOtherClass();
obj.insert(models);
}
}
public class SomeOtherClass{
//other code above.....
//my method that inserts each Model object in database
//Note: this is sample method , you should do it in optimized way
// e.g. batch insert
public void insert(ArrayList<Model> models){
for(Model myModel:models){
myModel.save();
}
}
//other code below.....
}
You are using the wrong type parameter for the ArrayList. Instead of ArrayList<String> you need ArrayList<Inquiries>. To fix the problem, you should remove this code ...
ArrayList<String> arrInq = new ArrayList<String>();
arrInq.add(name);
arrInq.add(mail);
arrInq.add(tp);
arrInq.add(msg);
... and replace it with this code:
ArrayList<Inquiries> arrInq = new ArrayList<Inquiries>();
arrInq.add(new Inquiries(name, mail, tp, msg));

Java Swing Object[][] getData() confusion

Hi I'm having some trouble getting started with a problem in a Java course learning Swing and starting on JTables and getting data into them. It's going to be hard to explain so I'm just going to post the code I was given, along with the question.
The question is:
The getData() method needs to return an Object[][] containing the data represented by the class.
The first class is MusicAlbum
class MusicAlbum {
private String id;
private String name;
private String genre;
private boolean isCompilation;
private int track_count;
public MusicAlbum(String id, String name, String genre, boolean isCompilation, int track_count) {
this.id = id;
this.name = name;
this.genre = genre;
this.isCompilation = isCompilation;
this.track_count = track_count;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGenre() {
return genre;
}
public boolean isCompilation() {
return isCompilation;
}
public int getTrackCount() {
return track_count;
}
public boolean equals(Object obj) {
if (obj instanceof MusicAlbum)
return this.id.equalsIgnoreCase(((MusicAlbum)obj).id);
return super.equals(obj);
}
}
The class I have to implement the methods in is MusicDataObject (at the bottom)
import java.util.Random;
import java.util.Scanner;
public class MusicDataObject {
private List<MusicAlbum> albums = new ArrayList<>();
private Random random = new Random(); // for generating IDs
public void addAlbum(MusicAlbum album) throws IllegalArgumentException {
if (searchAlbum(album.getId()) != null)
throw new IllegalArgumentException("Album ID is not new!");
albums.add(album);
}
public MusicAlbum searchAlbum(String id) {
for (MusicAlbum album : albums) {
if (album.getId().equalsIgnoreCase(id)) {
return album;
}
}
return null;
}
public MusicAlbum removeAlbum(String id) {
MusicAlbum album = searchAlbum(id);
albums.remove(album);
return album;
}
public void updateAlbum(MusicAlbum album)
throws IllegalArgumentException {
if (removeAlbum(album.getId()) == null)
throw new IllegalArgumentException("Album ID does not exist!");
addAlbum(album);
}
public String generateID() {
String formatter = "A%0" + (int)Math.ceil(Math.log10(albums.size() * 2) + 1) + "d";
String ID;
do {
ID = String.format(formatter, random.nextInt(albums.size() * 2 + 1));
} while (searchAlbum(ID) != null);
return ID;
}
public void saveData(String fileName) throws IOException {
// make sure that the file exists or try to create it
File fout = new File(fileName);
if (!fout.exists() && !fout.createNewFile())
return;
PrintWriter out = new PrintWriter(fout);
for (MusicAlbum album: albums) {
out.println(serializeAlbum(album));
}
out.close();
}
public String serializeAlbum(MusicAlbum album) {
return String.format(
"%s;%s;%s;%b;%d",
album.getId(),
album.getName(),
album.getGenre(),
album.isCompilation(),
album.getTrackCount());
}
public void loadFile(String fileName) throws FileNotFoundException {
albums = new ArrayList<>();
Scanner in = new Scanner(new File(fileName));
while (in.hasNext()) {
// --- split the next line with the character ";"
String line = in.nextLine();
String[] tokens = line.split(";");
// --- construct a new MusicAlbum using the resulting tokens. NOTE: This isn't very robust.
// If a line doesn't contain enough data or the data is invalid, this will crash
albums.add(new MusicAlbum(
tokens[0],
tokens[1],
tokens[2],
Boolean.parseBoolean(tokens[3]),
Integer.parseInt(tokens[4])
));
}
}
// ----- these methods need to be implemented
public Object[][] getData() {
// TODO
}
public String[] getColumnNames() {
// TODO
}
}
The sample data being used is in a txt file, formatted as so:
A01;Defiance;Soundtrack;true;24
A02;Insomniac;Punk Rock;false;14
A03;A Great Day For The Race;Gypsy Jazz;false;10
A04;Viva La Internet;Ska;false;31
A05;New Surrender;Rock;false;17
So basically it's this getData() method they want me to implement that is giving me grief. I don't fully understand what they want me to do, nor do I fully understand what the Object[][] does.
I hope I have been clear enough, and I will appreciate all help given. Also please try to explain things as best you can and dumb them down as much as possible, I'm new to a lot of this :)
Thanks for your time.
Object[][] is a 2-dimensional array. Each of its element is an Object[], a one-dimensional array.
Your task is to create a 2 dimensional array, having one element (Object[]) for each of your MusicAlbum. An Object[] should hold the properties of a MusicAlbum like id, name, genre, isCompilation and track_count.
You can create an object array like this:
Object[] arr = new Object[] { "some", "values", 23, true };
You can create a 2 dimensional array like this:
Object[][] arr2d = new Object[size][];
And you can iterate over all your MusicAlbums, create an Object[] for each of them containing the properties of that music album, and set it in the arr2d.
You can set/get elements of a 2-dimensional array just like any other arrays:
// Set first element:
arr2d[0] = arr;
// Get first element:
Object[] firstElement = arr2d[0];
The getColumnNames() method should just return a String[] (a String array) containing the column names, the names of the properties.
And it might be obvious but note that the order you return the column names and the order of the property values (in the elements of the Object[]) should be the same.

Different Connections to Different Database by checking Map size

I have a HashMap that look something like this-
HashMap<String, TableConnectionInfo> tableList
which means it's value is a Class TableConnectionInfo which looks something like this-
public class TableConnectionInfo {
public String url;
public String user;
public String password;
public String driver;
public String suffix;
public String sql;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
}
So Suppose, If I have two values in the above HashMap. That means, I need to make two different connections to two different database. And suppose if that map has three values, then I need to make three different connections to three different database.
In the main thread, I am populating the above map by reading it from the properties file like this and after that this map won't get modified.
for (String arg : databaseNames) {
TableConnectionInfo ci = new TableConnectionInfo();
String url = prop.getProperty(arg + ".url");
String user = prop.getProperty(arg + ".user");
String password = prop.getProperty(arg + ".password");
String driver = prop.getProperty(arg + ".driver");
String suffix = prop.getProperty(arg + ".suffix");
String sql = prop.getProperty(arg + ".sql");
ci.setUrl(url);
ci.setDriver(driver);
ci.setPassword(password);
ci.setSql(sql);
ci.setSuffix(suffix);
ci.setUser(user);
tableList.put(arg, ci);
}
Now I am passing this tableList map to various threads like this and it won't be modified (by making set calls) by any thread. Each thread will be using get method to get the required method.
for (int i = 0; i< 1000; i++) {
service.submit(new Task(tableList));
}
So in the run method I need to make different conenctions basis on the tableList size. So if tableList size is two, that means I need to make two different connections, callableStatements and methods to two different database.
Question:-
So is there any better way as compared to way I am doing in my run method to create different connections to database basis on tableList size ?
Below is my Task class that impelements Runnable Interface
class Task implements Runnable {
private Connection[] dbConnection = null;
private CallableStatement[] callableStatement = null;
private ArrayList<Method> methods[] = null;
private final HashMap<String, TableConnectionInfo> tableLists;
public Task(HashMap<String, TableConnectionInfo> tableList) {
this.tableLists = tableList;
}
#Override
public void run() {
try {
int j = 0;
dbConnection = new Connection[tableLists.size()];
callableStatement = new CallableStatement[tableLists.size()];
methods = new ArrayList[tableLists.size()];
for (TableConnectionInfo ci : tableLists.values()) {
dbConnection[j] = getDBConnection(ci.getUrl(), ci.getUser(), ci.getPassword(), ci.getDriver());
callableStatement[j] = dbConnection[j].prepareCall(ci.getSql());
methods[j] = getRequiredMethods(ci.getSuffix());
j++;
}
}
}
}
Make a connection to that database-
private Connection getDBConnection(String url, String username, String password, String driver) {
Connection dbConnection = null;
try {
Class.forName(driver);
dbConnection = DriverManager.getConnection(url, username, password);
}
return dbConnection;
}
Just to add here getRequiredMethods will get all the methodNames of a particular table. So suppose if tableList size is 1 then we will be having only one connection to that database so getRequiredMethods will get all the methods for that table1 and store it in a ArrayList. But suppose if tableList size is 2 then we will be having two different connections to two different database so that is the reason I made methods as an array so that it can hold methods for table 1 and methods for table 2.
Ok, I'm still not sure how the Task is meant to use the data it's getting. But, I would move the getConnection, getCallableStatement and getMethods() functions to methods on the TableConnectionInfo. You can simply create a Collection of TableConnectionInfo (initialised as you already have, storing in an ArrayList). Then the Runnable simply iterates through the TableConnectionInfo.
public class TableConnectionInfo {
private String url;
private String user;
private String password;
private String driver;
private String suffix;
private String sql;
private Connection connection;
<snip... getters and setters for the properties>
public Connection getConnection() {
// TODO create and return a connection
if (connection == null) {
// create the connection
}
return connection;
}
public CallableStatement getCallableStatement() {
// get the callable statement
return null;
}
public Collection<Method> getMethods() {
// Get the Methods
return null;
}
}
public class TableTask implements Runnable {
private Collection<TableConnectionInfo> tables;
public TableTask(Collection<TableConnectionInfo> tables) {
this.tables = tables;
}
#Override
public void run() {
for (TableConnectionInfo table : tables) {
// do something with table.getConnection(), or table.getCallableStatement()
// and/or table.getMethods()
}
}
}

modelling a customer class in java for a database application

I'm working on a java swing application which handles Customer management for a shop.
I'm trying to build it based on the mvc pattern, but actually i'm somewhat unexperienced in it.
Basically, theres a View with some Textfields for Customer Creation and of course the class called Customer. If the form is submitted an object of Customer is created and saved to the database.
Now, my problem was setting up a view to show all Customers in a JTable. Actually, not the view itself was the problem, but refreshing the view, when a new customer was added or a customer was changed.
Therefore i created a new class called "Customers" which had an arraylist with all customers in it and everytime a customer was created, it was added to this arraylist.
When the mainframe of my application started, an object of "Customers" was created, fetching all customers from my database, putting it in the arraylist just for the jtable.
the jtable was added to the object of customers as a listener and had an interface called CustomerListener implemented, which set a new model everytime the arraylist of customers changed.
mh okay, now i really have problems explaining what my problem is but, basically i thought this class "customers" was redundant, so i just added the arraylist and stuff to my "normal" "Customer" class:
package v1a;
import java.sql.ResultSet;
import java.util.ArrayList;
public class Customer {
public static final String KUNDENNUMMER = "Kundennummer";
public static final String ANREDE = "Anrede";
public static final String NACHNAME = "Nachname";
public static final String VORNAME = "Vorname";
public static final String PLZ = "PLZ";
public static final String ORT = "Ort";
public static final String STRASSE = "Strasse";
public static final String LAND = "Land";
public static final String TELEFON = "Telefon";
public static final String MOBIL = "Mobil";
public static final String EMAIL = "Email";
public static final String[] CUSTOMER_FIELDS = { KUNDENNUMMER, ANREDE, NACHNAME, VORNAME, PLZ, ORT, STRASSE, LAND, TELEFON, MOBIL, EMAIL };
private String kn, anrede, nachname, vorname, plz, ort, strasse, land, telefon, mobil = "", email = "";
private ArrayList<Customer> customers = new ArrayList<Customer>();
private ArrayList<ICustomerModelListener> listeners = new ArrayList<ICustomerModelListener>();
public Customer() {
getAllFromDatabase();
}
public Customer(String[] str) {
this.kn = str[0];
this.anrede = str[1];
this.nachname = str[2];
this.vorname = str[3];
this.plz = str[4];
this.ort = str[5];
this.strasse = str[6];
this.land = str[7];
this.telefon = str[8];
this.mobil = str[9];
this.email = str[10];
}
public void getAllFromDatabase(){
SQL.getInstance();
ArrayList<Customer> arrlist = new ArrayList<Customer>();
ResultSet rs = SQL.select("SELECT kundennummer, anrede, name, vorname, strasse, plz, ort, land, telefon, mobil, email FROM kunden");
try {
while(rs.next()){
String[] values = new String[Customer.CUSTOMER_FIELDS.length];
values[0] = String.valueOf(rs.getInt("kundennummer"));
values[1] = rs.getString("anrede");
values[2] = rs.getString("name");
values[3] = rs.getString("vorname");
values[4] = rs.getString("strasse");
values[5] = String.valueOf(rs.getInt("plz"));
values[6] = rs.getString("ort");
values[7] = rs.getString("land");
values[8] = rs.getString("telefon");
values[9] = rs.getString("mobil");
values[10] = rs.getString("email");
Customer c = new Customer(values);
arrlist.add(c);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
SQL.cleanup();
this.customers = arrlist;
for(ICustomerModelListener l : listeners){
l.CustomerChanged();
}
}
public ArrayList<Customer> getAll(){
return customers;
}
public void addCustomer(Customer customer){
customers.add(customer);
for(ICustomerModelListener l : listeners){
l.CustomerChanged();
}
}
public void addListener(ICustomerModelListener listener){
listeners.add(listener);
}
public static boolean knExists(int kn){
boolean bool = false;
SQL.getInstance();
ResultSet rs = SQL.select("SELECT kundennummer FROM kunden WHERE kundennummer = "+kn);
try {
while(rs.next()){
bool = true;
}
} catch (Exception e){
System.out.println(e.getMessage());
}
SQL.cleanup();
return bool;
}
public static int getFreeKn(){
SQL.getInstance();
ResultSet rs = SQL.select("SELECT kundennummer FROM kunden ORDER BY kundennummer DESC LIMIT 1");
int kn = 0;
try {
while(rs.next()){
kn = rs.getInt("kundennummer");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
kn++;
SQL.cleanup();
return kn;
}
public static Customer getByKn(int kn){
if(knExists(kn)){
SQL.getInstance();
ResultSet rs = SQL.select("SELECT * FROM kunden WHERE kundennummer = "+kn);
String[] values = new String[CUSTOMER_FIELDS.length];
try {
while(rs.next()){
values[0] = String.valueOf(rs.getInt("kundennummer"));
values[1] = rs.getString("anrede");
values[2] = rs.getString("name");
values[3] = rs.getString("vorname");
values[4] = String.valueOf(rs.getInt("plz"));
values[5] = rs.getString("ort");
values[6] = rs.getString("strasse");
values[7] = rs.getString("land");
values[8] = rs.getString("telefon");
values[9] = rs.getString("mobil");
values[10] = rs.getString("email");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
Customer customer = new Customer(values);
SQL.cleanup();
return customer;
} else {
return null;
}
}
public boolean save() {
SQL.getInstance();
boolean bool = SQL.saveUser(this.kn, this.anrede, this.nachname, this.vorname, this.plz, this.ort, this.strasse, this.land, this.telefon, this.mobil, this.email);
SQL.cleanup();
return bool;
}
public String getValue(String s){
switch (s){
case KUNDENNUMMER:
return this.kn;
case ANREDE:
return this.anrede;
case NACHNAME:
return this.nachname;
case VORNAME:
return this.vorname;
case PLZ:
return this.plz;
case ORT:
return this.ort;
case STRASSE:
return this.strasse;
case LAND:
return this.land;
case TELEFON:
return this.telefon;
case MOBIL:
return this.mobil;
case EMAIL:
return this.email;
default :
return "";
}
}
}
NOW to my question:
is this the right way of modelling such a class regarding mvc pattern?
maybe my first approach was "cleaner" because i had something like a "container" which had all customers in it and the "real" customer class.
The current approach would drive me mad. You have a Customer class with 2 completely distinct behaviors:
When using the default constructor, you are actually storing the contents of the whole database in memory (well, at least of the customer table). So in this case a Customer instance actually represents the whole list of customers
When using the constructor with parameters, a Customer object now represents one Customer
In short, whenever you encounter a Customer instance in your code, you know absolutely nothing.
Further, storing the whole database in memory might be a bit overkill (although probably doable for a limited amount of customers). However, if you will be using this approach for more tables, you will quickly run out-of-memory. Consider only retrieving the data you actually need (for example in a JTable only a certain numbers of customers are visible at the same time, so no need to fetch them all).
And then their is the problem of mixing the business logic with the database logic. I would suggest to clearly separate your Customer class from the actual database access. You never know you will switch databases in the future (or opt for something different then a database). You do not want to rewrite your whole application at that point, just the data-access layer
No, Customer should have no database code in it and should not have an ArrayList<Customer> as these should be handled by other classes. It needs to be pure and needs to simply encapsulate the essence of a Customer, and that's it.
You need to refactor and subdivide some more.

Categories

Resources