d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java: Interacting With A Database
123Next
Add Reply New Topic New Poll
Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 10 2014 03:10pm
Disclaimer: I am not a programmer.
Aka. Assume maximum ignorance.

I decided to take a SQL course, and the instructor has asked us to create a simple, console-based Java front end to work a database we're responsible for creating.

The problem here is that I don't really know Java or have much experience with OOP.

I know how to write the SQL for the queries I need, and I'm familiar with the basics of JDBC syntax,

However, I have no idea how to organize the project in term of OOP.

It's a lot to ask, but I was hoping someone could help me get started on this.
Perhaps lay down a framework and explain why its the best practice.

Edit: I guess I also have no idea what sorts of things can go wrong with all the resource management, haha.

Can't offer much aside from my thanks and the 678 fg to my name.

Let me know! :)

This post was edited by MidnightRider on Nov 10 2014 03:33pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 10 2014 03:52pm
I did something similar to this recently for a class.

I can post you some source code from my project later tonight.

It builds a simple Desktop App GUI with a file explorer, then executes some queries from a zip folder.

It isn't professional or anything, though. Just a school project, so it is a bit messy and inefficient (don't judge me!)

Regarding OOP, you don't have to make it any more complicated than it has to be. Early on in the CS degree, I often built the projects in my Java class (technically the OOP class) as single class, everything-in-main maximum noob style.

You don't have to try and impress your prof. with 100 custom interfaces and abstract classes.

If you do, though, I suggest asking Minko to do it. Lulz.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 10 2014 04:26pm
Quote (Eep @ Nov 10 2014 04:52pm)


You don't have to try and impress your prof. with 100 custom interfaces and abstract classes.

If you do, though, I suggest asking Minko to do it. Lulz.


I read that and I started grinning. And theN I finished reading and saw you called me out lol

Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 10 2014 04:37pm
Quote (Eep @ Nov 10 2014 05:52pm)
I did something similar to this recently for a class.

I can post you some source code from my project later tonight.

It builds a simple Desktop App GUI with a file explorer, then executes some queries from a zip folder.

It isn't professional or anything, though. Just a school project, so it is a bit messy and inefficient (don't judge me!)

Regarding OOP, you don't have to make it any more complicated than it has to be. Early on in the CS degree, I often built the projects in my Java class (technically the OOP class) as single class, everything-in-main maximum noob style.

You don't have to try and impress your prof. with 100 custom interfaces and abstract classes.

If you do, though, I suggest asking Minko to do it. Lulz.


I would actually really appreciate that.

Haha, trust me - I don't want to make it more complicated than it needs to be!!

The instructor did say we should separate business logic from user interaction though, and I'm not really sure how to going about do so.

P.s. I'm counting on you to organize the Anime Secret Santa!

Quote (Minkomonster @ Nov 10 2014 06:26pm)
I read that and I started grinning. And theN I finished reading and saw you called me out lol


I saw what you did to that poor user about a week ago :o
Tbh, I was down right scared you'd post in here and tear me a new one, lol :P

This post was edited by MidnightRider on Nov 10 2014 04:38pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 10 2014 04:58pm
Quote (MidnightRider @ Nov 10 2014 05:37pm)
I would actually really appreciate that. 

Haha, trust me - I don't want to make it more complicated than it needs to be!! 

The instructor did say we should separate business logic from user interaction though, and I'm not really sure how to going about do so.

P.s.  I'm counting on you to organize the Anime Secret Santa!



I saw what you did to that poor user about a week ago  :o
Tbh, I was down right scared you'd post in here and tear me a new one, lol  :P


You've got me all wrong. I don't shit on newbies. It's quite the opposite. And I am sure everyone else here will agree with this as well. I am one of the most interactive and engaging member of this sub in terms of what I will do for someone showing a vested interest in learning.

