Classified in Other subjects

Written at on English with a size of 4.12 KB.


Types of controllers.
There are four bridges different driver or JDBC driver JDBC specification defined by Sun.
· Bridge 1. Drivers using the system such as a gateway or bridge. One example is the JDBC-ODBC. Not the best solution because in many cases it is necessary to install specific software on the client besides being slow access to the database.
· Bridge 2. This type of controller is called native API, that is, the driver contains Java code in which calls are made to the native methods of the database in C or C + + made in access to the database. Sometimes it is necessary to install client software to use this type of controllers.
· Bridge 3. JDBC drivers or drivers of this type are reported with an intermediate application server using sockets that move the client program requests a driver specific API. This type of driver has the advantage of not using any software on the client.
· Bridge 4. Drivers or driver of this type use network protocols that are included in the DBMS (Database Management System Data), and therefore the drivers communicate directly with the database, also using Java sockets. It is the best because this 4drivers written entirely in Java. Most of these drivers are provided by the manufacturer of your DBMS.
Developing applications with the JDBC API.
One advantage of using a JDBC API is that all functions are the same, regardless of the database to use, so you schedule a time for any database handler, and can make an application for MySQL and then bring to a production server with a database engine more powerful, like SQL Server or Postgres.
Load the JDBC driver.
The first step is to connect our Java program to the server that is located in our database. On many occasions it will require a username and password to access the database.
When using Tomcat, to carry out the connection to the database, we need the JDBC driver, which can be a file. Jar or. Class, is included in the CLASSPATH of the environment in which they will use. In this case the file must be copied to the lib folder within the folder it is installed JSPs interpreter. see figure, we have the MySQL JDBC API in the file mysql-connector-java-3.0.9-stable-bin.jar.
Once you create a connection to the database, we create sentences or run queries from our Java program to interact with the database. We can create queries that modify the database without returning any results, p. eg INSERT, ALTER, DELETE, CREATE, DROP, or UPDATE.
The code in question should present a structure like this, see example:
Statement stmt;
stmt = con.createStatement ();
stmt.executeUpdate ( "INSERT INTO interest (interes_nombre, interes_url," + "interes_tipo) VALUES ( '" + args [0] + "', '" + args [1] + "', '" + args [2] + "')");

The method createStatement () creates an open channel through which to run queries.
Run a query with the Statement object-method executeUpdate ()
The method executeUpdate () is who really run the query in the database, while the close () method frees the resources allocated to the connection open.
The following example inserts a table named tuplesinterest belonging to the league database.
Run a query with the Statement object - the method executeQuery ()
Retrieval queries tuples or records are run with the method executeQuery (). This method returns a ResultSet object, which can be used to access each of the returned records:
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

Entradas relacionadas: