Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.mysql.jdbc.Connection;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class Main extends JavaPlugin {
- //DataBase vars.
- final String username="root"; //Enter in your db username
- final String password="password"; //Enter your password for the db
- final String url = "jdbc:mysql://localhost:3306/tutorial"; //Enter URL w/db name
- //Connection vars
- static Connection connection; //This is the variable we will use to connect to database
- @Override
- public void onEnable(){
- try { //We use a try catch to avoid errors, hopefully we don't get any.
- Class.forName("com.mysql.jdbc.Driver"); //this accesses Driver in jdbc.
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("jdbc driver unavailable!");
- return;
- }
- try { //Another try catch to get any SQL errors (for example connections errors)
- connection = (Connection) DriverManager.getConnection(url,username,password);
- //with the method getConnection() from DriverManager, we're trying to set
- //the connection's url, username, password to the variables we made earlier and
- //trying to get a connection at the same time. JDBC allows us to do this.
- } catch (SQLException e) { //catching errors)
- e.printStackTrace(); //prints out SQLException errors to the console (if any)
- }
- }
- @Override
- public void onDisable(){
- // invoke on disable.
- try { //using a try catch to catch connection errors (like wrong sql password...)
- if (connection!=null && !connection.isClosed()){ //checking if connection isn't null to
- //avoid receiving a nullpointer
- connection.close(); //closing the connection field variable.
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement