JDBC Connector 1.0

Encontrei no site da Sun o JDBC Coonector 1.0 e pelo que eu entendi, é possível fazer conexão com banco de dados sem codificação, apenas utilizaríamos as classes do pacote JDBC Connector. Eu não tive tempo de ler direito. É isso mesmo? Alguém aqui sabe usar esse recurso? Aguardo respostas. :frowning:

Bom não sei se é exatamente isso, mas que eu saiba a API do JDBC implementa a interface para conexao com o banco de dados, ou seja as classes e seus metodos respectivos para conexao com o banco, e ai atraves dos drivers que cada fornecedor de BD distribui vc faria a conexao com o bando, no proprio site existe exemplos de conexao com o bd que pode ajudar.

Então encontro aqui como utilizar estas classes para fazer a conexão! No caso então a conexão é direta e não precisa de mais nada, somente indicar o BD que eu quero? No momento estou com uma conexão com o Access através de ODBC. Usando essa API posso eliminar essa necessidade? Aguardo repsosta. Francesco. :sunglasses:

No site vc vai achar bastante conteudo, mas vai um exemplo abaixo





Code:


Connection con;

Statement st;



String url, user, password, strDriver, query;



query="insert into Alunos (nome,cpf,idade,endereco,cidade,uf) values(´"+nome+"´,´"+cpf+"´,"+idade+",´"+end+"´,´"+cidade+"´,´"+uf +"´)";



user="";

password="";



url="jdbc:odbc:NomedoseuDSN";

strDriver = "sun.jdbc.odbc.JdbcOdbcDriver";



try

{

//registra driver

Class.forName( strDriver );



//acesso



con = DriverManager.getConnection( url, user, password );

st = con.createStatement();



st.executeUpdate( query );



st.close();

con.close();

}

catch( Exception e )

{

System.out.println( e.getMessage() );

}






[]´s



ah troque " por aspas duplas

Obrigado! Vou analisar o código. Um abraço!

[ Esta mensagem foi editada por: Francesco em 13-04-2003 11:44 ]

Estudando o JDBC Connector 1.0 vi que ele permite configurar o BD que se quer acessar. Pelo que entendi vc personaliza a classe para acessar seu BD. O documento que li está em inglês mas acho que existe uma interface para configurar a classe, a qual permite que vc escolha a classe e configure. O processo é o seguinte: descompactar o arquivo .zip, depois utilizar o "deployTool". Só que não estou conseguindo utilizar isso. Alguém pode me ajudar? Segue abaixo o tutorial anexo ao JDBC Connector 1.0 para quem quiser dar uma espiada:



*tutorial anexo baixo



GettingStarted with the JDBCTM Connector :



These step-by-step instructions tell you how to set up the JDBCTM Connector. Note that the instructions are given twice. The first set of instruction is given in a generic way that you need to adapt to your particular deployment tool. The second set applies specifically to using the deploytool utility that is part of the J2EE 1.3.1 Reference Implementation. Once you have finished the deployment tasks, you can run your JDBC applications as usual.



Here are the general steps to follow:



Start your J2EE server



Start the deployment tool for your server



OPEN and EDIT the appropriate JDBC Connector .rar file using the deploy tool



ADD the .jar file or files for your driver



EDIT some of the properties (usually labelled "Configuration Properties"). If your driver uses the DriverManager class to obtain a connection, skip to the last bullet item in this list.

ServerName - change "value" column so that it contains the IP address of the server where your database is; "localhost" is OK if the DB is on your local machine.

Username - change to your database user name

Password - change to your database password

ClassName - change to the class name for your driver; your driver vendor should supply this

If your driver uses the DriverManager class to get a connection and if there are any driver-specific properties to be set, edit the value of the DriverProperties config-property. (Otherwise, the value will be blank.) For example, to set the driver URL, you would put in the following: setURL###.

Example:

setURL#jdbc:oracle:thin:@localhost:1521:ORCL##.





SAVE the .rar file



DEPLOY the .rar file on your server



CREATE a new Connection Factory and Supply the JNDI name to use for a lookup of the DataSource object your application will use to get a connection to the database.

Do what is comparable on your deployment tool to the following:



Click on or select "Servers" and "localhost" (or your server name if it is not "localhost")



Create a new Connection Factory, usually by clicking a "Resource Adapters" tab or button and then clicking "New". You may see something labelled "New Connection Factory". You should see a place to type in a name of your choosing to identify your database.



Type in "jdbc/" followed by any unique name you choose for your database. Example: "jdbc/EmpDB"

This name will be registered with a JNDI naming service and can be used to retrieve the implementation that will create a connection to your database.



SAVE, click "OK", or whatever is comparable on your deployment tool

Here are the instructions for using the deploytool that is part of the J2EE Reference Implementation

This section details using one of the JDBC Connector RAR files with the J2EE Reference Implementation and connecting to a driver. This document is based on using J2EE 1.3.1; the JDBC Connector also works with J2EE 1.4.

Prerequisites

Before getting started, you need to need to have the following set up.

The JDBC Connector (downloaded from Sun´s JDBC site and unzipped in your local drive).

The J2EE Reference Implementation (1.3.1). If you do not already have it, download it from Sun´s J2EE web site and install it.

A driver based on JDBC technology ("JDBC driver") for the database you want to use. You will find a list of JDBC drivers on the JDBC drivers page.

Steps for Setting Up the JDBC Connector

Start the J2EE application server.

Start the J2EE deploytool. See http://java.sun.com/j2ee/sdk_1.3/techdocs/release/ReleaseNotes.html if you need more information.

OPEN the appropriate JDBC Connector .rar file using the deploy tool. This example shows using an _xa RAR file, which is appropriate if your driver uses an XADataSource implementation to get a connection and thus enables distributed transactions.

For J2EE 1.3.1, open spi_10_xa.rar

For J2EE 1.4, open spi_15_xa.rar



In the right pane of deploytool, click on the "RAR File" tab. Click EDIT. Add the .jar file or files for your JDBC driver. Your driver documentation should tell you what file(s) to use. In some cases, you might need to change the name of a file from .zip to .jar before adding it.

In the right pane, click on the "Configuration Properties" tab and edit some of the properties in the table.

Change the value of the property ServerName to the IP address of your database server.

Change the value of UserName and Password to your database user name and password.

Change the value of ClassName so that it is the class name of the XADataSource implementation for your driver.

SAVE the .rar file .

DEPLOY the .rar file from the deploytool.

In the left pane of deploytool, under the "Servers" tree, click on "localhost". Then, in the right pane, click on the "Resource Adapters" tab and and click "New".

Put in a suitable "ConnectionFactory JNDI Name". This name should be "jdbc/" followed by any unique name you want to use to identify your database. For example, you could use "jdbc/EmpDB".

You have successfully deployed your first JDBC Connector resource adapter. Now you are ready to run your database applications the same way you usually do.



Sample Code

Here is some sample code for retrieving the DataSource object that will create a connection to your database. This code fragment assumes that you entered "jdbc/EmpDB" as your ConnectionFactory JNDI name.



Context ctx = new InitialCcontext();

DataSource ds = (DataSource)ctx.lookup("jdbc/EmpDB");

Connection con = ds.getConnection();



PreparedStatement pstmt = con.prepareStatement(

"SELECT NAME, TITLE FROM EMPLOYEES WHERE DEPT = ?");

pstmt.setString(1, "SALES");

ResultSet rs = pstmt.executeQuery();

. . .//rest of your application :-o