daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jun 4th, 2017 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package ivrlib;
  7.  
  8. import java.beans.Statement;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *-
  18.  * @author Jose Ariel, Edwin Quijada
  19.  */
  20. public class Database {
  21.  
  22.      public static String driver,url,db,usr,pass,server,port;
  23.      public static String error_db="";
  24.      public static Connection conexion;
  25.  
  26.     public void Conectar(String port,String db, String usr, String pass, String server, String driver)
  27.     {    }
  28.      
  29.     public void Conectar() {
  30.  
  31.         // parametros para postgres
  32.         port   ="5432";
  33.         db     = "db_xxxxx";
  34.         usr    = "yyy";
  35.         pass   = "xxxx";
  36.         server = "x.x.x.x";
  37.         driver ="org.postgresql.Driver";
  38.         url    = "jdbc:postgresql://"+server+":"+port+"/"+db;
  39.  
  40.        
  41.  
  42.         try {
  43.             Class.forName(driver).newInstance();
  44.             conexion=DriverManager.getConnection(url,usr,pass);
  45.         } catch (Exception exc){
  46.              System.err.println("** Error de Base de datos en la conexion**\n"+exc.getMessage());
  47.         }
  48.     }
  49.  
  50.     private void loadDriver()
  51.     {
  52.         try
  53.         {
  54.             Class.forName("org.postgresql.Driver");
  55.         }
  56.         catch (ClassNotFoundException err)
  57.         {
  58.             System.err.println("** Could not load PostgreSQL driver **\n"+exc.getMessage());
  59.             System.exit(1);
  60.         }
  61.     }
  62.    
  63.     //metodo que retorna un resultset pasandole el query
  64.     public static ResultSet exec_sel(String query){
  65.         try {
  66.             java.sql.Statement comando = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  67.             ResultSet rs = comando.executeQuery(query);
  68.             return rs;
  69.         } catch (SQLException ex) {
  70.             Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
  71.             System.err.println("** Error de Base de datos, select **\n"+ex.getMessage());
  72.             return null;
  73.         }
  74.     }
  75.  
  76.     // metodo para ejecutar update, insert o delete
  77.     public void exec_sql(String query){
  78.         try {
  79.             java.sql.Statement stmt = conexion.createStatement();
  80.             stmt.executeUpdate(query);
  81.         } catch (SQLException ex) {
  82.             Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
  83.             System.err.println("** Error de Base de datos, insert, update, delete **\n"+ex.getMessage());
  84.         }
  85.     }
  86.  
  87.     //metodo para obtener la cantidad de registros
  88.     public int CantRegistros(ResultSet ResultSet){
  89.         try {
  90.              int int_cantreg=0;
  91.              while(ResultSet.next()){
  92.                  int_cantreg++;
  93.              }
  94.              return int_cantreg;
  95.          } catch (SQLException ex) {
  96.             Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
  97.             return 0;
  98.         }
  99.     }
  100.  
  101.     public Connection getConexion(){
  102.         return conexion;
  103.     }
  104.  
  105.     public void CerrarConexion() {
  106.         try {
  107.  
  108.             conexion.close();
  109.             conexion = null;
  110.         } catch (SQLException ex) {
  111.             Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
  112.         }
  113.     }
  114. }
  115.  
  116.  
  117. When I compile everything is fine but when I run I get this error::
  118. D:\Asterisk-Java\test\ivr>java DBtest
  119. LLamando una clase que esta un mismo paquete
  120. ** Error de Base de datos **
  121. org.postgresql.Driver
  122. Exception in thread "main" java.lang.NullPointerException
  123.         at ivrlib.Database.exec_sel(Database.java:61)
  124.         at DBtest.main(DBtest.java:19)
  125.  
  126. I use this class in DBtest I am a mewbie using JAVA
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top