How do i Implement Sybdatasource interface

Can i get the example of Sybdatasource implementation. Specially how to set up the basic-statndard connection using datasource.
siddharthkoolAsked:
Who is Participating?
 
for_yanCommented:

I am trying to understand what you mean.

If your ultimate goal is to connect to Sybase database and get data from it or write/update
data in that database, then you can read my description below.
If your goal is something else, then please, explain again what you want.

You can connect to database using the JDBC driver developed for Sybase.
Such driver, for Sybase it is called jConnect, contains in particular classes which implement
DataSource interface. Say, jConnect for JDBC (to use it you need to have some jar, say jconn2.jar or jconn3.jar
hin your classpath) has class called

com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource

which implements DataSource interface.
It may have some other implementations of DataSource.

If you need to connect to database, you do not need
to implement datasource yourself, you need to have jconn2.jar or latter jar
in your cllasspath and you need to use
methods of DataSource implemntation
which already exists in the driver.
And most importnat method of DataSource interface is getConnection()

So you say:


                  // Load the jdbc driver.
                  Class drvClass = Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
                  DriverManager.registerDriver( (Driver) drvClass.newInstance());

                  DriverManager.setLoginTimeout(900);            //15 minute timeout

                  // Attempt to connect to a driver.
                  conn = DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638?ServiceName=storage",
 "Test", "Dummy");


(you of course need to use your parameters in similar call which will connect you to your instance with your user and password
info)

Then:

Statement stmt = db.createStatement();

and then:

ResultSet rs = stmt.executeQuery("select * from table");

(see full code below and in the link I posted before)

In this way you can access database.

In order to access database you don't need to implement DataSource yourself
because some people who know all the details
of Sybase table structure and way they store the data, etc wrote the low level
code  which allows to access their tables, etc.

If you are not privy to proprietary
information on how these tables a physically organized, you would not be
able to write  implementation of DataSource yourself,
but luckily you don't need to write it, as
some Sybase folks already wrote it for you.

This code can be downloaded from the same link I posted above:


import java.util.*;
import java.sql.*;


class test
{


	public static void printSQLException(SQLException e) 
	{
		e.printStackTrace();
		while(e != null) {
			System.out.println(e);
			e = e.getNextException();
		}
	}

	public static Connection openDatabase(String strDriver, String strURL, String strUserId, String strUserPswd)
	{
		Connection conn;

		try	{
			// Load the jdbc driver.
			Class drvClass = Class.forName(strDriver);
			DriverManager.registerDriver( (Driver) drvClass.newInstance());

			DriverManager.setLoginTimeout(900);		//15 minute timeout

			// Attempt to connect to a driver.
			conn = DriverManager.getConnection(strURL, strUserId, strUserPswd);
		}
		catch (SQLException e) {
			printSQLException(e);
			return null;
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace(); // for debugging
			return null;
		}
		catch (InstantiationException e) {
			e.printStackTrace();
			return null;
		}
		catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}

		return conn;
	}


	public static boolean closeDatabase(Connection conn)
	{
		if(conn == null)
			return true;
		try {
			conn.close();
		}
		catch (SQLException e) {
			return false;
		}
		return true;
	}



public static void access(Connection db, int msg_id, int no_msgs)
{

		Statement get_stmt;
		ResultSet get_rs;

		Statement update_stmt;
		ResultSet update_rs;
	
		int[] inst_id_ar = new int[50];
		int[] obj_type_ar = new int[50];
		int counter
		

		try
		{
			get_stmt = db.createStatement();
			update_stmt = db.createStatement();
			
			
			get_rs = get_stmt.executeQuery("SELECT * from test_table ");

			while(get_rs.next()) 
			{
				System.out.println(dummy_rs.getInt("Instr_ID"));
				System.out.println(dummy_rs.getString("Obj_Type"));
			
	
			} // while end
			
			update_stmt.executeUpdate("insert into test_table values ( 'Success', 1 )");


		} // end try 
		catch(SQLException e) 
		{
			printSQLException(e);
			return;
		}
	
		 
	 }

	 public static void main(String args[]) throws Exception
	{
		Connection db = openDatabase("com.sybase.jdbc3.jdbc.SybDriver", "jdbc:sybase:Tds:localhost:2638?ServiceName=storage", "Test", "Dummy");

		
		access(db);

		closeDatabase(db);
	}

}

Open in new window









0
 
for_yanCommented:
Looks like a detailed explanation of how to connect to Sybase:

http://www.sumedh.info/articles/java-jdbc-sybase.php
0
 
for_yanCommented:
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
siddharthkoolAuthor Commented:
@for yan: i want to implement a SybDataSource.java class using the methods given in the SybdataSource interface. I want to to do only the basic connection to the database no pooling or xa.
plz help.
0
 
for_yanCommented:
Hmm.. I would think that such class would already exist in the Sybase JDBC driver and it
would implement java.sql.DataSource. Unless you are writing a driver, as far as I understand,you would not need
to implement the DaaSource clas, you'd rather use those classes, which should exist in Sybase
JDBC driver.

I'm not so much familiar with Sybase, but this is how it workd with Oracle, I guess
the general paradigm is thesame for any database.

Is your goal to connect to database and use/update/insert/manipulate with  data in database ?
That's waht I though based on the second part of your question.
Or is it something else?

0
 
siddharthkoolAuthor Commented:
Yes exactly i want to just see whether i can connect to database using datasource. i want to implement a SybDataSource.java in order to manipulate the data. Yes i have seen the .class file but i want to write the implementation code for SybDataSource. I would like to have source code snippet for basic connection using DataSource object for sybase. or how can i implement it??
0
 
siddharthkoolAuthor Commented:
solved
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.