Advertisement
Guest User

ogffwqefwqpeofjhq

a guest
Oct 25th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. package jdbc01;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class Principal {
  11.  
  12. private static String URL = "jdbc:oracle:thin:@156.35.94.99:1521:DESA"; //(puertos:1521 o 5850)
  13. private static String USER = "UO232575"; //Identificador UO
  14. private static String PASS = "uo232575";
  15.  
  16. private static String URL1 = "jdbc:hsqldb:hsql://localhost";
  17. private static String USER1 = "sa";
  18. private static String PASS1 = "";
  19.  
  20. private static Connection con = null;
  21. private static Statement stmt = null;
  22. private static ResultSet rs = null;
  23. private static PreparedStatement ps = null;
  24.  
  25. public static void main(String[] args){
  26. metodoStatementDESA();
  27. metodoPreparedDESA();
  28. metodoStatementHSQLDB(); //Tener cuidado con el getInt(), porque DESA pilla lo que le pones pero hsqldb no.
  29. metodoPreparedHSQLDB();
  30. }
  31.  
  32.  
  33. public static void metodoStatementDESA(){
  34. try{
  35. int a = 0;
  36. con = DriverManager.getConnection(URL, USER, PASS);
  37. stmt = con.createStatement();
  38. long t1 = System.currentTimeMillis();
  39. for(int i=0; i<1000; i++){
  40. String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =" + i;
  41. rs = stmt.executeQuery(consulta);
  42. while(rs.next()){
  43. a += rs.getInt("count(*)");
  44. // String s = rs.getString("b");
  45. // System.out.println(i + " -> " + s);
  46. }
  47. }
  48. long t2 = System.currentTimeMillis();
  49. long tf = t2-t1;
  50. System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (DESA + statement)");
  51. }catch(SQLException e){
  52. System.out.println("Error en la consulta");
  53. }finally{
  54. if(rs != null){
  55. try{
  56. rs.close();
  57. }catch(SQLException e){}
  58. }
  59. if(stmt != null){
  60. try{
  61. stmt.close();
  62. }catch(SQLException e){}
  63. }
  64. if(con != null){
  65. try{
  66. con.close();
  67. }catch(SQLException e){}
  68. }
  69. }
  70. }
  71.  
  72. public static void metodoPreparedDESA(){
  73. try{
  74. int a = 0;
  75. con = DriverManager.getConnection(URL, USER, PASS);
  76. String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =?";
  77. ps = con.prepareStatement(consulta);
  78. long t1 = System.currentTimeMillis();
  79. for(int i=0; i<1000; i++){
  80. ps.setInt(1, i);
  81. rs = ps.executeQuery();
  82. while(rs.next()){
  83. a += rs.getInt("count(*)");
  84. // String s = rs.getString("b");
  85. // System.out.println(i + " -> " + s);
  86. }
  87. }
  88. long t2 = System.currentTimeMillis();
  89. long tf = t2-t1;
  90. System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (DESA + preparedStatement)");
  91. }catch(SQLException e){
  92. System.out.println("Error en la consulta");
  93. }finally{
  94. if(rs != null){
  95. try{
  96. rs.close();
  97. }catch(SQLException e){}
  98. }
  99. if(ps != null){
  100. try{
  101. ps.close();
  102. }catch(SQLException e){}
  103. }
  104. if(con != null){
  105. try{
  106. con.close();
  107. }catch(SQLException e){}
  108. }
  109. }
  110. }
  111.  
  112. public static void metodoStatementHSQLDB(){
  113. try{
  114. int a = 0;
  115. con = DriverManager.getConnection(URL1, USER1, PASS1);
  116. stmt = con.createStatement();
  117. long t1 = System.currentTimeMillis();
  118. for(int i=0; i<1000; i++){
  119. String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =" + i;
  120. rs = stmt.executeQuery(consulta);
  121. while(rs.next()){
  122. a += rs.getInt(1);
  123. // String s = rs.getString("b");
  124. // System.out.println(i + " -> " + s);
  125. }
  126. }
  127. long t2 = System.currentTimeMillis();
  128. long tf = t2-t1;
  129. System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (HSQLDB + statement)");
  130. }catch(SQLException e){
  131. System.out.println("Error en la consulta");
  132. }finally{
  133. if(rs != null){
  134. try{
  135. rs.close();
  136. }catch(SQLException e){}
  137. }
  138. if(stmt != null){
  139. try{
  140. stmt.close();
  141. }catch(SQLException e){}
  142. }
  143. if(con != null){
  144. try{
  145. con.close();
  146. }catch(SQLException e){}
  147. }
  148. }
  149. }
  150.  
  151. public static void metodoPreparedHSQLDB(){
  152. try{
  153. int a = 0;
  154. con = DriverManager.getConnection(URL1, USER1, PASS1);
  155. String consulta = "SELECT count(*) FROM tvehiculos WHERE ID =?";
  156. ps = con.prepareStatement(consulta);
  157. long t1 = System.currentTimeMillis();
  158. for(int i=0; i<1000; i++){
  159. ps.setInt(1, i);
  160. rs = ps.executeQuery();
  161. while(rs.next()){
  162. a += rs.getInt(1);
  163. // String s = rs.getString("b");
  164. // System.out.println(i + " -> " + s);
  165. }
  166. }
  167. long t2 = System.currentTimeMillis();
  168. long tf = t2-t1;
  169. System.out.println("Coinciden " + a + " vehiculos con el numero de iteracion en " + tf + " ms (HSQLDB + preparedStatement)");
  170. }catch(SQLException e){
  171. System.out.println("Error en la consulta");
  172. }finally{
  173. if(rs != null){
  174. try{
  175. rs.close();
  176. }catch(SQLException e){}
  177. }
  178. if(ps != null){
  179. try{
  180. ps.close();
  181. }catch(SQLException e){}
  182. }
  183. if(con != null){
  184. try{
  185. con.close();
  186. }catch(SQLException e){}
  187. }
  188. }
  189. }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement