Platforms to show: All Mac Windows Linux Cross-Platform

/Java/JavaDatabase/JavaDatabase MySQL test


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/JavaDatabase MySQL test

This example is the version from Sun, 17th Mar 2012.

Project "JavaDatabase MySQL test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() dim i as integer dim s as string for i=1 to 10000 InsertCounter=InsertCounter+1 s="INSERT INTO myTable(test_id, test_val,test_val2) VALUES("+str(InsertCounter)+",'HelloWorld','HelloWorld')" j.ExecuteSQL s next End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() dim r as JavaResultSetMBS // check second entry r=j.SelectSQL("SELECT * from myTable limit 10000") if r<>Nil then dim n,id as integer dim s as string while r.NextRecord n=n+1 id=r.getInt("test_id") s=r.getString("test_val") wend MsgBox str(n)+" records" end if End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() dim r as JavaResultSetMBS // check second entry r=j.SelectSQL("SELECT * from myTable") if r<>Nil then dim n,id as integer dim s as string while r.NextRecord id=r.getInt("test_id") s=r.getString("test_val") n=n+1 wend MsgBox str(n)+" records" end if r=nil End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action() d.vm.Runtime.gc End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() if d<>Nil then dim r as JavaRuntimeMBS = d.vm.Runtime StaticText1.text="RuntimeFreeMemory: "+str(r.FreeMemory)+EndOfLine+"RuntimeMaxMemory: "+str(r.MaxMemory)+EndOfLine+"RuntimeTotalMemory: "+str(r.TotalMemory) end if End EventHandler
End Control
EventHandler Sub Open() dim f as FolderItem = SpecialFolder.Desktop.Child("mysql-connector-java-5.1.6-bin.jar") if not f.Exists then MsgBox "Missing mysql connector classes!" Return end if if TargetLinux then // change path for your linux PC! JavaVMMBS.SetLibraryPath("/home/cs/jre1.6.0_05/lib/i386/client/libjvm.so") end if v=new JavaVMMBS(f) d=new JavaDatabaseMBS(v,"com.mysql.jdbc.Driver") j=d.getConnection("jdbc:mysql://localhost:3306","root","") if j<>Nil then MsgBox "connected" j.MyExecuteSQL "CREATE DATABASE JunkDB" j.MyExecuteSQL "USE JunkDB" j.MyExecuteSQL "DROP TABLE myTable" j.MyExecuteSQL "CREATE TABLE myTable(test_id int, test_val char(200) not null,test_val2 char(200) not null, PRIMARY KEY (test_id))" else MsgBox "not connected" end if Exception e as JavaExceptionMBS MsgBox e.message End EventHandler
Property InsertCounter As Integer
Property d As javadatabaseMBS
Property j As JavaConnectionMBS
Property v As javavmMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
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

See also:

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


The biggest plugin in space...