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 URL
			TimesTenDataSource ttds = new TimesTenDataSource();
			ttds.setUrl("jdbc:timesten:direct:dsn=my_ttdb;uid=root;pwd=password");
			
			// connect to the TimesTen database
			TimesTenConnection ttcon = (TimesTenConnection) ttds.getConnection();

			// create and execute the statement
			Statement stmt = ttcon.createStatement();
			ResultSet rset = stmt.executeQuery("select * from mytable");
			   
			// process the result set
			while(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/p/2778159.html