Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package cliento;
- import java.sql.*;
- /**
- *
- * @author mat.aules
- */
- public class Cliento {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- String driver ="org.postgresql.Driver"; // Nombre de la classe que hará de conector con BD
- String url = "jdbc:postgresql://127.0.0.1/booktown"; // URL de la BD.
- String user = "postgres"; // Nombre del usuario que accede a la base de datos
- String pwd = "db1"; //password del usuario que accede a la BD
- try {
- Class.forName(driver); // Intentamos cargar el conector
- }catch (ClassNotFoundException e) {
- System.err.println("Can't load driver "+ e.getMessage());
- }
- try { // Intentamos establecer la conexión con la base de dades usando el connector
- Connection con = DriverManager.getConnection(url, user, pwd);
- System.err.println("Conection OK");
- //acces(con);
- consDinamica(con);
- con.close(); // Tanquem la connexió
- }catch(Exception e) {
- System.err.println("Connection Attempt failed");
- System.err.println(e.getMessage());
- }
- }
- static private void acces(Connection con){
- String query;
- query = "select * from books";
- ResultSet resul = execQuery(con, query);
- printResults(resul);
- }
- static private void consDinamica(Connection con){
- String query = "Select title from books where id=? ;" ;
- String entrada = "190";
- ResultSet resul = execQueryd(con, query, entrada);
- printResults(resul);
- }
- static ResultSet execQuery( Connection con, String query ){
- try{
- // A partir de la conexión con la base de datos creamos un objeto Statment
- Statement stmt = con.createStatement();
- System.out.println( query );
- return( stmt.executeQuery( query )); // el resultado de la consulta
- }catch( SQLException e) {
- System.err.println( "Query failed - " + e.getMessage()); return( null );
- }
- }
- static void printResults( ResultSet res ) {
- System.out.println( " Columna1 | Columna2" );
- System.out.println( "---------+------------------------------" );
- try{
- // Recorrem tot el resultSet per files mentre n'hi hagi
- while( res.next()) {
- System.out.print( res.getString( 1 ));
- System.out.print( " | ");
- System.out.print( res.getString( 2 ));
- System.out.println( "" );
- }
- }catch( SQLException e ) {
- System.err.println( "Fetch failed: " + e.getMessage());
- }
- }
- static ResultSet execQueryd( Connection con, String query, String login) {
- try{
- PreparedStatement pstmt = con.prepareStatement(query);
- // Introducimos los parámetros dinámicos que son de tipo string
- pstmt.setString(1,login);
- //Ejecutamos la consulta preparada
- return pstmt.executeQuery();
- }catch( SQLException e ){
- System.err.println( "Query failed - " + e.getMessage()); return( null );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement