Problem bei Werteübergabe, MSQL verbindung

MurderItachi

Mitglied
Hey Leute,

ich bin dabei eine App zu programmieren, die Codes abscannt und diese dann in eine Datenbank schreibt. Das Scannen funktioniert. Das schreiben von Werten in eine Datenbank auch, zumindest wenn man es als normales Java Projekt ausführt.
Bei einer mysql connection braucht man ja den Treiber dazu, den man normal als externes Jar file Importiert. Das hat normal auch gut geklappt, wenn ich das jar file aber meine App hinzufüge, brüllt Lint, dass die Klassen Feherhaft sind. Desweiteren denke ich, dass ich Probleme mit der übergabe der Werte habe zwischen und innerhalb der zwei Klassen, obwohl für mich alles logisch aussieht. Jedes mal wenn ich den Send butten drücke, beendet sich die App.

Möchte da jemand einen Blick riskieren?

Java:
package com.example.appfinal;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends Activity implements OnClickListener {
    //UI instance variables
    private Button scanBtn;
    private Button sendBtn;
    private EditText preis_eingabe;

    public String getScan_result() {
        return scan_result;
    }

    public String getPrice() {
        return price;
    }

    public String scan_result="0";
    public String price="0";
    private TextView formatTxt, contentTxt, textView1, textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //instantiate UI items
        this.scanBtn = (Button)findViewById(R.id.scan_button);
        this.formatTxt = (TextView)findViewById(R.id.scan_format);
        this.contentTxt = (TextView)findViewById(R.id.scan_content);
        this.sendBtn =    (Button)findViewById(R.id.send_button);
        this.preis_eingabe= (EditText)findViewById(R.id.editText1);
        this.textView2=(TextView)findViewById(R.id.textView2);
        this.textView1= (TextView)findViewById(R.id.textView1);
        //listen for clicks
        scanBtn.setOnClickListener(this);
        sendBtn.setOnClickListener(this);
    }

    public void onClick(View v) {     
        this.price=preis_eingabe.getText().toString();     
        //check for scan button
        if (v.getId() == R.id.scan_button) {
            //instantiate ZXing integration class
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            //start scanning
            scanIntegrator.initiateScan();
        } else if (v.getId() == R.id.send_button) { 
            work.setScan_rs(scan_result);
            work.setPrice_rs(price);
            new work().execute();
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve result of scanning - instantiate ZXing object
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        //check we have a valid result
        if (scanningResult != null) {
            //get content from Intent Result
            String scanContent = scanningResult.getContents();
            this.scan_result=scanContent;
            //get format name of data scanned
            String scanFormat = scanningResult.getFormatName();
            //output to UI
            formatTxt.setText("FORMAT: "+scanFormat);
            contentTxt.setText("CONTENT: "+scanContent);     
            //zur Kontrolle der werteübergabe, die Ergebnisse in ein Textfeld schreiben-funktioniert allerdings nicht         
            textView1.setText(this.scan_result);
            textView2.setText(this.price);
        } else{
            //invalid scan data or scan canceled
            Toast toast = Toast.makeText(getApplicationContext(),
                    "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

Java:
package com.example.appfinal;

import android.media.AsyncPlayer;
import android.os.AsyncTask;

import java.sql.BatchUpdateException;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Created by Alex on 10.12.2015.
*/
public class work extends AsyncTask <Void, Void, Void>{

    public String url= "jdbc:mysql://sql4.freesqldatabase.com:3306/sql499392";
    public String user= "******";
    public String pw="******";
    public static String price_rs;
    Connection con=null;
    public static String scan_rs;

    public static void setPrice_rs(String price_rss) {
        price_rs = price_rss;
    }

    public static void setScan_rs(String scan_rss) {
        scan_rs = scan_rss;
    }

    @Override
    protected Void doInBackground(Void... args0){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // Bl�d: Treiber konnte nicht geladen werden.
            System.out.println("Treiber konnte nicht geladen werden");
        }
        try {
            con = DriverManager.getConnection(url, user, pw);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            PreparedStatement stmt = (PreparedStatement) con.prepareStatement("INSERT INTO barcode VALUES (?, ?)");

            stmt.setString(1, scan_rs);
            stmt.setString(2, price_rs);
            stmt.executeUpdate();
            System.out.println("hat alles geklappt");
        } catch (SQLException e) {
  e.printStackTrace();
System.out.println("fehler2");}

        try {
            con.close();
        } catch(SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("connection konnte nicht geschlossen werden");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result){
        super.onPostExecute(result);
  e.printStackTrace();
    }
}
 
Zuletzt bearbeitet von einem Moderator:

Thallius

Top Contributor
Du solltest auf gar gar gar keinen Fall von einer Android App aus auf irgendeinen mysql server direkt zugreifen! Was Du da machst ist technischer Selbstmord. Wenn Du die App verbreitest, kann ich innerhalb von 10min Deine ganze Datenbank löschen wenn ich lustig bin.

Gruß

Claus
 

MurderItachi

Mitglied
Die App soll als kleines Pilot projekt dienen und nicht veröffentlicht werden, aber gut zu wissen. Wäre eine Alternative, die Daten vom Handy auf einen Pc zu schicken, der das dann wiederum Weiterleitet an die Datenbank? Und wenn ja, wie geht das in etwa?
 

buggy84

Bekanntes Mitglied
Thallius möchte damit sagen, dass man für sowas Webservices oder eine PHP-Bridge benutzt.
Wie in deinem anderen Thread sehe ich keine Fehlermeldung. Wie soll man Dir helfen, wenn wir nichts von Dir sehen?
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Prüfen, ob App auf Gerät installiert ist Problem S10 Android & Cross-Platform Mobile Apps 11
W In App Purchase Problem? Android & Cross-Platform Mobile Apps 36
W Problem mit Android Studio Android & Cross-Platform Mobile Apps 0
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
K Android to Pi | Websocket Problem Android & Cross-Platform Mobile Apps 3
N Intent und finish() Problem Android & Cross-Platform Mobile Apps 5
B Android App Programmierung Einsteiger Problem Android & Cross-Platform Mobile Apps 4
emeraldo Android Problem mit Bottomnavmenu Android & Cross-Platform Mobile Apps 10
I Das Problem mit der Tastatur... android:windowSoftInputMode="adjustPan" Android & Cross-Platform Mobile Apps 1
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
A Android Android Studio Emulator Problem Android & Cross-Platform Mobile Apps 1
S Android Studio Bluetooth App Problem Android & Cross-Platform Mobile Apps 6
J TicTacToe Problem bei kontrolle Android & Cross-Platform Mobile Apps 7
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
S Android Problem mit Android Virtual Device erstellung. Android & Cross-Platform Mobile Apps 2
Anfänger2011 Text to Speech Problem Android & Cross-Platform Mobile Apps 1
S Android Android java onclick listener Problem Android & Cross-Platform Mobile Apps 9
A Android Problem mit ListView und OnItemClickListener.. Android & Cross-Platform Mobile Apps 10
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
R W-Lan Problem über Sockets Android & Cross-Platform Mobile Apps 1
P ViewPager Problem Android & Cross-Platform Mobile Apps 1
A Android Problem mit Video von Youtube abspielen Android & Cross-Platform Mobile Apps 4
A Android Problem mit Zurücktaste und ausgabe der Aktuellen Seite Android & Cross-Platform Mobile Apps 6
B Android Problem mit Soundwiedergabe Android & Cross-Platform Mobile Apps 2
T Android Android Sensor: Java Problem Android & Cross-Platform Mobile Apps 1
G Problem beim Rendern von 3D-Objekt Android & Cross-Platform Mobile Apps 0
L Android Gyroscope Sensor Problem Android & Cross-Platform Mobile Apps 2
S Android GPS Problem Android & Cross-Platform Mobile Apps 24
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 1
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 0
B Android Problem mit Rückgabewert Android & Cross-Platform Mobile Apps 13
L Android komisches Bitmap-Größe-Problem Android & Cross-Platform Mobile Apps 8
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Eclipse + AndroidSDK - Problem mit Referenzen Android & Cross-Platform Mobile Apps 6
M Problem mit setOnClickListener Android & Cross-Platform Mobile Apps 4
DaniSahne96 Problem beim Appdebuggen auf Smartphone Android & Cross-Platform Mobile Apps 3
P Android Problem beim Widget - Denkfehler ? Android & Cross-Platform Mobile Apps 2
M GCM IntentService Problem Android & Cross-Platform Mobile Apps 3
D Android Gallery Problem Android & Cross-Platform Mobile Apps 5
P Problem mit Cell id Android & Cross-Platform Mobile Apps 6
L Android Problem mit "spinner" Android & Cross-Platform Mobile Apps 10
D Android problem mit geschwindigkeitsberechnung app Android & Cross-Platform Mobile Apps 2
E Android Problem mit Contact Provider Android & Cross-Platform Mobile Apps 1
H Android Problem mit ListActivity Android & Cross-Platform Mobile Apps 3
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
F Android ExpandableList, SimpleCursorTreeAdapter, Cursor Problem Android & Cross-Platform Mobile Apps 2
A Android Problem mit Long.getLong() bzw. Integer.getInteger() Android & Cross-Platform Mobile Apps 2
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
V [Java] und [JavaME] ClientServer StreamConnection . Problem beim lesen / schreiben Android & Cross-Platform Mobile Apps 2
F Eclipse JAD File erzeugen -- Problem Android & Cross-Platform Mobile Apps 10
R Ein Problem beim ausführen von folgendem Quelltext Android & Cross-Platform Mobile Apps 11
M Problem mit dem Auslesen von System Properties Android & Cross-Platform Mobile Apps 7
P wtk problem Android & Cross-Platform Mobile Apps 3
G Math exp() Problem Android & Cross-Platform Mobile Apps 4
G S40 Problem Android & Cross-Platform Mobile Apps 8
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
C Problem Device/Emulator wird nicht erkannt Android & Cross-Platform Mobile Apps 3
S Image Problem Android & Cross-Platform Mobile Apps 11
M Problem mit den Softkeys Android & Cross-Platform Mobile Apps 4
G J2ME jar-problem Android & Cross-Platform Mobile Apps 10
S Komisches Problem Android & Cross-Platform Mobile Apps 3
F Problem beim Erstellen der Jar File Android & Cross-Platform Mobile Apps 4
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
S Problem mit Einbindung einer externer Bibliothek Android & Cross-Platform Mobile Apps 2
G Random - Problem Android & Cross-Platform Mobile Apps 5
E problem mit den resourcen Android & Cross-Platform Mobile Apps 2
O Problem mit Datagramconnection Android & Cross-Platform Mobile Apps 2
P Problem mit der Uhrzeit Android & Cross-Platform Mobile Apps 2
S Problem auf dem Handy Android & Cross-Platform Mobile Apps 3
G Werteübergabe nach unbestimmter Zeit Android & Cross-Platform Mobile Apps 28

Ähnliche Java Themen

Neue Themen


Oben