d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > A Class To Access My Database In Java > Reward 200 Fg
123Next
Add Reply New Topic New Poll
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Mar 17 2013 12:32pm
This is probably pretty basic, but the first time I have to do it. Hope someone can help me.
The first answer which works get 200 fg. And I will write in thread who gets it.
I am making a game in netbeans. I have created a database with the following stats:

name: Contact DB
URL = jdbc:derby://localhost:1527/contact
Driver = org.apache.derby.jdbc.ClientDriver
Schema= APP
User = nbuser

Under tables I have created a table called "player"
It contains the following:
IDnumber (primary key), type integer, size 10
name, type varchar, size 20
roll, type double, size 52
picture, type BLOB, size 2147483647
strategy, type integer, size 10

Need a class that can store, update and get "player" objects. The player objects contain all the fields above IDnumber, name etc.
So its a database for players (human and AIs). It needs to be accessed and modified continuously.

If you can do this please post the entire sourcecode and tell me if there is more I need to do to set up the database connection.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 17 2013 09:56pm
not really interested in doing it for you. but if you need tips, look into jdbc and write out your sql first.

alternatively you can use some sort of orm (hiberate, jpa, etc) but i'd stay away from them as a beginner.
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Mar 18 2013 10:15am
You'd have to make a connection to the database and then request all of those.

You can then parse what you got and create an object with the information fetched.
Member
Posts: 6,481
Joined: Jul 5 2009
Gold: 0.70
Mar 18 2013 12:35pm
PM me I'll help you out
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 18 2013 02:56pm
just use jpa

Player.java
Code
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "player")
public class Player {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long IDnumber;
 
  @Column(length = 20)
 private String name = null;

  @Column(length = 52)
 private double roll = null;
 
@Lob
@Column(length=2147483647)
private byte[] picture;

@Column(length = 10)
private int strategy;

public Long getIDnumber( ) {
 return IDnumber;
}

public void setIDnumber( Long iDnumber ) {
 IDnumber = iDnumber;
}

public String getName( ) {
 return name;
}

public void setName( String name ) {
 this.name = name;
}

public double getRoll( ) {
 return roll;
}

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

public byte[] getPicture( ) {
 return picture;
}

public void setPicture( byte[] picture ) {
 this.picture = picture;
}

public int getStrategy( ) {
 return strategy;
}

public void setStrategy( int strategy ) {
 this.strategy = strategy;
}
 

}




/src/META-INF/persistence.xml
Code
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
 version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
 <persistence-unit name="Contact DB" transaction-type="RESOURCE_LOCAL">
   <properties>
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
     <property name="javax.persistence.jdbc.url"
       value=jdbc:derby://localhost:1527/contact;create=true" />
     <property name="javax.persistence.jdbc.user" value="nbuser" />
     <property name="javax.persistence.jdbc.password" value="yourPassword" />
     <property name="eclipselink.ddl-generation" value="create-tables" />
     <property name="eclipselink.ddl-generation.output-mode"
       value="database" />
   </properties>

 </persistence-unit>
</persistence>


This post was edited by labatymo on Mar 18 2013 03:01pm
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Mar 20 2013 12:32pm
Thank you labatymo. But I already have an entity class. Netbeans generated it for me.

It looks like what u posted but with this at the end

Code
@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 play)) {
           return false;
       }
     play other = (play) 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.play[idnr=" + idnr + "]";
   }


But i don't understand how this helps me!

For example. What would the code look like if a player had lost some points and the roll forf that player needed to be updated in the database. ?

Im sorry that I don't understand this. But if I did I would not have posted :)

When I look in /src/META-INF/persistence.xml It also look almost like u posted. This was generted automaticly when I made the database connection.

Hope U can explain how this works so I can make code in my program to communicate with my database.

What are the commands like. Think I must use commands like these http://www.roseindia.net/jsp/execute-sql-query.shtml

Sorry that im too thick headed to understand this.

This post was edited by tigeranden on Mar 20 2013 12:40pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 20 2013 01:59pm
Something like this should work. I haven't tested it though

Code
EntityManagerFactory  factory = Persistence.createEntityManagerFactory("Contact DB");
   EntityManager em = factory.createEntityManager();

//begin a transaction
   em.getTransaction().begin();

//you can find that player like this....
Player player = em.find( entityClass, IDnumber);

//...or you can find the player like this
Query q = em.createQuery( "select e from "
       + Player.class.getSimpleName( ) + " e where e."
       + name+ "= :name" );

//set your query parameter for name
   q.setParameter( "name", "John Doe");
//Get the only player in the list
   q.setMaxResults( 1 );
Player player = (Player) q.getResultList( ).get( 0 );

//or get the whole list of players if you want
List<Player> players = q.getResultList( );


//then set whatever you want. It will update in the database automatically

player.setPoints(10);

   em.getTransaction().commit();
   em.close();


This post was edited by labatymo on Mar 20 2013 02:22pm
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Mar 23 2013 01:40pm
Thank U will be testing it tomorrow.

May have some questions. I will send the fg if t works.

Again thanks alot :)

Will write again tomorrow.

Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Mar 24 2013 12:33pm
It does compile.

But something is wrong in the first line :

EntityManagerFactory factory = Persistence.createEntityManagerFactory("Contact DB");

it says :

Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: No Persistence provider for EntityManager named Contact DB

The database is connected. the database is called "Contact DB", the schema is called "NBUSER"

Have been reading about it, but cant figure out what is wrong :(

This post was edited by tigeranden on Mar 24 2013 12:35pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 24 2013 12:35pm
check your xml file. are you using the same name he posted?

<persistence-unit name="Contact DB" transaction-type="RESOURCE_LOCAL">
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll