-1

Our app should connect to a SQL Database. It is in our Network. The App should edit data in the database. We have built the connection and want to set a onclicklistener to a Button, witch causes the connection code to connect.

this is the code we have got:

public class Werte_aendern extends AppCompatActivity {

TextView tvIP;

String Textauslesen = tvIP.getText().toString();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tvIP = (TextView) findViewById(R.id.tvIP);
    setContentView(R.layout.activity_werte_aendern);
}




     Connection con = null;
    //private static String dbHost = "192.168.40.148"; // Hostname
     String dbPort = "3306";      // Port -- Standard: 3306
     String dbName = "wasserwerte";   // Datenbankname
     String dbUser = "App";     // Datenbankuser
     String dbPass = "fruitcake";      // Datenbankpasswort

    private Werte_aendern(){
        try {
            Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden.

            // Verbindung zur JDBC-Datenbank herstellen.
            con = DriverManager.getConnection("jdbc:mysql://"+Textauslesen+":"+ dbPort+"/"+dbName+"?"+"user="+dbUser+"&"+"password="+dbPass);
          //  Statement createStatement();
          //  SQLiteDatabase wasserwerte =

        } catch (ClassNotFoundException e) {
            Toast.makeText(getApplicationContext(), "Treiber nicht gefunden", Toast.LENGTH_SHORT).show();
        } catch (SQLException e) {
            Toast.makeText(getApplicationContext(), "Verbindung nicht möglich", Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "SQLException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "SQLState: " + e.getSQLState(), Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "VendorError: " + e.getErrorCode(), Toast.LENGTH_SHORT).show();
        }
    }

}

We are noobs, but we have to do this for a schoolproject.
Can you help us please?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • There is not button in your code, so you will not be able to add a listener on one… Do you know how to create one? Please show us where you declare the button (java code or xml) – deHaar Oct 13 '17 at 15:17
  • @deHaar It's not necessary. In the layout you can reference a click event handler. But that's also not present in the shown code. – Phantômaxx Oct 13 '17 at 15:18
  • OK youre right, but the question was explicitly about an OnClickListener, so I thought there should be one in the code ;-) I know you can just write a method and put it in xml onClick. – deHaar Oct 13 '17 at 15:19
  • @deHaar we have no idea where to put the Button and the Onclicklistener, sorry, can you put it into the code at the right place? – AnfängerBenni123 Oct 13 '17 at 15:21

1 Answers1

1

I've updated your code. An Activity should always be named [Whatever]Activity. Donnot if "WerteAendern" is a correct name since I don't speak German (I assume it's German).

  public class WerteAendernActivity extends AppCompatActivity {

    TextView tvIP;

    // You should get the text from the View AFTER inflating the layout and find it with
    // findViewById. Otherwise it's gonna crash.
    String textauslesen;
    private Button connectBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // First you inflate the layout
        setContentView(R.layout.activity_werte_aendern);
        // Then you get the views
        tvIP = (TextView) findViewById(R.id.tvIP);
        // Probably should be somewhere else as there is no interesting text to retrieve from the 
        // view at the moment
        textauslesen = tvIP.getText().toString();  

        connectBtn = (Button) findViewById(R.id.connection_button); // ! You need to add a Button in your layout
        connectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Add some test, ... if you're already connected
                connectToDataBase();
            }
        });
    }

    Connection con = null;
    //private static String dbHost = "192.168.40.148"; // Hostname
    String dbPort = "3306";      // Port -- Standard: 3306
    String dbName = "wasserwerte";   // Datenbankname
    String dbUser = "App";     // Datenbankuser
    String dbPass = "fruitcake";      // Datenbankpasswort


    // Method to connect to the database.
    // !!! You're not supposed to override the constructor of an Activity!
    private void connectToDataBase() {
        try {
            Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden.

            // Verbindung zur JDBC-Datenbank herstellen.
            con = DriverManager.getConnection("jdbc:mysql://" + textauslesen + ":" + dbPort + "/" + dbName + "?" + "user=" + dbUser + "&" + "password=" + dbPass);
            //  Statement createStatement();
            //  SQLiteDatabase wasserwerte =

        } catch (ClassNotFoundException e) {
            Toast.makeText(getApplicationContext(), "Treiber nicht gefunden", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        } catch (SQLException e) {
            Toast.makeText(getApplicationContext(), "Error! See Exception logs", Toast.LENGTH_SHORT).show();
            // The logs will be displayed in the Logcat window in Android Studio 
            e.printStackTrace();
        }
    }
}
Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • 1
    You seem to be indeed beginners with Android. I'd suggest to follow some tutorials to do very basic apps first before starting to use databases. Start an Activity, inflate a layout, get the Views and interact with them should be the first step. You can't run before having learning to walk! – Eselfar Oct 13 '17 at 15:36
  • Thank you very much. You helped us a lot. But our App keeps crashing when we are clicking the button in the main activity which relegades to the activity you have seen the code of (WerteAendernActivity). What could be the matter of this issue? – AnfängerBenni123 Oct 13 '17 at 16:06
  • What do you mean "The main activity which relegates to the Activity above"? If your button is in the layout of the activity above and you try to access it from an other one with a different layout, yes it crashes. But at least put the error log for us to have an idea of what the issue is. – Eselfar Oct 14 '17 at 23:43