Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc01;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Principal {
- private static String URL = "jdbc:oracle:thin:@156.35.94.99:1521:DESA"; //(puertos:1521 o 5850)
- private static String USER = "UO232575"; //Identificador UO
- private static String PASS = "uo232575";
- private static String URL1 = "jdbc:hsqldb:hsql://localhost";
- private static String USER1 = "sa";
- private static String PASS1 = "";
- private static Connection con = null;
- private static Statement stmt = null;
- private static ResultSet rs = null;
- private static PreparedStatement ps = null;
- public static void main(String[] args){
- metodoStatementDESA();
- metodoPreparedDESA();
- metodoStatementHSQLDB(); //Tener cuidado con el getInt(), porque DESA pilla lo que le pones pero hsqldb no.
- metodoPreparedHSQLDB();
- }
- public static void metodoStatementDESA(){
- try{
- int a = 0;
- con = DriverManager.getConnection(URL, USER, PASS);
- stmt = con.createStatement();
- long t1 = System.currentTimeMillis();
- for(int i=0; i<1000; i++){
- String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =" + i;
- rs = stmt.executeQuery(consulta);
- while(rs.next()){
- a += rs.getInt("count(*)");
- // String s = rs.getString("b");
- // System.out.println(i + " -> " + s);
- }
- }
- long t2 = System.currentTimeMillis();
- long tf = t2-t1;
- System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (DESA + statement)");
- }catch(SQLException e){
- System.out.println("Error en la consulta");
- }finally{
- if(rs != null){
- try{
- rs.close();
- }catch(SQLException e){}
- }
- if(stmt != null){
- try{
- stmt.close();
- }catch(SQLException e){}
- }
- if(con != null){
- try{
- con.close();
- }catch(SQLException e){}
- }
- }
- }
- public static void metodoPreparedDESA(){
- try{
- int a = 0;
- con = DriverManager.getConnection(URL, USER, PASS);
- String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =?";
- ps = con.prepareStatement(consulta);
- long t1 = System.currentTimeMillis();
- for(int i=0; i<1000; i++){
- ps.setInt(1, i);
- rs = ps.executeQuery();
- while(rs.next()){
- a += rs.getInt("count(*)");
- // String s = rs.getString("b");
- // System.out.println(i + " -> " + s);
- }
- }
- long t2 = System.currentTimeMillis();
- long tf = t2-t1;
- System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (DESA + preparedStatement)");
- }catch(SQLException e){
- System.out.println("Error en la consulta");
- }finally{
- if(rs != null){
- try{
- rs.close();
- }catch(SQLException e){}
- }
- if(ps != null){
- try{
- ps.close();
- }catch(SQLException e){}
- }
- if(con != null){
- try{
- con.close();
- }catch(SQLException e){}
- }
- }
- }
- public static void metodoStatementHSQLDB(){
- try{
- int a = 0;
- con = DriverManager.getConnection(URL1, USER1, PASS1);
- stmt = con.createStatement();
- long t1 = System.currentTimeMillis();
- for(int i=0; i<1000; i++){
- String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =" + i;
- rs = stmt.executeQuery(consulta);
- while(rs.next()){
- a += rs.getInt(1);
- // String s = rs.getString("b");
- // System.out.println(i + " -> " + s);
- }
- }
- long t2 = System.currentTimeMillis();
- long tf = t2-t1;
- System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (HSQLDB + statement)");
- }catch(SQLException e){
- System.out.println("Error en la consulta");
- }finally{
- if(rs != null){
- try{
- rs.close();
- }catch(SQLException e){}
- }
- if(stmt != null){
- try{
- stmt.close();
- }catch(SQLException e){}
- }
- if(con != null){
- try{
- con.close();
- }catch(SQLException e){}
- }
- }
- }
- public static void metodoPreparedHSQLDB(){
- try{
- int a = 0;
- con = DriverManager.getConnection(URL1, USER1, PASS1);
- String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =?";
- ps = con.prepareStatement(consulta);
- long t1 = System.currentTimeMillis();
- for(int i=0; i<1000; i++){
- ps.setInt(1, i);
- rs = ps.executeQuery();
- while(rs.next()){
- a += rs.getInt(1);
- // String s = rs.getString("b");
- // System.out.println(i + " -> " + s);
- }
- }
- long t2 = System.currentTimeMillis();
- long tf = t2-t1;
- System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (HSQLDB + preparedStatement)");
- }catch(SQLException e){
- System.out.println("Error en la consulta");
- }finally{
- if(rs != null){
- try{
- rs.close();
- }catch(SQLException e){}
- }
- if(ps != null){
- try{
- ps.close();
- }catch(SQLException e){}
- }
- if(con != null){
- try{
- con.close();
- }catch(SQLException e){}
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment