Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQL {
- private String host;
- private String port;
- private String database;
- private String username;
- private String password;
- private Connection connection;
- public void connect() {
- if (!isConnected()) {
- /*
- Alright so here is where you would want to define the values of host, port, database, username, and password
- I am just going to assume that your config.yml has all that information and looks something like this:
- Host: "host"
- Port: "3306"
- Database: "database"
- Username: "username"
- Password: "password"
- here just simply gain access to your main class using Dependency Injections and you will be able to
- get your config if you are not using a config then just simply do the following:
- this.host = "host";
- this.port = "3306";
- this.database = "database";
- this.username = "username";
- this.password = "password";
- */
- try {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- } catch (SQLException e) {
- e.printStackTrace();;
- }
- }
- }
- public void disconnect() {
- if (isConnected()) {
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- return (connection == null ? false : true);
- }
- public Connection getConnection() {
- return connection;
- }
- public void createTable() {
- if (!isConnected()) {
- connect();
- }
- try {
- PreparedStatement ps = connection.prepareStatement("CREATE TABLE IF NOT EXISTS [TABLE NAME HERE] (UUID VARCHAR(50),NAME VARCHAR(20))");
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void insertIP(Player player) {
- if (!isConnected()) {
- connect();
- }
- try {
- PreparedStatement ps = connection.prepareStatement("INSERT INTO [YOUR TABLE NAME] (UUID,NAME) VALUES (?,?)");
- ps.setString(1, player.getUniqueId().toString());
- ps.setString(2, player.getName());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement