0

I need to make a mysql connection and get some data back. I can do this in Java using this code

        try{
                     String username;
                      String password;
                      username = tf.getText();
                      password = tf2.getText();
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    Connection con = DriverManager.getConnection("jdbc:mysql://188.181.248.30:3306/test","simon","123");
                    int counter = 0;
                    try{
                      Statement st = con.createStatement();
                      ResultSet res = st.executeQuery("SELECT * FROM  KUNDER");
                   //  System.out.println("NAVN: " + "\t" + "KREDIT: ");
                      while (res.next() && counter ==0) {
                        String i = res.getString("NAVN");
                        String s = res.getString("KREDIT");
                        System.out.println(i + "\t\t" + s);
                        if(username.equals(i)){
                            //stf3.setText("login true");
                    //      System.out.println("username og navn passer sammen et sted");
                            if(password.equals(s)){
                                tf3.setText("login true1");
                    //      System.out.println("pass og navn passer sammen");   
                            counter=10;
                            }else{
                                    tf3.setText("login fail");
                                    counter =10;

                            }
                            }else{
                                //tf3.setText("login fail");
                            }
                      }
                      con.close();
                    }
                    catch (SQLException s){
                        tf3.setText("sql fail");
                      System.out.println("SQL code does not execute.");
                    }    

But when I try to do this in Android, I get an error when trying to launch the app saying:

[2011-03-06 00:30:04 - sqlLite] Conversion to Dalvik format failed with error 1

I don't know what to do right now. I been reading around the net, but can't find anything I can get working.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • could you please paste exactly the entire code you're using, and with a better indentation? in this code, the first `try {...` never gets closed. – Oscar Mederos Mar 05 '11 at 23:53

2 Answers2

1

Do not access MySQL from Android via JDBC. Wrap your MySQL database in a Web service, and access the Web service from Android.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • okay im not that good at databases and all that so you are saying that there are no why that i can acces my mysql database true android like i due in normal java ??? –  Mar 05 '11 at 23:51
  • @simondid: Mr. Knego's linked-to answer is an excellent summary. In addition to the problems he cites, JDBC is not designed for use on unstable network connections, such as 3G. I would even hesitate to use JDBC over WiFi with a highly-mobile client, such as most Android devices. – CommonsWare Mar 06 '11 at 00:48
  • at what i can undet stand you gouys are saying that all this i becouse off security over the g3 network is that right –  Mar 06 '11 at 09:53
1

You might also want to read: Why is the paradigm of "Direct Database Connection" not welcomed by Android Platform?

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • so i my just forget the idear off making a mysql connection to retrive data and writh data?? –  Mar 06 '11 at 00:01
  • Yes. As CommonsWare noted, you should expose your database via a WebService (preferably REST). – Peter Knego Mar 06 '11 at 00:04
  • okay so i thing i just forgeth my idear about making some thing with mysql i if i can't make it as easy as i can in java –  Mar 06 '11 at 00:08