JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 各位大大!Android連結MySQL時,出現沒發現Class?






發文: 60
積分: 0
於 2015-03-01 04:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位大大!
Android連結MySQL時,我有加入mysql-connector-java-5.1.34-bin.jar的驅動檔案,

並且加入了Class.forName("org.gjt.mm.mysql.Driver");

以及import org.gjt.mm.mysql.Driver;

但遠端的MySQL伺服器,仍然收不到資料,請問各位大大該如何解決呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package workshop.connector_mysql;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
import org.gjt.mm.mysql.Driver;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
 
public class MainActivity extends ActionBarActivity {
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
      Class.forName("org.gjt.mm.mysql.Driver");
      Connection conn = DriverManager
          .getConnection("jdbc:mysql://48.359.68.567/GjunDB1?user=root&password=12345");
      Statement stmt = conn.createStatement();
      // 指定該次交易為明確交易
      conn.setAutoCommit(false);
      String sqlStr = String.format(
          "insert into student values('%s',%b,'%s')", "10306083",
          false, "Mary");
      int result = stmt.executeUpdate(sqlStr);
      sqlStr = String
          .format("insert into score (StudNo,ExamNo,Chinese,Math) values('%s',%d,%d,%d)",
              "10306093", 1, 77, 88);
      result = stmt.executeUpdate(sqlStr);
      sqlStr = String
          .format("insert into score (StudNo,ExamNo,Chinese,Math) values('%s',%d,%d,%d)",
              "10306093", 2, 88, 99);
      result = stmt.executeUpdate(sqlStr);
      sqlStr = String
          .format("insert into score (StudNo,ExamNo,Chinese,Math) values('%s',%d,%d,%d)",
              "10306093", 3, 99, 100);
      result = stmt.executeUpdate(sqlStr);
      // 確認交易
      conn.commit();
      stmt.close();
      conn.close();
    } catch (SQLException ex) {
      ex.printStackTrace();
 
    } catch (ClassNotFoundException e) {
      // TODO 自動產生的 catch 區塊
      e.printStackTrace();
      System.out.println("沒發現Class");
    }
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
 
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}


