Android App connected to MySql Database

409 views
Skip to first unread message

Elyes BenSalah

unread,
Dec 7, 2014, 12:19:09 PM12/7/14
to ormlite...@googlegroups.com
Hi Guys , 

i'm trying to connecte my App to MySQL database  but i dont know how  this is my class : 
package com.example.ormlitetomysql;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper{

private static final String DATABASE_NAME="testorm";
private static final String DATABASE_USERNAME="root";
private static final String DATABSE_PASSWORD ="esprit";
private static final int DATABASE_VERSION=1;
private static final  String driver = "com.mysql.jdbc.Driver";
private static final String databaseURL = "jdbc:mysql://localhost:3306/testorm";
ConnectionSource connectionSource=null;
{
if (connectionSource==null){
try {
Class.forName(driver);
connectionSource=new JdbcConnectionSource(databaseURL,DATABASE_USERNAME,DATABSE_PASSWORD);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private Dao<User, Integer> userDao=null;
private RuntimeExceptionDao<User, Integer> userRuntimeDao = null;
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION,R.raw.database_config);
}


@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, User.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion,
int newVersion) {
try {
TableUtils.dropTable(connectionSource, User.class, true);
onCreate(database,connectionSource);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Dao<User,Integer>getDao(){
if(userDao==null){
try {
userDao = getDao(User.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return userDao;
}
public RuntimeExceptionDao<User, Integer> getUserRuntimeExceptionDao(){
if(userRuntimeDao==null){
userRuntimeDao= getRuntimeExceptionDao(User.class);
}
return userRuntimeDao;
}

}


Logcat : 

12-07 17:17:13.183: I/OpenHelperManager(4176): helper was already closed and is being re-opened
12-07 17:17:13.183: I/DaoManager(4176): Loaded configuration for class com.example.ormlitetomysql.User
12-07 17:17:13.183: W/System.err(4176): java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
12-07 17:17:13.191: W/System.err(4176): at java.lang.Class.classForName(Native Method)
12-07 17:17:13.191: W/System.err(4176): at java.lang.Class.forName(Class.java:251)
12-07 17:17:13.191: W/System.err(4176): at java.lang.Class.forName(Class.java:216)
12-07 17:17:13.191: W/System.err(4176): at com.example.ormlitetomysql.DatabaseHelper.<init>(DatabaseHelper.java:29)
12-07 17:17:13.191: W/System.err(4176): at java.lang.reflect.Constructor.constructNative(Native Method)
12-07 17:17:13.191: W/System.err(4176): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
12-07 17:17:13.191: W/System.err(4176): at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:220)
12-07 17:17:13.191: W/System.err(4176): at com.j256.ormlite.android.apptools.OpenHelperManager.loadHelper(OpenHelperManager.java:170)
12-07 17:17:13.191: W/System.err(4176): at com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(OpenHelperManager.java:78)
12-07 17:17:13.191: W/System.err(4176): at com.example.ormlitetomysql.MainActivity.testOrm(MainActivity.java:29)
12-07 17:17:13.195: W/System.err(4176): at com.example.ormlitetomysql.MainActivity.onCreate(MainActivity.java:25)
12-07 17:17:13.195: W/System.err(4176): at android.app.Activity.performCreate(Activity.java:5231)
12-07 17:17:13.195: W/System.err(4176): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-07 17:17:13.195: W/System.err(4176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
12-07 17:17:13.195: W/System.err(4176): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
12-07 17:17:13.195: W/System.err(4176): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-07 17:17:13.195: W/System.err(4176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-07 17:17:13.195: W/System.err(4176): at android.os.Handler.dispatchMessage(Handler.java:102)
12-07 17:17:13.195: W/System.err(4176): at android.os.Looper.loop(Looper.java:136)
12-07 17:17:13.195: W/System.err(4176): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-07 17:17:13.195: W/System.err(4176): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 17:17:13.195: W/System.err(4176): at java.lang.reflect.Method.invoke(Method.java:515)
12-07 17:17:13.195: W/System.err(4176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-07 17:17:13.195: W/System.err(4176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-07 17:17:13.195: W/System.err(4176): at dalvik.system.NativeStart.main(Native Method)
12-07 17:17:13.195: W/System.err(4176): Caused by: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
12-07 17:17:13.207: W/System.err(4176): ... 25 more
12-07 17:17:13.207: W/System.err(4176): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mysql.jdbc.Driver" on path: DexPathList[[zip file "/data/app/com.example.ormlitetomysql-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.ormlitetomysql-1, /system/lib]]
12-07 17:17:13.223: W/System.err(4176): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-07 17:17:13.223: W/System.err(4176): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
12-07 17:17:13.223: W/System.err(4176): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-07 17:17:13.223: W/System.err(4176): ... 25 more
12-07 17:17:13.319: D/Users(4176): [User [id=1, login=Elyes, password=123, dateInscrit=Sun Dec 07 17:16:39 GMT+00:00 2014], User [id=2, login=TEST1, password=123, dateInscrit=Sun Dec 07 17:16:39 GMT+00:00 2014], User [id=3, login=TEST2, password=123, dateInscrit=Sun Dec 07 17:16:39 GMT+00:00 2014], User [id=4, login=TEST3, password=123, dateInscrit=Sun Dec 07 17:16:39 GMT+00:00 2014], User [id=5, login=Elyes, password=123, dateInscrit=Sun Dec 07 17:17:13 GMT+00:00 2014], User [id=6, login=TEST1, password=123, dateInscrit=Sun Dec 07 17:17:13 GMT+00:00 2014], User [id=7, login=TEST2, password=123, dateInscrit=Sun Dec 07 17:17:13 GMT+00:00 2014], User [id=8, login=TEST3, password=123, dateInscrit=Sun Dec 07 17:17:13 GMT+00:00 2014]]
12-07 17:17:13.407: W/EGL_genymotion(4176): eglSurfaceAttrib not implemented


 i dont find any data in my database

David Faerman

unread,
Dec 7, 2014, 2:53:25 PM12/7/14
to ormlite...@googlegroups.com
you dont....
connect to a remote mysql data base directly is la shoot in your feet... you leave it open for hackers
you should create an api.
David

--
You received this message because you are subscribed to the Google Groups "ORMLite Android Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ormlite-andro...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
 (\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.
Reply all
Reply to author
Forward
0 new messages