public static void createConnectionPool() {
   Context context;
   try {
     context = new InitialContext();
     Class.forName("org.firebirdsql.jdbc.FBDriver");
     ds = (DataSource) context.lookup("java:comp/env/jdbc/PanelTrackDB");
     Connection c = ds.getConnection();
   } catch (NamingException e) {
     // TODO Auto-generated catch block
     errorLogger.error("An Error Occured:", e);
     e.printStackTrace();
   } catch (ClassNotFoundException e) {
     errorLogger.error("An Error Occured:", e);
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (SQLException e) {
     errorLogger.error("An Error Occured:", e);
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
예제 #2
0
  public static Connection obtenerConexion() throws NamingException {
    Connection conexion = null;

    try {
      // Creamos un contexto jndi inicial
      // Este context no tiene nada que ver con
      // la etiqueta contexto donde esta el pool

      // Este context hace referencia un servicio de nombres
      // naming service
      Context ctx = new InitialContext();
      String nombreDelContextJNDI = "java:comp/env/";
      DataSource ds = (DataSource) ctx.lookup(nombreDelContextJNDI + "jdbc/MySqlPoolConections");

      conexion = ds.getConnection();

    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return conexion;
  }
예제 #3
0
  public static void main(String[] args) {
    Connection con = null;
    Statement stat = null;
    String s;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");

      //    con=DriverManager.getConnection("jdbc:oracle:oci8:@oracle9i","scott","tiger");

      //
      // con=DriverManager.getConnection("jdbc:oracle:thin:@saraswati:1521:oracle10g","scott","tiger");
      con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "scott", "tiger");

      /*Properties p=new Properties();
      p.put("user","scott");
      p.put("password","tiger");*/

      // Driver d=(Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

      // Connection con=d.connect("jdbc:odbc:oradsn",p);

      // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      // con=DriverManager.getConnection("jdbc:odbc:namadsn","scott","tiger");

      System.out.println("The connection is successful.");
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      stat = con.createStatement();
      s = "create table student(Rollno int,Name varchar2(50),serName varchar2(50))";
      stat.executeUpdate(s);
    } catch (SQLException e) {
      e.printStackTrace();
    }

    try {
      stat = con.createStatement();
      s = "insert into student values(1,'Namrata','Marathe')";
      stat.executeUpdate(s);
      s = "insert into student values(2,'Arjun','Marathe')";
      stat.executeUpdate(s);
      s = "insert into student values(3,'Rahul','Marathe')";
      stat.executeUpdate(s);

    } catch (SQLException e) {
      e.printStackTrace();
    }

    try {
      stat.close();
    } catch (SQLException e) {
    }

    System.out.println("create is successful:");

    try {
      stat = con.createStatement();
      s = "select * from student";
      ResultSet rset = stat.executeQuery(s);

      while (rset.next())
        System.out.println(rset.getInt(1) + " " + rset.getString(2) + "    " + rset.getString(3));
    } catch (SQLException e) {
      e.printStackTrace();
    }

    try {
      stat.close();
    } catch (SQLException se) {

    }
  }