Just as long as you don't flat out ask for code. If you flat out as for code, or beg/bribe others to do your assignment for you...well then I will give you code. It just won't be as you wanted it. However, it will do exactly what you asked. My dream is that someday someone is desperate enough to take my code and submit it as their own, only to have to justify the amount of WTF I put in it to their professor.

Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 10 2014 05:31pm
Quote (Minkomonster @ Nov 10 2014 06:58pm)
You've got me all wrong. I don't shit on newbies. It's quite the opposite. And I am sure everyone else here will agree with this as well. I am one of the most interactive and engaging member of this sub in terms of what I will do for someone showing a vested interest in learning.

Just as long as you don't flat out ask for code. If you flat out as for code, or beg/bribe others to do your assignment for you...well then I will give you code. It just won't be as you wanted it. However, it will do exactly what you asked. My dream is that someday someone is desperate enough to take my code and submit it as their own, only to have to justify the amount of WTF I put in it to their professor.


Sorry if I presented you in an inaccurate light.
My comment was primarily meant to be a light joke.

From what little time I've spent in this sub-forum (I replied in the aforementioned thread, haha), I've frequently noticed you giving relevant answers/help.

That said, I will admit I was worried people might get on my case for not consulting the web for help.
I recognize independent learning is high valued among computer scientists (which I am not), but I learn much better from guided assistance because I am prone to missing the big picture.

This post was edited by MidnightRider on Nov 10 2014 05:32pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 10 2014 05:32pm
So Let me go ahead and start you off. Now, as you can imagine, this can be abstracted way more. But this is just a basic way to establish a boundary between the data access layer (Our CustomerDAOImpl) and the application layer (our client, or the Store). In this example, we will have a datasource which persists Customer objects (presumably within some Customer table). We will expose an interface to the application layer which represents the data access layer, and hide the implementation. This means the client will only ever know about the interface. The actual implementation of this interface will be where you put your JDBC stuff to communicate with the datasource to get your Customer objects. What follows is whats known as the Data Access Object pattern


Code
public class Customer
{
private String id;

public Customer(String id)
{
setId(id);
}

public void setId(String id)
{
this.id = id;
}

public String getId()
{
return id;
}
}


Code
import java.util.List;

//represents a Customer table within a datasource
public interface CustomerDAO
{
List<Customer> getCustomers();
Customer getCustomerById(String id);

boolean updateCustomer(Customer c);
boolean insertCustomer(Customer c);
boolean deleteCustomer(Customer c);
}


Code
import java.util.List;

//communicates with datasource to obtain/modify/delete Customer objects
//stub because I am lazy
public class CustomerDAOImpl implements CustomerDAO
{
public CustomerDAOImpl() {}

public List<Customer> getCustomers() {}

public Customer getCustomerById(String id) {}

public boolean updateCustomer(Customer c) {}

public boolean insertCustomer(Customer c) {}

public boolean deleteCustomer(Customer c) {}

}


Code
import java.util.List;

//represents the client
public class Store {

private CustomerDAO customerDAO;

public Store(CustomerDAO customerDAO)
{
this.customerDAO = customerDAO;
}

public Customer getCustomerByID(String id)
{
return customerDAO.getCustomerById(id);
}
}


This post was edited by Minkomonster on Nov 10 2014 05:38pm
Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 10 2014 06:07pm
And... I'm already confused :unsure:

On a very high level, I understand the value of interfaces, but looking at what you have, I'm already lost as to how I want to incorporate something like this in my code.

I'm working with a simple Student enrollment database (Students, Enroll, Section, Course).
Students need to be able to view their schedules, enroll in classes, drop a class - stuff of this nature.
I'm completely ignoring validation atm; students just login with an id.

At present, I have a main method, and class that looks like this:

I'm sure one look at this will demonstrate just how little I understand interfaces and encapsulation, haha.

Code
package courseCatalog;

