mirror of
				https://github.com/mcMMO-Dev/mcMMO.git
				synced 2025-10-30 16:53:43 +01:00 
			
		
		
		
	Merge pull request #88 from krinsdeath/master
Fixed Database handler being ridiculously inefficient
This commit is contained in:
		| @@ -8,147 +8,95 @@ import java.util.HashMap; | |||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.sql.PreparedStatement; | import java.sql.PreparedStatement; | ||||||
|  |  | ||||||
|  |  | ||||||
| import org.bukkit.entity.Player; |  | ||||||
|  |  | ||||||
| import com.avaje.ebeaninternal.server.lib.sql.DataSourceException; |  | ||||||
| import com.gmail.nossr50.config.LoadProperties; | import com.gmail.nossr50.config.LoadProperties; | ||||||
|  |  | ||||||
|  |  | ||||||
| public class Database { | public class Database { | ||||||
|  |  | ||||||
| 	private Connection conn; |  | ||||||
| 	private mcMMO plugin; | 	private mcMMO plugin; | ||||||
|  | 	private String connectionString; | ||||||
|  |  | ||||||
| 	public Database(mcMMO instance)  | 	public Database(mcMMO instance) { | ||||||
| 	{ | 		this.plugin = instance; | ||||||
| 		plugin = instance; | 		this.connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass; | ||||||
| 		// Load the driver instance | 		// Load the driver instance | ||||||
| 		try { | 		try { | ||||||
|             Class.forName("com.mysql.jdbc.Driver").newInstance(); | 			Class.forName("com.mysql.jdbc.Driver"); | ||||||
|         } catch (Exception ex) { | 			DriverManager.getConnection(connectionString); | ||||||
|         	throw new DataSourceException("Failed to initialize JDBC driver"); | 		} catch (ClassNotFoundException e) { | ||||||
|         } | 			plugin.getServer().getLogger().warning(e.getLocalizedMessage()); | ||||||
| 		 | 		} catch (SQLException e) { | ||||||
| 		// make the connection | 			plugin.getServer().getLogger().warning(e.getLocalizedMessage()); | ||||||
| 		try { | 			System.out.println("SQLException: " + e.getMessage()); | ||||||
| 		    conn = DriverManager.getConnection("jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass);			 | 			System.out.println("SQLState: " + e.getSQLState()); | ||||||
| 		} catch (SQLException ex) { | 			System.out.println("VendorError: " + e.getErrorCode()); | ||||||
| 		    // handle any errors |  | ||||||
| 		    System.out.println("SQLException: " + ex.getMessage()); |  | ||||||
| 		    System.out.println("SQLState: " + ex.getSQLState()); |  | ||||||
| 		    System.out.println("VendorError: " + ex.getErrorCode()); |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	//Create the DB structure | 	//Create the DB structure | ||||||
|  |  | ||||||
| 	public void createStructure() { | 	public void createStructure() { | ||||||
| 		Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"huds` (`user_id` int(10) unsigned NOT NULL," + | 		Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "huds` (`user_id` int(10) unsigned NOT NULL," | ||||||
| 				"`hudtype` varchar(50) NOT NULL DEFAULT ''," + | 				+ "`hudtype` varchar(50) NOT NULL DEFAULT ''," | ||||||
| 				"PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | 				+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | ||||||
| 		Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT," + | 		Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT," | ||||||
| 				"`user` varchar(40) NOT NULL," + | 				+ "`user` varchar(40) NOT NULL," | ||||||
| 				"`lastlogin` int(32) unsigned NOT NULL," + | 				+ "`lastlogin` int(32) unsigned NOT NULL," | ||||||
| 				"`party` varchar(100) NOT NULL DEFAULT ''," + | 				+ "`party` varchar(100) NOT NULL DEFAULT ''," | ||||||
| 				"PRIMARY KEY (`id`)," + | 				+ "PRIMARY KEY (`id`)," | ||||||
| 				"UNIQUE KEY `user` (`user`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"); | 				+ "UNIQUE KEY `user` (`user`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"); | ||||||
| 		Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"cooldowns` (`user_id` int(10) unsigned NOT NULL," + | 		Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "cooldowns` (`user_id` int(10) unsigned NOT NULL," | ||||||
| 				"`taming` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`taming` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`mining` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`mining` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`woodcutting` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`repair` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`repair` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`unarmed` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`unarmed` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`herbalism` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`herbalism` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`excavation` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`excavation` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`archery` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`archery` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`swords` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`swords` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`axes` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`axes` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`acrobatics` int(32) unsigned NOT NULL DEFAULT '0'," + | 				+ "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | 				+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | ||||||
| 		Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"skills` (`user_id` int(10) unsigned NOT NULL," + | 		Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "skills` (`user_id` int(10) unsigned NOT NULL," | ||||||
| 				"`taming` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`taming` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`mining` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`mining` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`repair` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`repair` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`excavation` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`archery` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`archery` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`swords` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`swords` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`axes` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`axes` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | 				+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | ||||||
| 		Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"experience` (`user_id` int(10) unsigned NOT NULL," + | 		Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "experience` (`user_id` int(10) unsigned NOT NULL," | ||||||
| 				"`taming` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`taming` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`mining` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`mining` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`repair` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`repair` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`excavation` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`archery` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`archery` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`swords` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`swords` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`axes` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`axes` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," + | 				+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," | ||||||
| 				"PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | 				+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | ||||||
| 		Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"spawn` (`user_id` int(10) NOT NULL," + | 		Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "spawn` (`user_id` int(10) NOT NULL," | ||||||
| 				"`x` int(64) NOT NULL DEFAULT '0'," + | 				+ "`x` int(64) NOT NULL DEFAULT '0'," | ||||||
| 				"`y` int(64) NOT NULL DEFAULT '0'," + | 				+ "`y` int(64) NOT NULL DEFAULT '0'," | ||||||
| 				"`z` int(64) NOT NULL DEFAULT '0'," + | 				+ "`z` int(64) NOT NULL DEFAULT '0'," | ||||||
| 				"`world` varchar(50) NOT NULL DEFAULT ''," + | 				+ "`world` varchar(50) NOT NULL DEFAULT ''," | ||||||
| 				"PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | 				+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;"); | ||||||
| 	} |  | ||||||
| 	// check if its closed |  | ||||||
| 	private void reconnect() |  | ||||||
| 	{ |  | ||||||
| 		System.out.println("[mcMMO] Reconnecting to MySQL..."); |  | ||||||
| 		try  |  | ||||||
| 		{ |  | ||||||
| 		    conn = DriverManager.getConnection("jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass);			 |  | ||||||
| 		     |  | ||||||
| 		    System.out.println("[mcMMO] Connection success!"); |  | ||||||
| 		} catch (SQLException ex)  |  | ||||||
| 		{ |  | ||||||
| 			System.out.println("[mcMMO] Connection to MySQL failed! Check status of MySQL server!"); |  | ||||||
| 		    System.out.println("SQLException: " + ex.getMessage()); |  | ||||||
| 		    System.out.println("SQLState: " + ex.getSQLState()); |  | ||||||
| 		    System.out.println("VendorError: " + ex.getErrorCode()); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 		try { |  | ||||||
| 			if(conn.isValid(5)){ |  | ||||||
| 				Users.clearUsers(); |  | ||||||
| 				 |  | ||||||
| 				for(Player x : plugin.getServer().getOnlinePlayers()) |  | ||||||
| 				{ |  | ||||||
| 					Users.addUser(x); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		} catch (SQLException e) { |  | ||||||
| 			//Herp |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	// write query | 	// write query | ||||||
| 	public boolean Write(String sql)  | 	public boolean Write(String sql) { | ||||||
| 	{ | 		try { | ||||||
| 		/* | 			Connection conn = DriverManager.getConnection(connectionString); | ||||||
| 		 * Double check connection to MySQL | 			PreparedStatement stmt = conn.prepareStatement(sql); | ||||||
| 		 */ |  | ||||||
| 		try  |  | ||||||
| 		{ |  | ||||||
| 			if(!conn.isValid(5)) |  | ||||||
| 			{ |  | ||||||
| 			reconnect(); |  | ||||||
| 			} |  | ||||||
| 		} catch (SQLException e)  |  | ||||||
| 		{ |  | ||||||
| 			e.printStackTrace(); |  | ||||||
| 		} |  | ||||||
| 			 |  | ||||||
| 		try  |  | ||||||
| 			{ |  | ||||||
| 		  		PreparedStatement stmt = null; |  | ||||||
| 		  		stmt = this.conn.prepareStatement(sql); |  | ||||||
| 			stmt.executeUpdate(); | 			stmt.executeUpdate(); | ||||||
|  | 			conn.close(); | ||||||
| 			return true; | 			return true; | ||||||
| 		} catch (SQLException ex) { | 		} catch (SQLException ex) { | ||||||
| 			System.out.println("SQLException: " + ex.getMessage()); | 			System.out.println("SQLException: " + ex.getMessage()); | ||||||
| @@ -161,36 +109,23 @@ public class Database { | |||||||
| 	// Get Int | 	// Get Int | ||||||
| 	// only return first row / first field | 	// only return first row / first field | ||||||
| 	public Integer GetInt(String sql) { | 	public Integer GetInt(String sql) { | ||||||
|   		PreparedStatement stmt = null; |  | ||||||
| 		ResultSet rs = null; | 		ResultSet rs = null; | ||||||
| 		Integer result = 0; | 		Integer result = 0; | ||||||
| 		 |  | ||||||
| 		/* |  | ||||||
| 		 * Double check connection to MySQL |  | ||||||
| 		 */ |  | ||||||
| 		try  |  | ||||||
| 		{ |  | ||||||
| 			if(!conn.isValid(5)) |  | ||||||
| 			{ |  | ||||||
| 			reconnect(); |  | ||||||
| 			} |  | ||||||
| 		} catch (SQLException e)  |  | ||||||
| 		{ |  | ||||||
| 			e.printStackTrace(); |  | ||||||
| 		} |  | ||||||
| 		 |  | ||||||
| 		try { | 		try { | ||||||
| 			stmt = this.conn.prepareStatement(sql); | 			Connection conn = DriverManager.getConnection(connectionString); | ||||||
|  | 			PreparedStatement stmt = conn.prepareStatement(sql); | ||||||
|  | 			stmt = conn.prepareStatement(sql); | ||||||
| 			if (stmt.executeQuery() != null) { | 			if (stmt.executeQuery() != null) { | ||||||
| 				stmt.executeQuery(); | 				stmt.executeQuery(); | ||||||
| 				rs = stmt.getResultSet(); | 				rs = stmt.getResultSet(); | ||||||
| 				if (rs.next()) { | 				if (rs.next()) { | ||||||
| 					result = rs.getInt(1); | 					result = rs.getInt(1); | ||||||
| 		        } | 				} else { | ||||||
| 		        else { result = 0; } | 					result = 0; | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		catch (SQLException ex) { | 			conn.close(); | ||||||
|  | 		} catch (SQLException ex) { | ||||||
| 			System.out.println("SQLException: " + ex.getMessage()); | 			System.out.println("SQLException: " + ex.getMessage()); | ||||||
| 			System.out.println("SQLState: " + ex.getSQLState()); | 			System.out.println("SQLState: " + ex.getSQLState()); | ||||||
| 			System.out.println("VendorError: " + ex.getErrorCode()); | 			System.out.println("VendorError: " + ex.getErrorCode()); | ||||||
| @@ -201,26 +136,11 @@ public class Database { | |||||||
|  |  | ||||||
| 	// read query | 	// read query | ||||||
| 	public HashMap<Integer, ArrayList<String>> Read(String sql) { | 	public HashMap<Integer, ArrayList<String>> Read(String sql) { | ||||||
| 		/* |  | ||||||
| 		 * Double check connection to MySQL |  | ||||||
| 		 */ |  | ||||||
| 		try  |  | ||||||
| 		{ |  | ||||||
| 			if(!conn.isValid(5)) |  | ||||||
| 			{ |  | ||||||
| 			reconnect(); |  | ||||||
| 			} |  | ||||||
| 		} catch (SQLException e)  |  | ||||||
| 		{ |  | ||||||
| 			e.printStackTrace(); |  | ||||||
| 		} |  | ||||||
| 		 |  | ||||||
|   		PreparedStatement stmt = null; |  | ||||||
| 		ResultSet rs = null; | 		ResultSet rs = null; | ||||||
| 		HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>(); | 		HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>(); | ||||||
| 		 |  | ||||||
| 		try { | 		try { | ||||||
| 			stmt = this.conn.prepareStatement(sql); | 			Connection conn = DriverManager.getConnection(connectionString); | ||||||
|  | 			PreparedStatement stmt = conn.prepareStatement(sql); | ||||||
| 			if (stmt.executeQuery() != null) { | 			if (stmt.executeQuery() != null) { | ||||||
| 				stmt.executeQuery(); | 				stmt.executeQuery(); | ||||||
| 				rs = stmt.getResultSet(); | 				rs = stmt.getResultSet(); | ||||||
| @@ -232,28 +152,12 @@ public class Database { | |||||||
| 					Rows.put(rs.getRow(), Col); | 					Rows.put(rs.getRow(), Col); | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 			conn.close(); | ||||||
| 		catch (SQLException ex) { | 		} catch (SQLException ex) { | ||||||
| 			System.out.println("SQLException: " + ex.getMessage()); | 			System.out.println("SQLException: " + ex.getMessage()); | ||||||
| 			System.out.println("SQLState: " + ex.getSQLState()); | 			System.out.println("SQLState: " + ex.getSQLState()); | ||||||
| 			System.out.println("VendorError: " + ex.getErrorCode()); | 			System.out.println("VendorError: " + ex.getErrorCode()); | ||||||
| 		} | 		} | ||||||
| 		 |  | ||||||
| 		// release dataset |  | ||||||
| 	    if (rs != null) { |  | ||||||
| 	        try { |  | ||||||
| 	            rs.close(); |  | ||||||
| 	        } catch (SQLException sqlEx) { } // ignore |  | ||||||
| 	        rs = null; |  | ||||||
| 	    } |  | ||||||
| 	    if (stmt != null) { |  | ||||||
| 	        try { |  | ||||||
| 	            stmt.close(); |  | ||||||
| 	        } catch (SQLException sqlEx) { } // ignore |  | ||||||
| 	        stmt = null; |  | ||||||
| 	    } |  | ||||||
|  |  | ||||||
| 		return Rows; | 		return Rows; | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 nossr50
					nossr50