ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3463|ตอบกลับ: 0

[B4A] การเชื่อมต่อฐานข้อมูล MySQL ทั้งแบบ Local/Remote ผ่านทางการใช้งาน JDBC

[คัดลอกลิงก์]

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5971




สำหรับบทความชุดนี้เป็นการเชื่อมต่อฐานข้อมูล MySQL ทั้งแบบ Local/Remote ผ่านทาง JDBC เป็นการติดต่อโดยตรงเข้าสู่ฐานข้อมูลครับผม ...

ดาวน์โหลด B4A เพื่อติดตั้งใช้งานฟรี ...

ดาวน์โหลดโค้ดต้นฉบับสำหรับบทความชุดนี้ ...

ไฟล์ Libraries จะอยู่ใน Zip File เมื่อแตกออกมาให้คัดลอกไฟล์ทั้งหมดไปเก็บไว้ที่ C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries ...


ไฟล์ Libraries ที่จำเป็นต้องใช้งาน ...


ใน phpMyAdmin สำหรับการ Import ไฟล์ SQL ตัวอย่างเข้ามา ...



ตารางข้อมูลตัวอย่าง ...


มาดูโค้ดกันเถอะ ... Starter.bas
  1. #Region  Service Attributes
  2.         #StartAtBoot: False
  3.         #ExcludeFromLibrary: True
  4. #End Region

  5. Sub Process_Globals
  6.         'These global variables will be declared once when the application starts.
  7.         'These variables can be accessed from all modules.
  8.         '// Remote MySQL Server
  9.         Public mysql As JdbcSQL
  10.         Public driver As String = "com.mysql.jdbc.Driver"
  11.         '// Link to Remote MySQL/Table Name
  12.         Public jdbcUrl As String = "jdbc:mysql://www.remotemysql.com/PgseQEAkdl?useSSL=false"
  13.         Public Username As String = "USERNAME"
  14.         Public Password As String = "PASSWORD"
  15.         '// Localhost
  16.         'Private jdbcUrl As String = "jdbc:mysql://192.168.1.11:3306/contact"
  17.         'Private Username As String = "USERNAME"
  18.         'Private Password As String = "PASSWORD"
  19. End Sub

  20. Sub Connect As ResumableSub
  21.         mysql.InitializeAsync("mysql", driver, jdbcUrl, Username, Password)
  22.         Wait For MySQL_Ready (Success As Boolean)
  23.         If Success = False Then
  24.                 Log("Check unfiltered logs for JDBC errors.")
  25.         End If
  26.         Return Success
  27. End Sub

  28. Sub CloseConnection
  29.         mysql.Close
  30. End Sub

  31. Sub DisableStrictMode
  32.         Dim jo As JavaObject
  33.         jo.InitializeStatic("android.os.Build.VERSION")
  34.         If jo.GetField("SDK_INT") > 9 Then
  35.                 Dim policy As JavaObject
  36.                 policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
  37.                 policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
  38.                 Dim sm As JavaObject
  39.                 sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
  40.         End If
  41. End Sub

  42. Sub Service_Create
  43.         'This is the program entry point.
  44.         'This is a good place to load resources that are not specific to a single activity.
  45.         'need to disable it as reading from large JdbcResultSet will cause network requests to be sent on the main thread.
  46.         DisableStrictMode
  47. End Sub

  48. Sub Service_Start (StartingIntent As Intent)
  49.         Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
  50. End Sub

  51. Sub Service_TaskRemoved
  52.         'This event will be raised when the user removes the app from the recent apps list.
  53. End Sub

  54. 'Return true to allow the OS default exceptions handler to handle the uncaught exception.
  55. Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
  56.         Return True
  57. End Sub

  58. Sub Service_Destroy

  59. End Sub
คัดลอกไปที่คลิปบอร์ด


โค้ดในส่วนของ Main ...
  1. #Region  Project Attributes
  2.         #ApplicationLabel: Connect Remote MySQL
  3.         #VersionCode: 1
  4.         #VersionName:
  5.         'SupportedOrientations possible values: unspecified, landscape or portrait.
  6.         #SupportedOrientations: unspecified
  7.         #CanInstallToExternalStorage: False
  8. #End Region

  9. #Region  Activity Attributes
  10.         #FullScreen: False
  11.         #IncludeTitle: True
  12.     'MySQL Connector/J Driver
  13.     #AdditionalJar: mysql-connector-java-5.1.47-bin.jar
  14.         '#AdditionalJar: mysql-connector-java-5.1.34-bin
  15.         '#AdditionalJar: mysql-connector-java-5.1.49-bin.jar
  16.         #BridgeLogger: true
  17. #End Region

  18. '// https://www.b4x.com/android/help/jdbcsql.html
  19. Sub Process_Globals
  20.         'These global variables will be declared once when the application starts.
  21.         'These variables can be accessed from all modules.
  22. End Sub

  23. Sub Globals
  24.         'These global variables will be redeclared each time the activity is created.
  25.         'These variables can only be accessed from this module.
  26.         Private Kanit As CustomFonts
  27.         Private ListView1 As ListView
  28.         Private ProgressBar1 As ProgressBar
  29. End Sub


  30. Sub Activity_Create(FirstTime As Boolean)
  31.         Activity.LoadLayout("main")
  32.         Activity.Title="Connect Remote MySQL [JDBC]"
  33.         '// Set Own Font.
  34.         Kanit.Initialize("Kanit-Regular.ttf")
  35.         Kanit.SetCustomFontsToAllViews(Activity)
  36.         '// Initialized.
  37.         ProgressBar1.Visible = True
  38.         Wait For (Starter.Connect) Complete (Success As Boolean)
  39.         'Log(Success)
  40.         If Success Then
  41.                 Dim jsql As JdbcResultSet
  42.                 Starter.mysql.Initialize2(Starter.driver, Starter.jdbcUrl, Starter.Username, Starter.Password)
  43.                 '/ Localhost Query
  44.                 '/jsql=Starter.mysql.ExecQuery("SELECT ContactPK, ContactID, FullName FROM contact.tblcontact ")
  45.                 jsql = Starter.mysql.ExecQuery("SELECT ContactPK, ContactID, FullName FROM tblcontact ORDER BY ContactID")
  46.                 Do While jsql.NextRow
  47.                         'Log(jsql.GetString("ContactID"))
  48.                         ListView1.AddTwoLines2(jsql.GetString("ContactID"), jsql.GetString("Fullname"), jsql.GetString("ContactPK"))
  49.                 Loop
  50.                 Starter.CloseConnection
  51.         End If
  52.         ProgressBar1.Visible = False
  53. End Sub
  54.        
  55. Sub Activity_Resume

  56. End Sub

  57. Sub Activity_Pause (UserClosed As Boolean)

  58. End Sub
คัดลอกไปที่คลิปบอร์ด


สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-3-28 18:07 , Processed in 0.215183 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้