java trouble making a correct interface -


i want make class can interact database, has following desired functionality:

  • it has method return fields database, later can changed such can limit returns.
  • it has method insert specific instance of class.
  • it has method update specific instance of class. show code in moment after further explanation.

now want extract interface, or rather abstract class think might more appriopiate, sure classes/datafields follow same 'interface', , able use them supertype in lists etc.

the data class, in case account.java, should represent table in database stores {username, password}, omitting explicite unique identifier now, still not sure if make additional id field or use uniqueness of username field.

it best if abstract class handle mysql interaction 'mess'.

account.java far:

package testthing;  import java.util.map;  /**  *  * @author frank  */ public class account {     private final static string all_query = "select * accounts";     private final static string insert_query = "insert accounts (username, password) values(?, ?)";     private final static string update_query = "update accounts set password=? username=?";      private string username;     private string password;      public account(final string username, final string password) {         this.username = username;         this.password=  password;     }      public string getusername() {         return username;     }      public void setusername(final string username) {         this.username = username;     }      public string getpassword() {         return password;     }      public void setpassword(final string password) {         this.password = password;     }      public static map<string, account> getall() {         //return map using all_query string     }      public void insert() {         //insert using insert_query     }      public void update() {         //update using update_query     } } 

i know haven't been clear i'm afraid, hope enough me going.

basically want always able use followings methods when working tableobject, account subset of:

  • account.getall();
  • new account("test", "test").insert();
  • currentaccount.setpassword("newpassword"); currentaccount.update();

all nasty sql stuff should hidden inside proposed abstract class. thing cannot escape in tableobject class definition of sql queries.

regards.

edit: in current example account.getall() returns map<string, account>, in reality first generic argument should type of key in database. if use unique id need return map<integer, account>. hope change makes in time people read it.

is not more logical, have connection code , "nasty" stuff in superclass, have more general method in superclass, used it's sub classes. example:

public void executeupdate(string query) {     // code execute update. }  public map<string, data> getdata(string query) {     // code data.     return data; } 

this way, these methods more general. means can implement several classes pass query data, rather having update superclass every single time want add new functionality.

obviously i've assumed type data here, might into. aim here decouple classes as possible. means can add many new classes want, , can use supertype without hinderence.

this means things like

account.getall(); 

is little less complicated, because if have getall in superclass, reference want accounts? if code in account class, can customize query, , send off getdata method executed.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -