Advertisement
Guest User

Untitled

a guest
May 17th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package cliento;
  7.  
  8.  
  9. import java.sql.*;
  10.  
  11. /**
  12. *
  13. * @author mat.aules
  14. */
  15. public class Cliento {
  16.  
  17. /**
  18. * @param args the command line arguments
  19. */
  20. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  21. String driver ="org.postgresql.Driver"; // Nombre de la classe que hará de conector con BD
  22. String url = "jdbc:postgresql://127.0.0.1/booktown"; // URL de la BD.
  23. String user = "postgres"; // Nombre del usuario que accede a la base de datos
  24. String pwd = "db1"; //password del usuario que accede a la BD
  25. try {
  26. Class.forName(driver); // Intentamos cargar el conector
  27. }catch (ClassNotFoundException e) {
  28. System.err.println("Can't load driver "+ e.getMessage());
  29. }
  30. try { // Intentamos establecer la conexión con la base de dades usando el connector
  31. Connection con = DriverManager.getConnection(url, user, pwd);
  32. System.err.println("Conection OK");
  33. //acces(con);
  34. consDinamica(con);
  35. con.close(); // Tanquem la connexió
  36. }catch(Exception e) {
  37. System.err.println("Connection Attempt failed");
  38. System.err.println(e.getMessage());
  39. }
  40. }
  41.  
  42. static private void acces(Connection con){
  43. String query;
  44.  
  45. query = "select * from books";
  46. ResultSet resul = execQuery(con, query);
  47. printResults(resul);
  48.  
  49. }
  50.  
  51. static private void consDinamica(Connection con){
  52. String query = "Select title from books where id=? ;" ;
  53. String entrada = "190";
  54.  
  55. ResultSet resul = execQueryd(con, query, entrada);
  56. printResults(resul);
  57.  
  58. }
  59.  
  60. static ResultSet execQuery( Connection con, String query ){
  61. try{
  62. // A partir de la conexión con la base de datos creamos un objeto Statment
  63. Statement stmt = con.createStatement();
  64. System.out.println( query );
  65. return( stmt.executeQuery( query )); // el resultado de la consulta
  66. }catch( SQLException e) {
  67. System.err.println( "Query failed - " + e.getMessage()); return( null );
  68. }
  69. }
  70.  
  71. static void printResults( ResultSet res ) {
  72. System.out.println( " Columna1 | Columna2" );
  73. System.out.println( "---------+------------------------------" );
  74. try{
  75. // Recorrem tot el resultSet per files mentre n'hi hagi
  76. while( res.next()) {
  77. System.out.print( res.getString( 1 ));
  78. System.out.print( " | ");
  79. System.out.print( res.getString( 2 ));
  80. System.out.println( "" );
  81. }
  82. }catch( SQLException e ) {
  83. System.err.println( "Fetch failed: " + e.getMessage());
  84. }
  85. }
  86.  
  87. static ResultSet execQueryd( Connection con, String query, String login) {
  88. try{
  89. PreparedStatement pstmt = con.prepareStatement(query);
  90. // Introducimos los parámetros dinámicos que son de tipo string
  91. pstmt.setString(1,login);
  92. //Ejecutamos la consulta preparada
  93. return pstmt.executeQuery();
  94. }catch( SQLException e ){
  95. System.err.println( "Query failed - " + e.getMessage()); return( null );
  96. }
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement