I'm fairly certain that I am not doing something class related correctly.
I am using a class to create a set of variables (like a javascript object sort of maybe but not really).
I am using it as shown bellow (a basic example)
public class myScript {
public static void main(String[] args) {
Client warehouse = new Client();
Contacts warehouseContactsA = new Contacts();
Contacts warehouseContactsB = new Contacts();
warehouse.idno = 1;
warehouse.name = "warehouse";
warehouse.desc = "I don't exist";
warehouseContactsA.client_idno = 1;
warehouseContactsA.email = "[email protected]"
warehouseContactsB.client_idno = 1;
warehouseContactsB.email = "[email protected]"
insertIntoDB(warehouse,
warehouseContactsA,
warehouseContactsB);
}
public static void insertIntoDB(Client warehouse,
Contacts warehouseContactsA,
Contacts warehouseContactsB) {
// code to insert into database here
}
private class Client {
int idno;
String name;
String desc;
}
private class Contacts {
int client_idno;
String email;
}
}
Is there any reason to not use classes this way and if so is there a simpler way to store/manage data that doesn't require a class?
Creating inner classes is probably going to create pitfalls for you. When you don't define them as static then they require an implicit reference back to the outer class, which your code doesn't need, it will only get in the way and cause obscure errors. Maybe you're doing this so you can compile only one class and avoid having a build script? Simple build scripts that use gradle are trivial (not like the bad old days when we used ant), so that shouldn't be an issue. It would be better to move your persistent entities out into separate files.
What you're doing with database connections and transactions is not clear. Generally it's bad to try to do each insert in its own transaction, if only because each transaction has overhead and it increases the time the inserts need to run. Typically you'd want to process the inserts in batches.
Mainly, though, if you're writing a script, use a scripting language. Groovy could be a good choice here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With