Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. package application;
  2. //STEP 1. Import required packages
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6. public class GrafoDB {
  7.     // JDBC driver name and database URL
  8.     static final String JDBC_DRIVER = "org.postgresql.Driver";  
  9.     static final String DB_URL = "jdbc:postgresql://[::1]:5432/postgres";
  10.  
  11.     //  Database credentials
  12.     static final String USER = "postgres";
  13.     static final String PASS = "ayelen";
  14.  
  15.     public void conexion() {
  16.         Connection conn = null;
  17.         Statement stmt = null;
  18.         try{
  19.             //STEP 2: Register JDBC driver
  20.             Class.forName("org.postgresql.Driver");
  21.  
  22.             //STEP 3: Open a connection
  23.             System.out.println("Connecting to database...");
  24.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  25.  
  26.             //STEP 4: Execute a query
  27.             System.out.println("Creating database...");
  28.             stmt = conn.createStatement();
  29.  
  30.             System.out.println("Database created successfully...");
  31.         }catch(SQLException se){
  32.             //Handle errors for JDBC
  33.             se.printStackTrace();
  34.         }catch(Exception e){
  35.             //Handle errors for Class.forName
  36.             e.printStackTrace();
  37.         }finally{
  38.             //finally block used to close resources
  39.             try{
  40.                 if(stmt!=null)
  41.                     stmt.close();
  42.             }catch(SQLException se2){
  43.             }// nothing we can do
  44.             try{
  45.                 if(conn!=null)
  46.                     conn.close();
  47.             }catch(SQLException se){
  48.                 se.printStackTrace();
  49.             }//end finally try
  50.         }//end try
  51.         System.out.println("Goodbye!");
  52.     }//end main
  53.     //end JDBCExample
  54.  
  55.     public static void main(String[] args){
  56.  
  57.         Connection conn = null;
  58.         Statement stmt = null;
  59.         try{
  60.             //STEP 2: Register JDBC driver
  61.             Class.forName("org.postgresql.Driver");
  62.  
  63.             //STEP 3: Open a connection
  64.             System.out.println("Connecting to a selected database...");
  65.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  66.             System.out.println("Connected database successfully...");
  67.             stmt = conn.createStatement();
  68.  
  69.             String sql = "CREATE TABLE IF NOT EXISTS grafoS " +
  70.                     "( nombre_grafo VARCHAR(255), " +
  71.                     " PRIMARY KEY ( nombre_grafo ))";
  72.  
  73.             stmt.executeUpdate(sql);
  74.  
  75.             sql = "DROP TABLE IF EXISTS nodos";
  76.             stmt.executeUpdate(sql);
  77.            
  78.             sql = "CREATE TABLE IF NOT EXISTS nodos " +
  79.                     "( id_nodo VARCHAR(255) not NULL, " +
  80.                     " nombre_grafo VARCHAR(255) not NULL, " +
  81.                     " PRIMARY KEY ( id_nodo, nombre_grafo )," +
  82.                     " FOREIGN KEY ( nombre_grafo )  REFERENCES grafos(nombre_grafo))";
  83.                        
  84.             stmt.executeUpdate(sql);
  85.  
  86.             sql = "DROP TABLE IF EXISTS arcos";
  87.             stmt.executeUpdate(sql);
  88.            
  89.             sql = "CREATE TABLE arcos " +
  90.                     "( id_arco VARCHAR(255) not NULL, " +
  91.                     " nombre_grafo VARCHAR(255) not NULL, " +
  92.                     " nodo1 VARCHAR(255) not NULL, " +
  93.                     " nodo2 VARCHAR(255) not NULL," +
  94.                     " PRIMARY KEY ( id_arco, nombre_grafo )," +
  95.                     " FOREIGN KEY ( nombre_grafo )  REFERENCES grafos(nombre_grafo)," +
  96.                     " FOREIGN KEY ( nodo1, nombre_grafo )   REFERENCES nodos (id_nodo, nombre_grafo)," +
  97.                     " FOREIGN KEY ( nodo2, nombre_grafo )   REFERENCES nodos (id_nodo, nombre_grafo))";
  98.  
  99.             stmt.executeUpdate(sql);
  100.            
  101.             sql = "ALTER TABLE atributosnodos " +
  102.                     "( id_nodo VARCHAR not NULL, " +
  103.                     " nombre_grafo VARCHAR(255) not NULL, " +
  104.                     " nombre_atributo VARCHAR(255) not NULL, " +
  105.                     " valor VARCHAR(255), " +
  106.                     " PRIMARY KEY ( id_nodo, nombre_grafo, nombre_atributo )," +
  107.                     " FOREIGN KEY ( id_nodo, nombre_grafo)  REFERENCES nodos (id_nodo, nombre_grafo))";
  108.            
  109.         //  stmt.executeUpdate(sql);
  110.            
  111.             sql = "ALTER TABLE atributosarcos " +
  112.                     "( id_arco VARCHAR not NULL, " +
  113.                     " nombre_grafo VARCHAR(255) not NULL, " +
  114.                     " nombre_atributo VARCHAR(255) not NULL, " +
  115.                     " valor VARCHAR(255), " +
  116.                     " PRIMARY KEY ( id_arco, nombre_grafo, nombre_atributo )," +
  117.                     " FOREIGN KEY ( id_arco, nombre_grafo)  REFERENCES arcos (id_arco, nombre_grafo))";
  118.            
  119.             //stmt.executeUpdate(sql);
  120.            
  121.                     System.out.println("Created table in given database...");
  122.         }catch(SQLException se){
  123.             //Handle errors for JDBC
  124.             se.printStackTrace();
  125.         }catch(Exception e){
  126.             //Handle errors for Class.forName
  127.             e.printStackTrace();
  128.         }finally{
  129.             //finally block used to close resources
  130.             try{
  131.                 if(stmt!=null)
  132.                     conn.close();
  133.             }catch(SQLException se){
  134.             }// do nothing
  135.             try{
  136.                 if(conn!=null)
  137.                     conn.close();
  138.             }catch(SQLException se){
  139.                 se.printStackTrace();
  140.             }//end finally try
  141.         }
  142.     }
  143.  
  144.  
  145.     public static void agregarGrafo(Grafo g){
  146.  
  147.         Set<Arco> arcos = g.getArcos();
  148.  
  149.         Set<Nodo> nodos = g.getNodos();
  150.  
  151.  
  152.  
  153.     }
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement