---
title: "Timesten 学习笔记（六）Java"
author: "Intrepid2012"
date: "2012-05-17T22:25:27Z"
tags: []
categories: ["数据库"]
slug: "timesten-java"
draft: false
lastmod: "2012-05-17T22:25:27Z"
id: 1534299
url: "https://intrepid2012.iteye.com/blog/1534299"
---

连接timesten 数据库，在Java下有两种方法：

 

第一种，使用 Drivermanager

 

```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class TTTest {

	public static void main(String[] args) {
      // create the TimesTen data source and set the connection URL
    	String URL = "jdbc:timesten:direct:dsn=my_ttdb;UID=oracle;PWD=oracle";
		Connection conn = null;

		try {
		     Class.forName("com.timesten.jdbc.TimesTenDriver");
		} catch (ClassNotFoundException ex) {
		      // See "Handling errors"
		}

		try {
		    // Open a connection to TimesTen
		    conn = DriverManager.getConnection(URL);
		    System.out.println("success");
 
      // create and execute the statement
      Statement stmt = conn.createStatement();
      ResultSet rset = stmt.executeQuery("select count(*) from cmn_tn2lrn201");
 
      // process the result set
      while(rset.next()) {
        System.out.println("Value: " + rset.getInt(1));
      }
    } catch(SQLException e) {
      e.printStackTrace();
    }
}
	}
```

 

 

第二种 使用Datasource

 

```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class TTTest {

	public static void main(String[] args) {
       
        TimesTenDataSource ds = new TimesTenDataSource();
         ds.setUser(myttusername);                    // User name to log in to TimesTen.
         ds.setPassword(myttpwd);                     // Password to log in to TimesTen.
         ds.setUrl("jdbc:timesten:direct:<dsn>");
         ds.setOraclePassword(myorapwd);              // Password to log in to Oracle.
         Connection conn = ds.getConnection();

 
      // create and execute the statement
      Statement stmt = conn.createStatement();
      ResultSet rset = stmt.executeQuery("select count(*) from cmn_tn2lrn201");
 
      // process the result set
      while(rset.next()) {
        System.out.println("Value: " + rset.getInt(1));
      }
    } catch(SQLException e) {
      e.printStackTrace();
    }
}
	}
```