16 May 2008

OpenOffice Baseを通さず、直接MySQLサーバに接続

OpenOffice BASICマクロを用いて、Baseを通さずに直接MySQLに接続する方法。
(JDBCドライバを用いる場合の説明)


Dim DatabaseContext as object
Dim DataSource as Object
Dim Connection as Object
Dim Statement as Object
Dim sSQL as String

Dim oResultSet as Object
Dim nDlgResult As Integer

Dim sURL as String
Dim oProps(2) as new com.sun.star.beans.PropertyValue

' ***** データベースのコネクション  *****
DatabaseContext=createUnoService("com.sun.star.sdbc.DriverManager")

sURL = "jdbc:mysql://192.168.1.115:3306/test?useUnicode=true&characterEncoding=UTF-8"
oProps(0).Name = "user"
oProps(0).value = "testuser"
oProps(1).Name = "password"
oProps(1).value = "test"
oProps(2).name = "JavaDriverClass"
oProps(2).value = "com.mysql.jdbc.Driver"

Connection = DatabaseContext.getConnectionWithInfo(sURL, oProps())

' ***** SQLの実行  *****
Statement = Connection.createStatement()

nResult = Statement.executeUpdate("INSERT INTO TBL_TEST (col1, col2) VALUES ('string', 'string')")

oResultSet = Statement.executeQuery("SELECT * FROM TBL_TEST")

' ***** データベースを閉じる  *****
Statement.Close()
Connection.Close()
Connection.Dispose()