Gamer 08
asked on
Mysql | Android Problem
CODE:
package me.DevGamer08.g24l;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement ;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Created by Gamer08 on 31.03.2018.
*/
public class Mysql {
//Local Server
public static String host= "127.0.0.1";
//public static String host = "localhost";
public static String port = "3306";
public static String database = "app";
public static String username = "root";
public static String password = "";
public static String getID(String Name){
try{
PreparedStatement ps = Mysql.getStatement("SELECT * FROM user WHERE username= ?");
//ps.setString(1, Name);
ps.setString(0, "Gamer08");
ResultSet rs = ps.executeQuery();
rs.next();
String coins = rs.getString("passwort");
rs.close();
ps.close();
con.createStatement();
return coins;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
public static Connection con;
public static boolean isConnected(){
return con != null;
}
public static void connect(){
if(!isConnected()) try {
Class.forName("net.sourcef orge.jtds. jdbc.Drive r");
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void disconnect(){
try {
con.close();
} catch (SQLException e) {
}
}
public static PreparedStatement getStatement(String sql){
if(isConnected()){
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);
return ps;
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public static ResultSet getResult(String sql){
if(isConnected()){
PreparedStatement ps;
ResultSet rs;
try {
ps = getStatement(sql);
rs = ps.executeQuery();
return rs;
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
ERROR:
D/EGL_emulation: eglMakeCurrent: 0xa07a7880: ver 3 0 (tinfo 0x9e87f7b0)
W/art: Before Android 4.1, method int android.support.v7.widget. ListViewCo mpat.lookF orSelectab lePosition (int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
W/System.err: java.lang.NullPointerExcep tion: Attempt to invoke interface method 'void java.sql.PreparedStatement .setString (int, java.lang.String)' on a null object reference
W/System.err: at me.DevGamer08.g24l.Mysql.g etID(Mysql .java:27)
W/System.err: at me.DevGamer08.g24l.Main$1. onClick(Ma in.java:36 )
W/System.err: at android.view.View.performC lick(View. java:5637)
W/System.err: at android.view.View$PerformC lick.run(V iew.java:2 2429)
W/System.err: at android.os.Handler.handleC allback(Ha ndler.java :751)
W/System.err: at android.os.Handler.dispatc hMessage(H andler.jav a:95)
W/System.err: at android.os.Looper.loop(Loo per.java:1 54)
W/System.err: at android.app.ActivityThread .main(Acti vityThread .java:6119 )
W/System.err: at java.lang.reflect.Method.i nvoke(Nati ve Method)
W/System.err: at com.android.internal.os.Zy goteInit$M ethodAndAr gsCaller.r un(ZygoteI nit.java:8 86)
W/System.err: at com.android.internal.os.Zy goteInit.m ain(Zygote Init.java: 776)
package me.DevGamer08.g24l;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Created by Gamer08 on 31.03.2018.
*/
public class Mysql {
//Local Server
public static String host= "127.0.0.1";
//public static String host = "localhost";
public static String port = "3306";
public static String database = "app";
public static String username = "root";
public static String password = "";
public static String getID(String Name){
try{
PreparedStatement ps = Mysql.getStatement("SELECT
//ps.setString(1, Name);
ps.setString(0, "Gamer08");
ResultSet rs = ps.executeQuery();
rs.next();
String coins = rs.getString("passwort");
rs.close();
ps.close();
con.createStatement();
return coins;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
public static Connection con;
public static boolean isConnected(){
return con != null;
}
public static void connect(){
if(!isConnected()) try {
Class.forName("net.sourcef
Class.forName("com.mysql.j
con = DriverManager.getConnectio
con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void disconnect(){
try {
con.close();
} catch (SQLException e) {
}
}
public static PreparedStatement getStatement(String sql){
if(isConnected()){
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);
return ps;
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public static ResultSet getResult(String sql){
if(isConnected()){
PreparedStatement ps;
ResultSet rs;
try {
ps = getStatement(sql);
rs = ps.executeQuery();
return rs;
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
ERROR:
D/EGL_emulation: eglMakeCurrent: 0xa07a7880: ver 3 0 (tinfo 0x9e87f7b0)
W/art: Before Android 4.1, method int android.support.v7.widget.
W/System.err: java.lang.NullPointerExcep
W/System.err: at me.DevGamer08.g24l.Mysql.g
W/System.err: at me.DevGamer08.g24l.Main$1.
W/System.err: at android.view.View.performC
W/System.err: at android.view.View$PerformC
W/System.err: at android.os.Handler.handleC
W/System.err: at android.os.Handler.dispatc
W/System.err: at android.os.Looper.loop(Loo
W/System.err: at android.app.ActivityThread
W/System.err: at java.lang.reflect.Method.i
W/System.err: at com.android.internal.os.Zy
W/System.err: at com.android.internal.os.Zy
Where do you call connect()? And why are you calling con.createStatement as the penultimate line of getID()?
ASKER
Thanks for your fast awnser...
I call the "connect()" Method in my Main class :
@Override
protected void onCreate(Bundle savedInstanceState) {
Mysql.connect();
}
And "con.createStatement" had i found on Google, in other projects it went well without this.
I call the "connect()" Method in my Main class :
@Override
protected void onCreate(Bundle savedInstanceState) {
Mysql.connect();
}
And "con.createStatement" had i found on Google, in other projects it went well without this.
That's asking for trouble in getStatement. I would throw a RuntimeException in your else blockreturn null;
So is
since it's assuming the username is present. Not good to be assumptivers.next();
And "con.createStatement" had i found on Google, in other projects it went well without this.Get rid of it. It's useless
I call the "connect()" Method in my Main class :I don't do Android so am not certain about the lifecycle. Isn't onCreateBundle just created on init? If so, that might not be reliable
ASKER
Okay, thanks for your anwser i will test it in a other start method
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.