Hallo,
ich versuche gerade schon seit längerer Zeit eine MySQL Verbindung zum Laufen zu bringen.
habe folgenden Code in meiner MySQLConnection.java:
Java
package de.soohallo.shoebilltest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLConnection {
private Connection connection;
private String host = "*********";
private String port = "3306";
private String username = "samp";
private String dbname = "samp";
private String password = "**********";
public void initConnection(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + dbname, username, password);
Shoebilltest.logger().info("|MySQL| Verbindung erfolgreich hergestellt.");
} catch (Exception ex) {
Shoebilltest.logger().info("|MySQL| Es ist ein Fehler aufgetreten!");
ex.printStackTrace();
}
}
public void closeConnection() {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public int executeUpdate(String query) {
try {
if (connection != null && connection.isValid(1000)) {
Statement stmnt = connection.createStatement();
stmnt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
try {
ResultSet rs = stmnt.getGeneratedKeys();
rs.next();
return rs.getInt(1);
} catch (Exception ignored) { }
return -1;
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public ResultSet executeQuery(String query) {
ResultSet rs = null;
Statement statement;
try {
if (connection != null && connection.isValid(1000)) {
statement = connection.createStatement();
rs = statement.executeQuery(query);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public boolean check(String table, String field, String str) {
ResultSet rs;
Statement statement;
String query = String.format("SELECT * FROM %s WHERE %s = '%s'", table, field, str);
try {
if (connection != null && connection.isValid(1000)) {
statement = connection.createStatement();
rs = statement.executeQuery(query);
if(rs.first()){
try {
if(rs.getString(field).toLowerCase().equals(str.toLowerCase())){
return true;
}
} catch (Exception e){
return false;
}
}
}
else {
return false;
}
} catch (SQLException e) {
System.out.print("ERROR - Stacktrace : ");
e.printStackTrace();
}
return false;
}
public boolean existsPlayer(String name){
ResultSet rs = null;
Statement statement;
String query = String.format("SELECT * FROM samp WHERE name = '%s'", name);
try {
if (connection != null && connection.isValid(1000)) {
statement = connection.createStatement();
rs = statement.executeQuery(query);
if(rs.first()){
return true;
}
}
else {
return false;
}
} catch (SQLException e) {
System.out.print("ERROR - Stacktrace : ");
e.printStackTrace();
}
return false;
}
public String newPlayer(String name){
try {
if(connection != null && connection.isValid(1000)){
executeUpdate("INSERT INTO samp (name, level, money, adminlevel) VALUES ('" + name + "', '1', '50000', '0')");
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int getPlayerLevel(String name){
ResultSet rs = null;
Statement statement;
String query = String.format("SELECT * FROM samp WHERE name = '%s'", name);
try {
if(connection != null && connection.isValid(1000)){
statement = connection.createStatement();
rs = statement.executeQuery(query);
if(rs.first()){
return rs.getInt("level");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public int getPlayerMoney(String name){
ResultSet rs = null;
Statement statement;
String query = String.format("SELECT * FROM samp WHERE name = '%s'", name);
try {
if(connection != null && connection.isValid(1000)){
statement = connection.createStatement();
rs = statement.executeQuery(query);
if(rs.first()){
return rs.getInt("money");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public int getPlayerAdminLevel(String name){
ResultSet rs = null;
Statement statement;
String query = String.format("SELECT * FROM samp WHERE name = '%s'", name);
try {
if(connection != null && connection.isValid(1000)){
statement = connection.createStatement();
rs = statement.executeQuery(query);
if(rs.first()){
return rs.getInt("adminlevel");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public int getPlayerSkin(String name){
ResultSet rs = null;
Statement statement;
String query = String.format("SELECT * FROM samp WHERE name = '%s'", name);
try {
if(connection != null && connection.isValid(1000)){
statement = connection.createStatement();
rs = statement.executeQuery(query);
if(rs.first()){
return rs.getInt("skin");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
}
Alles anzeigen
und bekomme beim Aufrufen der initConnection() in meiner Hauptklasse folgenden Fehler:
Code
[12:29:58][ERROR][err] java.lang.NullPointerException
[12:29:58][ERROR][err] at de.soohallo.shoebilltest.Shoebilltest.onEnable(Shoebilltest.java:43)
[12:29:58][ERROR][err] at net.gtaun.shoebill.resource.Resource.enable(Resource.java:91)
[12:29:58][ERROR][err] at net.gtaun.shoebill.resource.ResourceManagerImpl.loadGamemode(ResourceManagerImpl.java:179)
[12:29:58][ERROR][err] at net.gtaun.shoebill.resource.ResourceManagerImpl.loadAllResource(ResourceManagerImpl.java:62)
[12:29:58][ERROR][err] at net.gtaun.shoebill.ShoebillImpl.loadPluginsAndGamemode(ShoebillImpl.java:289)
[12:29:58][ERROR][err] at net.gtaun.shoebill.ShoebillImpl.access$100(ShoebillImpl.java:47)
[12:29:58][ERROR][err] at net.gtaun.shoebill.ShoebillImpl$1.onAmxLoad(ShoebillImpl.java:233)
[12:29:58][ERROR][err] at net.gtaun.shoebill.samp.SampCallbackManagerImpl$1.lambda$null$80(SampCallbackManagerImpl.java:60)
[12:29:58][ERROR][err] at net.gtaun.shoebill.util.TryUtils.tryTo(TryUtils.java:21)
[12:29:58][ERROR][err] at net.gtaun.shoebill.util.TryUtils.tryTo(TryUtils.java:14)
[12:29:58][ERROR][err] at net.gtaun.shoebill.samp.SampCallbackManagerImpl$1.lambda$onAmxLoad$81(SampCallbackManagerImpl.java:60)
[12:29:58][ERROR][err] at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
[12:29:58][ERROR][err] at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source)
[12:29:58][ERROR][err] at java.util.concurrent.ConcurrentLinkedQueue$CLQSpliterator.forEachRemaining(Unknown Source)
[12:29:58][ERROR][err] at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
[12:29:58][ERROR][err] at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
[12:29:58][ERROR][err] at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
[12:29:59][ERROR][err] at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
[12:29:59][ERROR][err] at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
[12:29:59][ERROR][err] at java.util.stream.ReferencePipeline.forEach(Unknown Source)
[12:29:59][ERROR][err] at net.gtaun.shoebill.samp.SampCallbackManagerImpl$1.onAmxLoad(SampCallbackManagerImpl.java:60)
Alles anzeigen