public class DbInteraction {
private int studentID;
private Connection conn;
Statement stmt = null;
PreparedStatement pStmt = null;
String query = ("");


public DbInteraction(int studentID, Connection conn)
{
this.studentID = studentID;
this.conn = conn;
}

public boolean validID() throws SQLException
{
this.setQuery("Select s.ID FROM STUDENT s WHERE s.ID = ?");
this.pStmt = conn.prepareStatement(this.query);
this.pStmt.setInt(1, this.studentID);

ResultSet rs = this.pStmt.executeQuery();
return rs.isBeforeFirst();
}



public Statement getStmt() {
return stmt;
}


public void setStmt(Statement stmt) {
this.stmt = stmt;
}


public String getQuery() {
return query;
}


public void setQuery(String query) {
this.query = query;
}


public int getStudentID() {
return studentID;
}


public void setStudentID(int studentID) {
this.studentID = studentID;
}


}


This post was edited by MidnightRider on Nov 10 2014 06:07pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 10 2014 06:34pm
The interface serves as a contract between the application layer and the data access layer. By using this contract, the data access layer is bound to provide implementations for the members it has defined in the interface. This means the application layer can utilize this contract in place of an actual instance of the data access layer. And because of this, the data access layer is free to change its underlying implementation without directly affecting the application layer. The only time the application layer must make changes is if the contract (interface) changes.

This also allows for modularity. Now you can swap implementations of that interface in and out to provide new/different functionality. Above I mentioned CustomerDAOImpl would access a database using jdbc. Well, what if you didn't want to access the database. What if you were writing unit tests, and needed a mocked version of the database? Well how about this

Code
public MockCustomerDAO implements CustomerDAO
{
private List<Customer> customers = new List<Customer> { new Customer("Minko"), new Customer("Midnight") };

public List<Customer> getCustomers() { return customers; }

private Customer get(String id)
{
for(Customer c : customers)
if(c.getId().equals(id)) return c;
return null;
}
public Customer getCustomerById(String id)
{
return get(id);
}

//this method makes no sense because I only have 1 property on the Customer class
public boolean updateCustomer(Customer c)
{
Customer cust = get(c.getId());

if(cust == null) return false;

//set all of cust properties
cust.setId(c.getId());

return true;

}

public boolean insertCustomer(Customer c)
{
customers.add(c);
return true;
}

public boolean deleteCustomer(Customer c)
{
c = get(c.getId());
return customers.remove(c);
}

}


Now you have a mocked version of your database which operates off of a static list. You can now just inject this in place of your normal implementation for unit testing.
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Nov 10 2014 07:16pm
Quote (MidnightRider @ Nov 10 2014 06:07pm)
And... I'm already confused    :unsure: 

On a very high level, I understand the value of interfaces, but looking at what you have, I'm already lost as to how I want to incorporate something like this in my code.

I'm working with a simple Student enrollment database (Students, Enroll, Section, Course).
Students need to be able to view their schedules, enroll in classes, drop a class - stuff of this nature. 
I'm completely ignoring validation atm; students just login with an id. 

At present, I have a main method, and class that looks like this: 

I'm sure one look at this will demonstrate just how little I understand interfaces and encapsulation, haha.

Code
package courseCatalog;

public class DbInteraction {
private int studentID;
private Connection conn;
Statement stmt = null;
PreparedStatement pStmt = null;
String query = ("");


public DbInteraction(int studentID, Connection conn)
{
  this.studentID = studentID; 
  this.conn = conn;
}

public boolean validID() throws SQLException
{
  this.setQuery("Select s.ID FROM STUDENT s WHERE s.ID = ?");
  this.pStmt = conn.prepareStatement(this.query); 
  this.pStmt.setInt(1, this.studentID); 
 
  ResultSet rs = this.pStmt.executeQuery();
  return rs.isBeforeFirst();
}



public Statement getStmt() {
  return stmt;
}


public void setStmt(Statement stmt) {
  this.stmt = stmt;
}


public String getQuery() {
  return query;
}


public void setQuery(String query) {
  this.query = query;
}


public int getStudentID() {
  return studentID;
}


public void setStudentID(int studentID) {
  this.studentID = studentID;
}


}


it looks like you're off to a good start. do you have specific questions?
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll