Results 1 to 5 of 5
  1. #1
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default can someone explain what this exactly does?

    so i have writen a small program to create a new player (and also ask them from the database)

    Java Code:
    	public Speler registreerSpeler(String naam, SchipKlasse klasse,String schipNaam) 
    	{
    		{
    			Speler speler = null;
    			Statement statement;
    			try
    			{
    				Connection connection = PersistentieController.getInstance().getConnection();
    				statement = connection.createStatement();
    				String naamCaptain = naam;
    				String ship = schipNaam;
    				
    				String className = klasse.getNaamKlasse();
    				int hitpoints = klasse.getHitpoints();
    				int snelheid = klasse.getSpeedpoints();
    				int cannons = klasse.getCannons();
    				int rockets = klasse.getRockets();
    				int AA = klasse.getAA();
    				int soldiers = klasse.getSoldiers();
    				int aircrafts = klasse.getAircrafts();
    				int copters = klasse.getCopters();
    				int mines = klasse.getMines();
    				int schadepunten = 0;
    				statement.executeUpdate("INSERT INTO SpelerShip (nameship,classname,hitpoints,speedpoints,cannons,rockets,AA,soldiers,aircrafts,copters,mines,schadepunten) VALUES ('" + ship + "', '" + className + "', '" + hitpoints + "', '" + snelheid + "', '" + cannons + "', '" + rockets + "', '" + AA + "', '" + soldiers + "', '" + aircrafts + "', '" + copters + "','" + mines + "','" + schadepunten + "')");
    				statement.close();
    				statement = connection.createStatement();
    				statement.executeUpdate("INSERT INTO Spelers (naamcaptain,ship) VALUES ('" + naamCaptain + "','" + ship + "')");
    				
    				//statement.executeUpdate("INSERT INTO Spelers (Naam,Categorie) VALUES ('" + naam + "','" + categorieNaam + "')");
    				statement.close();
    				
    			    String cmd = "select @@identity from ShipClass";
    			    statement = connection.createStatement();
    			    ResultSet rs = statement.executeQuery(cmd);
    			    if (!rs.next())
    			    	return null;
    			    
    			    speler = new Speler(naam, new Schip(klasse, schipNaam, schadepunten));
    			    statement.close();
    			} 
    			catch ( SQLException sqlException )
    			{
    			    sqlException.printStackTrace();
    			    PersistentieController.getInstance().closeConnection();
    			} 
    			  
    			return speler;
    		}

    this is the code

    now i'm only wondering what this does...

    Java Code:
        String cmd = "select @@identity from ShipClass";
    			    statement = connection.createStatement();
    			    ResultSet rs = statement.executeQuery(cmd);
    			    if (!rs.next())
    			    	return null;
    this is a picture of the relationship model of my database


    i know it has to be there in order to work, but i just can't figure out what exactly happens...


    tho i will learn this next year i would love to know what it does now.

    please help me. :p ^^

    grtz liluma

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I don't know what that code is for.

    I just wanted to suggest that you use a PreparedStatement for your SQL to make the code more readable and less error prone:

    Java Code:
    String sql = "INSERT INTO Page (Name, Title) VALUES (?, ?)";
    
    PreparedStatement stmt = connection.prepareStatement(sql);
    
    stmt.setString( 1, nameVariable );
    stmt.setString( 2, titleVariable );
    stmt.executeUpdate();

  3. #3
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default

    the program just writes information(strings, int) to a microsoft database...
    that function makes a new record in two tables...

    i know that preparedStatement are more clear and easier and less likely to make errors...
    but i need to be able to do both...
    so that's the only reason i used nrml statement here and manually put in the variables... :p

    but i know it works correctly i'm just wondering what that last bit of code does...

    Java Code:
        String cmd = "select @@identity from ShipClass";
    			    statement = connection.createStatement();
    			    ResultSet rs = statement.executeQuery(cmd);
    			    if (!rs.next())
    			    	return null;

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Click here.

    db

  5. #5
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default

    hmm that gave me the awnser indeed...

    searched for awnser in the wrong place. :o :p

    anyway ty for pointing me out in the right direction DarrylBurke. ^^

Similar Threads

  1. Anyone can explain this?
    By kazumahits in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 02:03 AM
  2. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  3. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  4. Can somebody explain me this plz
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 07:47 PM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •