Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package me.senzuri.SMod;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6.  
  7. public class SModMySQL
  8. {  
  9.     protected Connection con = null;
  10.     protected PreparedStatement stmt = null;
  11.     protected boolean connected = false;
  12.    
  13.     //Constructor
  14.     public static SMod plugin;
  15.     // Prepare SQL Statement + Connect to SQL
  16.     public SModMySQL(SMod instance)
  17.     {
  18.         plugin = instance;
  19.  
  20.         try {
  21.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  22.  
  23.             // try to connect
  24.             String url  = "jdbc:mysql://localhost/minecraft";
  25.             String user = plugin.ConfigHandler.username;
  26.             String pass = plugin.ConfigHandler.password;
  27.            
  28.             if(user == null || pass == null) {
  29.                 this.con = DriverManager.getConnection(url);
  30.             } else {
  31.                 this.con = DriverManager.getConnection(url, user, pass);
  32.             }
  33.             this.stmt = this.con.prepareStatement("INSERT INTO `test` (`player`, "
  34.                                                                  + "`ip`) "
  35.                                                                  + "VALUES (?, ?)");
  36.             this.connected = true;
  37.         }
  38.         catch(java.lang.ClassNotFoundException ex) {
  39.             System.out.println("ERROR: can`t load database driver");
  40.             System.out.println(ex.getMessage());
  41.         }
  42.         catch(java.lang.InstantiationException ex) {
  43.             System.out.println("ERROR: can`t load database driver");
  44.             System.out.println(ex.getMessage());
  45.         }
  46.         catch(java.lang.IllegalAccessException ex) {
  47.             System.out.println("ERROR: can`t load database driver");
  48.             System.out.println(ex.getMessage());
  49.         }
  50.         catch(java.sql.SQLException sqlex) {
  51.             System.out.println("ERROR: can`t connect to the database");
  52.             System.out.println(sqlex.getMessage());
  53.         }
  54.     }
  55.  
  56.     public void SendSQL(String player, String ip)
  57.     {
  58.         try {
  59.             this.stmt.setString(1, player);
  60.             this.stmt.setString(2, ip);
  61.             this.stmt.execute();
  62.         }
  63.         catch(java.sql.SQLException ex) {
  64.             System.out.println(ex.getMessage());
  65.         }
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement