Java 连接 timesten
所使用的環境是windows 7,timesten 是11g.
1. 設置dsn
在使用timesten之前需要先設置dsn,我在按照這里的步驟試完之后發現有些地方不一致,現將我的步驟介紹如下,一些內容是負責過來的,希望原作者能同意。
安裝好timesten后,打開控制面板-管理工具-ODBC 點擊“系統DNS”--“添加”找到TimesTen Data Manager 選中,點擊“完成”,彈出一個 “oracletimesten Client DSN Setup”, 在“DataStore”選項卡,在 “Data Source Name” 輸入框中輸入my_ttdb,?單擊“DataStore Path+name”后的Browse找到你想放數據的文件夾,輸入名字,點擊“打開”。
如選擇G:\TimesTen\my_ttdb\my_ttdb,數據文件實際上存放在G:\TimesTen\my_ttdb\
一定要先建立文件夾G:\TimesTen\my_ttdb\,否則后面創建的時候會報出錯誤
836:?Cannot?create?data?store?shared-memory?segment,?error?3
?
在”Transncation Log Directory“后的Browse,找到你想要放log的文件夾,點擊開啟。
我在使用中發現不設置日志文件存放位置,才能在下一步的ttisql my_ttdb命令中建立數據存儲文件。
在“Database Character Set” 下拉框選AL32UTF8,點擊ok完成dns配置
2. 配置
輸入“cmd”->輸入“ttisql my_ttdb”創建剛剛配置的dsn信息并連接my_ttdb。
創建用戶并授予權限“create user abc identified by password”
abc為用戶名,password為密碼
grant create session, create table to abc;
用新建的用戶登錄:connect "dsn=my_ttdb;uid=abc";
創建用例表:create table mytable(id number(4), title varchar2(10));
往表中插入測試數據:insert into mytable values (1, '12');
下面你就可以對此表進行操作了。
3. 在eclipse中寫java代碼連接timesten
連接代碼如下
import com.timesten.jdbc.TimesTenDataSource; import com.timesten.jdbc.TimesTenConnection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException;public class datawriter {public static void main(String[] args) {try{// create the TimesTen data source and set the connection URLTimesTenDataSource ttds = new TimesTenDataSource();ttds.setUrl("jdbc:timesten:direct:dsn=my_ttdb;uid=root;pwd=password");// connect to the TimesTen databaseTimesTenConnection ttcon = (TimesTenConnection) ttds.getConnection();// create and execute the statementStatement stmt = ttcon.createStatement();ResultSet rset = stmt.executeQuery("select * from mytable");// process the result setwhile(rset.next()) {System.out.println("id: " + rset.getInt(1) + ", title: " + rset.getString(2));}} catch(SQLException e) {e.printStackTrace();}} }
這一步是出現最多問題的,我弄了一個晚上才解決。首先是在timesten的安裝目錄\TimesTen\tt1122_32\lib\中找到ttjdbc5.jar,ttjdbc6.jar,ttjdbc7.jar包,然后根據使用的jdk版本加載對應的包。
接下來出現的第一個異常是:ttJdbcCS1122 in java.library.path,按照這里的方法試過之后依然不行,出現了新的異常:ttJdbcCS1122.dll: Can't find dependent libraries
然后改用直接加上這段代碼,來加入到java.library.path中
String libpath = System.getProperty("java.library.path");
libpath = "G:\\TimesTen\\tt1122_32\\bin;" + libpath;//timesten安裝路徑 System.setProperty("java.library.path", libpath);
libpath = System.getProperty("java.library.path");
不過還是不行,最后重新啟動eclipse,結果竟然可以了。后來發現,因為是安裝timesten之前就打開eclipse,導致環境變量沒加載,重啟eclipse就可以了。
?
在連接字符串中:jdbc:timesten:direct:dsn=my_ttdb;uid=root;pwd=password
如果使用jdbc:timesten:client:dsn=my_ttdb;uid=root;pwd=password
則無法連接,因為在第二步的配置中只建立系統dsn,如果要用client訪問需要設置client dsn,方法可參照這里。
步驟如下:
(1) 創建用戶DSN,選擇timesten client
(2) 輸入client DSN名,名字可以自定義,然后點擊servers配置服務器IP
注意,如果是client / server模式,且分別安裝的是不同版本的timesten,端口會不一樣。可以先在server端建用戶DSN查看端口,然后在client處修改為一樣。
或者在命令行下下輸入ttstatus命令查看
(3) ?選擇server dsn,并輸入user id 和密碼
做完上述步驟后,連接字符串改成:
?jdbc:timesten:client:dsn=client_ttdb;uid=root;pwd=password
?
轉載于:https://www.cnblogs.com/restran/archive/2012/11/19/2778159.html
總結
以上是生活随笔為你收集整理的Java 连接 timesten的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: APL开发日志--2012-11-18
- 下一篇: drop table中cascade的含