en otra ventana

Anuncio
Apéndice D: Código Fuente del Programa ObjetoLargo.java
Código Fuente del Programa ObjetoLargo.java
import java.sql.*;
import java.io.*;
import java.*;
import java.util.*;
import java.net.*;
import org.postgresql.largeobject.*;
public class ObjetoLargo
{
public void connection()
{
Connection connection,conn = null;
try
{
/*Conexion a la base de datos de Mysql*/
Class.forName("com.mysql.jdbc.Driver");
String serverName = "localhost";
String url="jdbc:mysql://localhost:3306/claudiadb";
String username = "root";
String password = "saviaga";
connection = DriverManager.getConnection(url, username, password);
/*Conexion a la base de datos de Postgres*/
String pgsqlURL = "jdbc:postgresql://localhost:5432/isystemDB";
String pgsqlUser = "test";
String pgsqlPW = "test";
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(pgsqlURL, pgsqlUser, pgsqlPW);
// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj2 = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();
/*Obtenemos los datos de tabla*/
PreparedStatement psx = conn.prepareStatement("SELECT * FROM word_sound_default");
ResultSet rs =psx.executeQuery();
if (rs != null)
{
while (rs.next())
{
/*Obtenemos el OID guardado en la tabla*/
int oid = rs.getInt(2);
/*Obtenemos un tipo de dato LargeObject con los datos correspondientes al OID obtenido*/
LargeObject obj = lobj2.open(oid, LargeObjectManager.READ);
/*Leemos los datos*/
byte buf2[] = new byte[obj.size()];
obj.read(buf2, 0, obj.size());
/*Insertamos los datos en la tabla correspondiente de Mysql*/
PreparedStatement ps= connection.prepareStatement("INSERT INTO word_sound_default
VALUES (?,?,?,?)");
String nombre = rs.getString(1);
ps.setString(1,nombre );
ps.setBytes(3,null);
ps.setBytes(2,buf2);
ps.setInt(4,0);
ps.execute();
ps.clearParameters();
obj.close();
ps.close();
}
rs.close();
}
psx.close();
conn.close();
connection.close();
}//fin del try
catch (ClassNotFoundException cnfe)
{
System.out.println("Could not load database driver:" + cnfe.getMessage());
}
catch (SQLException sqle)
{
System.out.println("Could not connect to the database: " + sqle.getMessage());
}
catch (ClassCastException cle)
{
System.out.println("Excepcion " + cle);
}
}//end connection()
public static void main(String args[])
{
new ObjetoLargo().connection();
}
}//class
Descargar