Seeing as I forgot about this, I'll go ahead and make the Tutorial. For now, I'm only going to show you how to connect, and the basic Syntax on how to send and receive data from the Database. I have not properly set up loading and saving, so that is for another day.
Step 1: Getting the Database driver and setting it up
No I'm not pulling your leg. The JDBC driver, if you haven't guessed, stands for Java Database Connect driver. It has all of the necessary classes nested in it for immediate SQL-java interaction. The driver can be downloaded Here
Now I'd recommend renaming this to something simple, like JDBC Driver. Next, you want to store it somewhere safe, where you won't accidentally delete it. In this tutorial, I stored it inWe're going to set up this path in our system variables, just like what we did with our JDK. Navigate to your control panel ( Start>Control Panel). Now double click System. Then click the advanced tab, and click Environment Variables.Code:C:\Program Files\Java
Find the PATH variable, on the bottom panel, labeled System Variables
My Path variable looks like so; yours should have the beginning look similarThe bold is the path I added for the JDBC driver. the . after the Jar is important, as it allows access inside of the Jar as well; also, make sure to add a ; at the end to separate for the next comment.Code:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\DMIX;C:\Program Files\QuickTime\QTSystem\;c:\program files\java\jdk1.6.0_06\bin;C:\Program Files\Autodesk\Backburner\;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Program Files\Java\JDBC.Jar.;
Step 2: Connecting to the database from your Run Escape server.
Firstly, make a new class called Database.java. In it, include the following code. Look through it, and try and understand. I've left comments inside to help out
variables are static and private because they, in this instance, are called directly from the server class. Next we need to have our connection initialized in the server class.Code:import java.sql.*; public class Database { private static Connection conn = null; private static Statement state; public static void createConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); //Initialized the JDBC Driver //sets the connection and opens the conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/RuneScape" , "root", ""); // ("jdbc:mysql://SQL IP:SQL port /database" "SQLUser" "SQLPass") state = conn.createStatement(); // sets state to use connection to send SQL statements } catch (Exception e) { // Must catch the errors e.printStackTrace(); // Prints the error, and where the error occurs in the source file } } public static ResultSet sqlQuery(String s){ try{ // In SQL, try's are needed, because there is always the chance of error if((s.toLowerCase()).startsWith("select")){ ResultSet result = state.executeQuery(s); return result; } else{ state.executeUpdate(s); } } catch(SQLException e){ e.printStackTrace(); } return null; //must have the return statement. This usually doesn't occur } public static void closeConnection(){ //try's to close the connections to avoid memory leaks. used try in case it's closed, so no fatals are thrown try { state.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
Open your server.java. Now if you are more advanced, and neat, put this where you see fit. otherwise add this below your main, like so
The above code basically initializes the void createConnection in the Database class(DUH). the other voids I've introduced so you can learn to use them.Code:public static void main(java.lang.String args[]) { Database.createConnection();
Tomorrow, I'll explain the ResultSet method :)



Reply With Quote![[Tutorial] How to connect to your MySQL server through your server](http://ragezone.com/hyper728.png)


