Link to home
Start Free TrialLog in
Avatar of Gamer 08
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.sourceforge.jtds.jdbc.Driver");
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://" + 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.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
W/System.err: java.lang.NullPointerException: 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.getID(Mysql.java:27)
W/System.err:     at me.DevGamer08.g24l.Main$1.onClick(Main.java:36)
W/System.err:     at android.view.View.performClick(View.java:5637)
W/System.err:     at android.view.View$PerformClick.run(View.java:22429)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Where do you call connect()? And why are you calling con.createStatement as the penultimate line of getID()?
Avatar of Gamer 08
Gamer 08

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.
         return null;

Open in new window

That's asking for trouble in getStatement. I would throw a RuntimeException in your else block

So is

     
      rs.next();

Open in new window

since it's assuming the username is present. Not good to be assumptive

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
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 TRIAL
Members 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.