Jump to content

David

Member
  • Content count

    2,229
  • Joined

  • Last visited

About David

  • Rank
    Game Developer
  • Birthday 12/10/1994

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. If the 'java' command isn't recognized, you either need to put your path in your system environments or specify the whole path you java.exe in your runserver.bat
  2. [quote name='rspshelpplz']Caused by: java.net.BindException: Address already in use: bind[/QUOTE] The address is already used ;)
  3. I think that came around revision 508, which is after PI ;)
  4. Well, if I understand you correctly you would be making a whole new game from scratch, since you will have to make the runescape world itself in 8bit blocks and thus needing to re-write pretty much everything of the code, since it calculations of the pathfinding etcetera would be a lot different. Would it be impossible? No, but you're basically making your own game which requires very, very very much time, patience and skill to do.
  5. The title is incorrect, as it are statements, not only if-statements or conditional statements. Also I'm missing operators, especially the conditional operator [code]private int rights = (name == "David") ? 1 : 0;[/code]
  6. I personally think it [b]is[/b] crucial. If you're not being creative you will simply produce the same as the rest, which does not earns a name.
  7. I would like to see where and how you're handling your main game loop, please :P
  8. [b]Operating system:[/b] Mac OS X Mountain Lion [b]IDE for Java, PHP, C++, HTML, JavaScript:[/b] NetBeans 7.2 [b]IDE for C#:[/b] Microsoft Visual Studio [b]Background music:[/b] Spotify or YouTube [b]Resource gathering:[/b] Either school or Google
  9. [quote name='GreyMan']Good work. Although for design purposes I would export the actual connection for the database out of the constructor and to a method because that tends to lead to design issues when you need to implement a try and catch block to construct an object. Of course then you'd have more control of the state of the connection (connected/not connected) because then you could assume that until the database has been connected it is unconnected and you don't have implement any special handling for if the connection fails as in the scenario I mapped out above.[/QUOTE] Thank you. And of course it would be better to handle the connecting/disconnecting in methods and not in the constructor. But this is just to show how to make a connection at all ;)
  10. Hello everyone, As has been mentioned in the [url=http://www.runelocus.com/forums/showthread.php?84161-Reviving-the-programming-section]Reviving the programming section[/url] thread. The programming section hasn't been as active as it should. Therefor I hereby start making more tutorials for several programming languages. To begin with, a tutorial about the usage of databases in Java. [b]Java and Databases[/b] The storage of application data in a database has been around for a long time. In PHP for example, you make a dynamic website you are required to either use a database for storage or save content in files. The plus point of using databases is that the information stored within is accessible from everywhere with an internet connection and the related login details. Data can be inserted, retrieved, updated and deleted. One of the negative points of using a database that it's hard to make your database secure. In Java, the creation of a database connection is quite easy. What you first need to do is download the MySQL Connector/J, which is a Java library developed by MySQL to handle the creation of a database connection. You can download it [b][url=http://www.mysql.com/downloads/connector/j/]here[/url][/b]. For this tutorial I will be using the NetBeans IDE, which can be downloaded [b][url=http://netbeans.org/downloads/]here[/url][/b]. Ofcourse, the tutorial could be implemented in any other IDE, it's just a matter of opinion which IDE you prefer to use. [b]Creating the project[/b] What we first need to do after downloading the MySQL Connector/J is create a new project in NetBeans. Press the 'New Project...' button, select 'Java' as category and 'Java Class Library' as project. Pick a name for your project and specify the path. [spoiler=Images][img]http://atlasgames.net/tutorials/images/java_and_databases/new_project_1.png[/img] [img]http://atlasgames.net/tutorials/images/java_and_databases/new_project_2.png[/img][/spoiler] A new project is automatically created with a 'Source Packages' and 'Libraries' folder. Right click the 'Libraries' folder and press 'Add JAR/Folder...' Select the 'mysql-connector-java-5.1.22-bin.jar' and press choose. [spoiler=Image][img]http://atlasgames.net/tutorials/images/java_and_databases/library.png[/img][/spoiler] After you've registered the Connector/J, create a new package. In the created package, create a new Java class named 'Database'. [b]Creating the connection[/b] Open the newly created 'Database' class and declare the following variables. [code]private Connection connection; private ResultSet result; private PrepareStatement statement;[/code] And add the following imports: [code]import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger;[/code] The variables will be explained later on. Now the next thing we will do is create a connection to the database in the constructor of the Database class. [code]public Database(String host, String database, String username, String password) { [indent]try { [indent]connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password); [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent]} [/indent]}[/code] I'll explain what it does. When the constructor is called, it will try to find a connection with the specified protocol and database details. If it did not succeed to do that because the connection does not exist for example, it will throw a SQLException which will be caught and printed in the console to make the debugging easier. But of course, a connection should not be open forever, so lets disconnect it at the end of the constructor. [code]public Database(String host, String database, String username, String password) { [indent]try { [indent]connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password); [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent][b]} finally { [indent]try { [indent]if(connection != null) { [indent]connection.close(); [/indent]} [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent]} [/indent]}[/b] [/indent]}[/code] This code (after the actions in the try block have been executed) will check if the connection isn't a null (because how can we close something that's not even there) and if it isn't it will try to close it. Now lets move on to some queries. [b]Executing queries[/b] Now that we have an active connection to the database and the ability to close it, we would like to insert some data in the database, wouldn't we? There are multiple ways to do this, but I will show you only one: the prepared statement. Using prepared statements will increase both security as performance. So here's how we create an insert sql query. [code]public Database(String host, String database, String username, String password) { [indent]try { [indent]connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password); [b]statement = connection.prepareStatement("INSERT INTO Tutorials(Name) VALUES(?)"); statement.setString(1, "Java and Databases"); statement.executeUpdate();[/b] [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent]} finally { [indent]try { [b][indent]if(statement != null) { [indent]statement.close(); [/indent]}[/b] if(connection != null) { [indent]connection.close(); [/indent]} [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent]} [/indent]} [/indent]}[/code] If you create a prepared insert statement, the query looks the same as regular except for the values. [code]statement = connection.prepareStatement("INSERT INTO Tutorials(Name) VALUES(?)");[/code] By setting the values to '?', it creates a placeholder instead of writing the value down itself. Doing so prevents the query from SQL injections and improves the performance. Because we use a placeholder, we will need to fill it in later: [code]statement.setString(1, "Java and Databases");[/code] And then execute an update because we don't expect any data to return from it. [b]Retrieving data[/b] Not only we want to insert data into a database, but we also want to retrieve data from it. Retrieving data from a database is not so different from the inserting. [code]public Database(String host, String database, String username, String password) { [indent]try { [indent]connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password); [b]statement = connection.prepareStatement("SELECT * FROM Tutorials"); result = statement.executeQuery(); while(result.next()) { [indent]System.out.println(result.getString(1)); [/indent]}[/b] [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent]} finally { [indent]try { [indent][b]if(result != null) { [indent]result.close(); [/indent]} if(statement != null) { [indent]statement.close(); [/indent]}[/b] if(connection != null) { [indent]connection.close(); [/indent]} [/indent]} catch(SQLException exception) { [indent]Logger.getLogger(Database.class.getName()).log(Level.SEVERE, exception.getMessage(), exception); [/indent]} [/indent]} [/indent]}[/code] So what we do here is creating a prepared statement [code]statement = connection.prepareStatement("SELECT * FROM Tutorial");[/code] Execute the statement [code]result = [b]statement.executeQuery();[/b][/code] And store the data in the result. While the result contains a next row, it will retrieve the data of the first column as a string and print it. So now we know how to - Opening and closing a database connection - Executing update, delete and insert queries (update and delete work the same as insert queries) - Executing select queries. I hope you've enjoyed it. Yours truly, David
  11. David

    Compare class

    [quote name='Fascism']Thank you, fixed.[/QUOTE] You shouldn't forget to update your first post ;)
  12. David

    Compare class

    [quote name='Hash']Why's that?[/QUOTE] Because you define them as constant which sets their value regardless of any instantiation. Which makes the distance variable for example using the default values which are 0, 0, 0 and 0.
  13. David

    Compare class

    You do realize that distance, midpointX and midpointY will always be 0, right?
  14. A good idea for a future feature might be screen recording. Good luck on this by the way, I'm looking forward to see more progress.
×