Bit messy and could be edited a bit/not the best, but here we go...
Things you will need
EasyPHP - EasyPHP.org (For MySQL, Etc.)
MySQL JDBC Connector - MySQL :: Download Connector/J
Create a new file called "connection.java" and put this in it:
Code:
package mysql;
import java.sql.*;
public class connection {
//How to use the connURL Statement:
//jdbc:mysql// is needed by default
//127.0.0.1/ is the Host and can be edited to your URL, or just localhost
//usermaster is the database
private static final String connURL = "jdbc:mysql://127.0.0.1/hiscores";
//Make our variables and set them all to null
private static Statement stmt;
private static ResultSet rs = null;
private static Connection conn = null;
private static String query = null;
private static boolean mysqlEnabled = true;
public static synchronized void connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
//connURL : Explained Above
//root : Username for MySQL
//Empty space : Password for MySQL
conn = DriverManager.getConnection(connURL, "root", "");
System.out.println("Connected to MySQL Successfully");
} catch(Exception e) {
System.out.println("Error: Unable to connect to MySQL. Details below.");
System.out.println(e);
mysqlEnabled = false;
}
}
public static synchronized void close() {
try {
conn.close();
stmt.close();
System.out.println("Closed MySQL Successfully");
} catch(Exception e) {
System.out.println("No MySQL Connection");
}
}
public static synchronized void execute(String s) {
if(mysqlEnabled == true) {
try {
stmt = conn.createStatement();
query = s;
stmt.executeUpdate(query);
System.out.println("'"+query+"'");
System.out.println("MySQL: Query was a success.");
} catch(Exception e) {
System.out.println("Error: Unable to update MySQL. Error below.");
System.out.println(e);
}
} else {
System.out.println("MySQL is not currently being used.");
}
}
}
Editing stuff should be self explanitory
Now, download and extract the MySQL JDBC Connector and copy/paste the "com" folder into your src directory
You will get errors in the files, but you aren't using any of them so it doesn't matter. I suppose you could always delete all of them except the Driver.java in com/mysql/jdbc
You are now done! But how do we use this code?
In any new class, or the same class, put this:
Code:
connection conn = new connection();
and then inside a void statement, or anything like that, put this:
Code:
conn.connect();
conn.execute("QUERY HERE");
conn.close();
All this does is establishes your connection, updates the MySQL, and then abolishes it after the execute is done.
You are now officially done!