Platforms to show: All Mac Windows Linux Cross-Platform

/Java/JavaDatabase/java Database Thread Tests


Required plugins for this example: MBS Java Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Java/JavaDatabase/java Database Thread Tests

This example is the version from Thu, 6th Apr 2016.

Project "java Database Thread Tests.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Close() if SQLiteDB<>Nil then SQLiteDB.Close end if if j<>Nil then j.close end if End EventHandler
EventHandler Sub Open() dim f as FolderItem = FindFile("sqlitejdbc-v056.jar") if not f.Exists then MsgBox "Missing sqlite connector classes!" Quit end if dim file as FolderItem = FindFile("SQLite.rsd") v = new JavaVMMBS(f) d = new JavaDatabaseMBS(v,"org.sqlite.JDBC") dim path as string #if RBVersion < 2013 then path = file.UnixpathMBS #else path = file.NativePath #endif j = d.getConnection("jdbc:sqlite:"+path) if j = Nil then MsgBox "Cannot connect to sqlite Database" Quit end if SQLiteDB = New REALSQLDatabase // or SQLiteDatabase SQLiteDB.databaseFile = file if SQLiteDB.databaseFile.Exists then if Not SQLiteDB.Connect then MsgBox "Cannot connect to SQLite Database" Quit end if else MsgBox "Missing Database File!" Quit end Exception e as JavaExceptionMBS MsgBox e.message+" errorcode: "+str(e.ErrorNumber) End EventHandler
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend End Function
Property SQLiteDB As REALSQLDatabase
Property d As JavaDatabaseMBS
Property j As JavaConnectionMBS
Property v As JavaVMMBS
End Class
Class Window1 Inherits Window
Control Thread1 Inherits Thread
ControlInstance Thread1 Inherits Thread
EventHandler Sub Run() Dim rs As JavaResultSetMBS rs = App.j.MySelectSQL("SELECT * FROM master") if rs <> NIL Then if rs.NextRecord Then data = rs.getString("str1") + " , " + rs.getString("str2") + " , " + rs.getString("str3") + " , " + rs.getString("str4") end if end if Timer1.Mode = 1 Timer1.Enabled = True app.j.vm.FreeCurrentThread End EventHandler
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() Thread1.Run End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() Thread2.Run End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() quit End EventHandler
End Control
Control Thread2 Inherits Thread
ControlInstance Thread2 Inherits Thread
EventHandler Sub Run() Dim rs As RecordSet rs = App.SQLiteDB.SQLSelect("SELECT * FROM master") If App.SQLiteDB.Error Then data = "DB Error : " + App.SQLiteDB.ErrorMessage else if rs <> Nil Then if Not rs.EOF Then data = rs.Field("str1").StringValue + " , " + rs.Field("str2").StringValue + " , " + rs.Field("str3").StringValue + " , " + rs.Field("str4").StringValue end if rs.Close end if end if Timer1.Mode = 1 Timer1.Enabled = True End EventHandler
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() TextField1.Text = data End EventHandler
End Control
Control TextField1 Inherits TextField
ControlInstance TextField1 Inherits TextField
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action() Thread3.Run End EventHandler
End Control
Control Thread3 Inherits Thread
ControlInstance Thread3 Inherits Thread
EventHandler Sub Run() Dim rs As JavaResultSetMBS Dim blob As JavaBlobMBS rs = App.j.MySelectSQL("SELECT * FROM master") if rs <> NIL Then if rs.NextRecord Then blob = rs.getBlob("blb1") data = blob.getBytes(0, blob.length) end if end if Timer1.Mode = 1 Timer1.Enabled = True app.j.vm.FreeCurrentThread End EventHandler
End Control
Property data As String
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Module JavaUtil
Sub ExecuteSQL(extends c as JavaConnectionMBS, sql as string) try dim s as JavaStatementMBS s=c.createStatement if s<>nil then call s.executeUpdate sql end if catch d as JavaExceptionMBS MsgBox d.message+" ErrorCode: "+str(d.errornumber) end try End Sub
Sub MyExecuteSQL(extends j as javaconnectionMBS, sql as string) try j.ExecuteSQL sql catch d as JavaExceptionMBS MsgBox d.message+" ErrorCode: "+str(d.errornumber) end try End Sub
Sub MyExecuteSQLwithPreparedStatement(extends j as javaconnectionMBS, sql as string) try dim p as JavaPreparedStatementMBS p=j.prepareStatement(sql) if p<>Nil then call p.execute end if catch d as JavaExceptionMBS MsgBox d.message+" ErrorCode: "+str(d.errornumber) end try End Sub
Function MySelectSQL(extends j as javaconnectionMBS, sql as string, editable as boolean=false) As JavaResultSetMBS try return j.SelectSQL(sql,editable) catch d as JavaExceptionMBS MsgBox d.message end try End Function
Function MySelectSQLwithPreparedStatement(extends j as javaconnectionMBS, sql as string, editable as boolean=false) As JavaResultSetMBS try dim p as JavaPreparedStatementMBS p=j.prepareStatement(sql) if p<>Nil then dim r as JavaResultSetMBS r=p.executeQuery r.Tag=p // keep a reference to the statement Return r end if catch d as JavaExceptionMBS MsgBox d.message end try End Function
Function SelectSQL(extends c as JavaConnectionMBS, sql as string, editable as boolean=false) As JavaResultSetMBS try dim mode as integer = c.CONCUR_READ_ONLY dim s as JavaStatementMBS s=c.createStatement(c.TYPE_FORWARD_ONLY, mode) if s<>nil then dim r as JavaResultSetMbs r=s.executeQuery(sql) if r<>Nil then // you need to keep the statement with the r.Tag=s Return r end if end if catch d as JavaExceptionMBS MsgBox d.message+" ErrorCode: "+str(d.errornumber) end try End Function
End Module
End Project

The items on this page are in the following plugins: MBS Java Plugin.


The biggest plugin in space...