d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Easy One About Databases > Jdbc In Netbeans
Add Reply New Topic New Poll
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Nov 29 2012 02:28pm
I am trying to learn how to work with databases in netbeans.
Have created a database, and netbeans have generated an entityclass from that database.

(my database hold and ID(identifier), an image (blob), name (varchar) etc )

Do I need use a entitymanager class or can I read write etc by making an instance of the entityclass directly ?
(Fell free to post good links with examples)

Have also read something about entitymanagerfactory which I didnt really understand.
Is this usefull/nessesary ?

Any other advise on databases is welcome

Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 29 2012 02:45pm
you need to be a lot more specific about the code/libraries you're using. posting your code (all of it) would be a start.
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Nov 29 2012 03:01pm
I know its kinda unspecific. Thats because im new to databases and need some advise about what I should explore.

Here is the code for my entityclass.

Can I make an instance of this class and call lets say the setroll() method. Or do I need to use an entitymanager ?
Looks to me like the first option could create some logical mistakes.

Tell me if there is more I need to post.

Again any general advise or good links for studying DBs are welcome.

Code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package holdem;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
*
* @author sune
*/
@Entity
@Table(name = "BOTS")
@NamedQueries({
   @NamedQuery(name = "Bots.findAll", query = "SELECT b FROM Bots b"),
   @NamedQuery(name = "Bots.findByIdnr", query = "SELECT b FROM Bots b WHERE b.idnr = :idnr"),
   @NamedQuery(name = "Bots.findByNavn", query = "SELECT b FROM Bots b WHERE b.navn = :navn"),
   @NamedQuery(name = "Bots.findByRoll", query = "SELECT b FROM Bots b WHERE b.roll = :roll"),
   @NamedQuery(name = "Bots.findByStrategi", query = "SELECT b FROM Bots b WHERE b.strategi = :strategi")})
public class Bots implements Serializable {
   private static final long serialVersionUID = 1L;
   @Id
   @Basic(optional = false)
   @Column(name = "IDNR")
   private Long idnr;
   @Column(name = "NAVN")
   private String navn;
   @Lob
   @Column(name = "BILLEDE")
   private Serializable billede;
   @Column(name = "ROLL")
   private Double roll;
   @Column(name = "STRATEGI")
   private Integer strategi;

   public Bots() {
   }

   public Bots(Long idnr) {
       this.idnr = idnr;
   }

   public Long getIdnr() {
       return idnr;
   }

   public void setIdnr(Long idnr) {
       this.idnr = idnr;
   }

   public String getNavn() {
       return navn;
   }

   public void setNavn(String navn) {
       this.navn = navn;
   }

   public Serializable getBillede() {
       return billede;
   }

   public void setBillede(Serializable billede) {
       this.billede = billede;
   }

   public Double getRoll() {
       return roll;
   }

   public void setRoll(Double roll) {
       this.roll = roll;
   }

   public Integer getStrategi() {
       return strategi;
   }

   public void setStrategi(Integer strategi) {
       this.strategi = strategi;
   }

   @Override
   public int hashCode() {
       int hash = 0;
       hash += (idnr != null ? idnr.hashCode() : 0);
       return hash;
   }

   @Override
   public boolean equals(Object object) {
       // TODO: Warning - this method won't work in the case the id fields are not set
       if (!(object instanceof Bots)) {
           return false;
       }
       Bots other = (Bots) object;
       if ((this.idnr == null && other.idnr != null) || (this.idnr != null && !this.idnr.equals(other.idnr))) {
           return false;
       }
       return true;
   }

   @Override
   public String toString() {
       return "holdem.Bots[idnr=" + idnr + "]";
   }

}

Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Nov 29 2012 03:06pm
and here is the player class, Its danish :/

Its just the player class in a poker program.

the bot class are for storing computer opponents.

Code
package holdem;

import javax.swing.ImageIcon;

/**
*
* @author sune
*/
public class spiller {
   private long idnr = 0;//et unik nummer til alle spillere
   private String navn = new String();
   private ImageIcon billede;
   private boolean folded = false;
   private boolean allin = false;
   private boolean aktiv = true;//hvorvidt spilleren ønsker kort i den følgende hånd
   private boolean aktivlast = false;//fik spilleren kort sidste hånd?
   private boolean betallowed = true;//er der åbnet for et bet eller raise
   private double bankroll;
   private double buyin;
   private double stack; //hvor meget har spilleren foran sig
   private double stackstart; //hvor meget havde spilleren foran sig da hånden startede
   private int seat;
   private int strategy = 1;
   private int[] hand = new int[2];

   public spiller(String navn,double stack)
   {
this.navn = navn;
       this.stack = stack;
   }

public void sethand (int[] a)  {
   hand = a;
}
public int[] gethand ()  {
   return hand;
}
public void setfolded (boolean a){
   folded = a;
}
public boolean getfolded ()
       {
  return folded;
}
public void setidnr (long a){
   idnr = a;
}
public long getidnr ()
       {
  return idnr;
}
public void setallin (boolean a){
   allin = a;
}
public boolean getallin ()
       {
  return allin;
}
public void setaktiv (boolean a){
   aktiv = a;
}
public boolean getaktiv ()
       {
  return aktiv;
}
public void setaktivlast (boolean a){
   aktivlast = a;
}
public boolean getaktivlast ()
       {
  return aktivlast;
}
public void setbetallowed (boolean a){
   betallowed = a;
}
public boolean getbetallowed ()
       {
  return betallowed;
}
public void setnavn (String a){//gøres i constructor
   navn = a;
}
public String getnavn ()
       {
  return navn;
}
public void setbillede (ImageIcon a){
   billede = a;
}
public ImageIcon getbillede ()
       {
  return billede;
}
public void setbankroll (double a){
   bankroll = a;
}
public double getbankroll ()
       {
  return bankroll;
}
public void setstack (double a){
   stack = a;
}
public double getstack ()
       {
  return stack;
}
public void stackind (double a){
   stack = stack + a;
}
public void stackud (double a){
   stack = stack - a;
}
public void setstackstart (double a){
   stackstart = a;
}
public double getstackstart ()
       {
  return stackstart;
}
public void setbuyin (double a){
   buyin = a;
}
public double getbuyin ()
       {
  return buyin;
}
public void setseat (int a){
   seat = a;
}
public int getseat ()
       {
  return seat;
}
public void setstrategy (int a){
   strategy = a;
}
public int getstrategy ()
       {
  return strategy;
}
public double nedrund(double num) {
double result = num * 100;
result = Math.floor(result);//floor runder ned, round til nærmeste, ceil op
result = result / 100;
return result;
}
}
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 29 2012 03:09pm
uhm, so what's the point of the database here? why aren't you just using a hashtable?
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Nov 29 2012 03:15pm
It should be possible to quit a table and return to the game again some other day. With stacks and everything intact.
Also at a later point. I want to make it possible to make your own player profile with picture and everything.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 29 2012 03:50pm
OK, that's a good answer.

That said, unless your program is fairly complex in terms of the data it needs to store, a database really should be a bit of a last resort for persisting program state. You ought to look into alternative serialization/persistence approaches before investing a whole lot of time in databases (well, unless you really want to learn about using them.. in which case, go bananas!).
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll