Thread: MySQL to Server Integration

Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1 MySQL to Server Integration 
    doodoohead
    Guest
    Description: Having some things load from a SQL server. The example in this context is loading autospawn from SQL

    Difficulty: 2-3/depending on brain capacity

    Assumed Knowledge: How to set up a SQL database, copy and paste,

    Tested Server: Cleaned v4

    Files/Classes Modified: SQL.java, server.java

    Procedure
    Step 1: Setting up the SQL Server
    First of all, get a SQL server. I use db4free.net. Next, add this in server.java
    Code:
    	public server() {
    
    	try {
    		String driverName = "org.gjt.mm.mysql.Driver";
    		Class.forName(driverName);
    
    		// Create a connection to the database
    		String serverName = "";//your SQL server
    		String dbase = "";
    		String url = "jdbc:mysql://" + serverName +  "/" + dbase;
    		String username = "";
    		String password = "";
    		connection = DriverManager.getConnection(url, username, password);
    		misc.println("connected to :" + serverName +  "/" + dbase);
    	} catch (ClassNotFoundException e) {
    		misc.println("Class not found error >.<");
    		killServer();
    	} catch (SQLException e) {
    		misc.println("SQL Error AMG");
    		killServer();
    	}
    
    }
    and also add this
    Code:
    	public static Connection connection = null;
    Fill out the information accordingly to how you setup your SQL server at your host.

    Step 2: Making the actual SQL file
    Make a new class and in it, put this
    Code:
    import java.sql.*;
    import java.io.*;
    
    public class SQL {
    	public Connection connection = server.connection;
    	public static Connection myConnection = server.connection;
    
    	public static int AutoSpawn() {
    		int count = 0;
    		try {
    			Statement stmt = myConnection.createStatement();
    			ResultSet rs = stmt.executeQuery("SELECT * FROM autospawn");
    			while (rs.next()) {
    				count = rs.getRow();
    				server.npcHandler.newNPC(rs.getInt("NpcID"), rs.getInt("StartX"), rs.getInt("StartY"), rs.getInt("StartHeight"), rs.getInt("RangeX1"), rs.getInt("RangeY1"), rs.getInt("RangeX2"), rs.getInt("RangeY2"), rs.getInt("WalkType"), server.npcHandler.GetNpcListHP(rs.getInt("NpcID")));
    			}
    			stmt.close();
    		} catch (Exception e){
    			misc.println("Error"+e);
    		}	
    		return count;
    	}
    }
    Add this
    Code:
    		misc.println(""+SQL.AutoSpawn()+" Autospawns loaded");
    under
    Code:
    objectHandler = new ObjectHandler();
    in server.java. Now, open NPCHandler and quote out
    Code:
    loadAutoSpawn("autospawn.cfg");
    Step 3: Executing the SQL script
    Go into your SQL host, and open phpmyadmin. Press the 'Execute SQL Script' button on your far left(should be the middle button, i think) and when the window pops up, execute this script
    Code:
    CREATE TABLE `autospawn` (
      `NpcID` int(10) NOT NULL default '0',
      `StartX` int(10) NOT NULL default '0',
      `StartY` int(10) NOT NULL default '0',
      `StartHeight` int(10) NOT NULL default '0',
      `RangeX1` int(10) NOT NULL default '0',
      `RangeY1` int(10) NOT NULL default '0',
      `RangeX2` int(10) NOT NULL default '0',
      `RangeY2` int(10) NOT NULL default '0',
      `WalkType` int(10) NOT NULL default '0'
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    this will create your table where your data will be stored, in this case, autospawn. Now take a look at this code
    Code:
    INSERT INTO `autospawn` VALUES (1234, xxxx, yyyy, hhhh, xxxx, yyyy, xxxx, yyyy, 1);
    1234 = The NPC I.D.
    The first xxxx and yyyy are the coordinates of the NPC
    The hhhh is the height of your NPC
    The second and third xxxx's and yyyy's are the walking range of it
    And the 1 is the walktype
    Execute this script in the same way you did the other.

    Download the attachment and extract the folders to your source folder, compile, and run.

    If you understood this tutorial and the example of this tutorial, you could probably integrate it to nearly anything. And you're welcome for the example

    Credits: Me
     

  2. #2  
    Registered Member thoompie's Avatar
    Join Date
    Feb 2007
    Age
    30
    Posts
    115
    Thanks given
    0
    Thanks received
    0
    Rep Power
    34
    Nice one, i might make a tut on making sql handled login system, skills, and items.
     

  3. #3  
    Old School Member

    Join Date
    Feb 2008
    Age
    26
    Posts
    2,163
    Thanks given
    372
    Thanks received
    40
    Rep Power
    687
    Quote Originally Posted by thoompie View Post
    Nice one, i might make a tut on making sql handled login system, skills, and items.
    Please do so thanks.

    Quote Originally Posted by Colby View Post
    Rofl, moron. They're ALL going to be null idiot. This is such complete bullshit, it makes me want to strangle you.
     

  4. #4  
    Registered Member
    netzo's Avatar
    Join Date
    Feb 2008
    Age
    28
    Posts
    1,226
    Thanks given
    402
    Thanks received
    135
    Rep Power
    185
    Very nice! Thank you!
     

  5. #5  
    Donator


    Join Date
    Sep 2007
    Age
    24
    Posts
    2,430
    Thanks given
    127
    Thanks received
    508
    Rep Power
    386
    V Nice
    Bumping an old useful post lol
    ok im making forum integration now THANKS!
     

  6. #6  
    Registered Member

    Join Date
    Jul 2008
    Posts
    939
    Thanks given
    0
    Thanks received
    3
    Rep Power
    781


    Problem
    I'm Rog3r, fool.
     

  7. #7  
    Donator


    Join Date
    Sep 2007
    Age
    24
    Posts
    2,430
    Thanks given
    127
    Thanks received
    508
    Rep Power
    386
    i did this tut and now ive lost the source i did it on
    darnit i nearly actualy had it for my site aswell
     

  8. #8  
    x19
    Guest
    Credits to sqlscape...
     

  9. #9  
    Registered Member
    Harvey's Avatar
    Join Date
    Mar 2007
    Posts
    965
    Thanks given
    0
    Thanks received
    55
    Rep Power
    296
    credits to whitescape v2?
    Thanks,

    Harvey
     

  10. #10  
    GS Speced
    Guest
    Dude I fu cking love you this was the last piece i needed to add highscores to my server.
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

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