以下為logcat:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
02-28 23:33:37.642: D/AndroidRuntime(1968): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-28 23:33:37.642: W/linker(1968): libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.
02-28 23:33:37.642: D/AndroidRuntime(1968): CheckJNI is ON
02-28 23:33:37.642: D/dalvikvm(1968): Trying to load lib libjavacore.so 0x0
02-28 23:33:37.642: D/dalvikvm(1968): Added shared lib libjavacore.so 0x0
02-28 23:33:37.652: D/dalvikvm(1968): Trying to load lib libnativehelper.so 0x0
02-28 23:33:37.652: D/dalvikvm(1968): Added shared lib libnativehelper.so 0x0
02-28 23:33:37.652: D/dalvikvm(1968): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
02-28 23:33:37.662: D/dalvikvm(1968): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
02-28 23:33:37.692: E/memtrack(1968): Couldn't load memtrack module (No such file or directory)
02-28 23:33:37.692: E/android.os.Debug(1968): failed to load memtrack module: -2
02-28 23:33:37.702: D/AndroidRuntime(1968): Calling main entry com.android.commands.pm.Pm
02-28 23:33:37.712: D/AndroidRuntime(1968): Shutting down VM
02-28 23:33:37.712: D/dalvikvm(1968): Debugger has detached; object registry had 1 entries
02-28 23:33:37.712: I/AndroidRuntime(1968): NOTE: attach of thread 'Binder_1' failed
02-28 23:33:37.972: D/AndroidRuntime(1978): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-28 23:33:37.972: W/linker(1978): libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.
02-28 23:33:37.982: D/AndroidRuntime(1978): CheckJNI is ON
02-28 23:33:37.982: D/dalvikvm(1978): Trying to load lib libjavacore.so 0x0
02-28 23:33:37.982: D/dalvikvm(1978): Added shared lib libjavacore.so 0x0
02-28 23:33:37.982: D/dalvikvm(1978): Trying to load lib libnativehelper.so 0x0
02-28 23:33:37.982: D/dalvikvm(1978): Added shared lib libnativehelper.so 0x0
02-28 23:33:37.982: D/dalvikvm(1978): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
02-28 23:33:37.992: D/dalvikvm(1978): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
02-28 23:33:38.022: E/memtrack(1978): Couldn't load memtrack module (No such file or directory)
02-28 23:33:38.022: E/android.os.Debug(1978): failed to load memtrack module: -2
02-28 23:33:38.042: D/AndroidRuntime(1978): Calling main entry com.android.commands.am.Am
02-28 23:33:38.042: I/ActivityManager(1274): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=workshop.connector_mysql/.MainActivity} from pid 1978
02-28 23:33:38.052: E/gralloc_goldfish(936): gralloc_alloc: Mismatched usage flags: 492 x 874, usage 333
02-28 23:33:38.052: W/GraphicBufferAllocator(936): alloc(492, 874, 1, 00000333, ...) failed -22 (Invalid argument)
02-28 23:33:38.052: E/(936): GraphicBufferAlloc::createGraphicBuffer(w=492, h=874) failed (Invalid argument), handle=0x0
02-28 23:33:38.052: E/BufferQueue(1274): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
02-28 23:33:38.052: W/WindowManager(1274): Screenshot failure taking screenshot for (492x874) to layer 21005
02-28 23:33:38.072: W/System.err(1915): java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
02-28 23:33:38.072: W/System.err(1915):   at java.lang.Class.classForName(Native Method)
02-28 23:33:38.072: W/System.err(1915):   at java.lang.Class.forName(Class.java:251)
02-28 23:33:38.072: W/System.err(1915):   at java.lang.Class.forName(Class.java:216)
02-28 23:33:38.072: W/System.err(1915):   at workshop.connector_mysql.MainActivity.onCreate(MainActivity.java:21)
02-28 23:33:38.072: W/System.err(1915):   at android.app.Activity.performCreate(Activity.java:5231)
02-28 23:33:38.072: W/System.err(1915):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-28 23:33:38.072: W/System.err(1915):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-28 23:33:38.072: W/System.err(1915):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-28 23:33:38.072: W/System.err(1915):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-28 23:33:38.072: W/System.err(1915):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-28 23:33:38.072: W/System.err(1915):   at android.os.Handler.dispatchMessage(Handler.java:102)
02-28 23:33:38.072: W/System.err(1915):   at android.os.Looper.loop(Looper.java:136)
02-28 23:33:38.072: W/System.err(1915):   at android.app.ActivityThread.main(ActivityThread.java:5017)
02-28 23:33:38.072: W/System.err(1915):   at java.lang.reflect.Method.invokeNative(Native Method)
02-28 23:33:38.072: W/System.err(1915):   at java.lang.reflect.Method.invoke(Method.java:515)
02-28 23:33:38.072: W/System.err(1915):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-28 23:33:38.072: W/System.err(1915):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-28 23:33:38.072: W/System.err(1915):   at dalvik.system.NativeStart.main(Native Method)
02-28 23:33:38.072: W/System.err(1915): Caused by: java.lang.NoClassDefFoundError: org/gjt/mm/mysql/Driver
02-28 23:33:38.072: W/System.err(1915):   ... 18 more
02-28 23:33:38.072: W/System.err(1915): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.gjt.mm.mysql.Driver" on path: DexPathList[[zip file "/data/app/workshop.connector_mysql-1.apk"],nativeLibraryDirectories=[/data/app-lib/workshop.connector_mysql-1, /system/lib]]
02-28 23:33:38.072: W/System.err(1915):   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-28 23:33:38.072: W/System.err(1915):   at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
02-28 23:33:38.072: W/System.err(1915):   at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
02-28 23:33:38.072: W/System.err(1915):   ... 18 more
02-28 23:33:38.072: I/System.out(1915): 沒發現Class
02-28 23:33:38.082: D/AndroidRuntime(1978): Shutting down VM
02-28 23:33:38.082: D/jdwp(1978): Got wake-up signal, bailing out of select
02-28 23:33:38.082: D/dalvikvm(1978): Debugger has detached; object registry had 1 entries
02-28 23:33:38.222: W/EGL_emulation(1915): eglSurfaceAttrib not implemented
02-28 23:33:38.572: I/ActivityManager(1274): Displayed workshop.connector_mysql/.MainActivity: +502ms


以下為logcat的System.out.println圖:


弘 edited on 2015-03-01 04:35
reply to postreply to post
作者 Re:各位大大!Android連結MySQL時,出現沒發現Class? [Re:弘]
roytsang





發文: 618
積分: 1
於 2015-03-03 03:02 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問你所謂"加入mysql-connector-java-5.1.34-bin.jar的驅動檔案"的意思是什麼?
有沒有在BUILD PATH中加入這個JAR file 呢?


reply to postreply to post
作者 Re:各位大大!Android連結MySQL時,出現沒發現Class? [Re:roytsang]






發文: 60
積分: 0
於 2015-03-03 12:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝roytsang大大的回覆!
"加入mysql-connector-java-5.1.34-bin.jar的驅動檔案"意思,是有在BUILD PATH中加入這個JAR file


reply to postreply to post
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8