Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- //STEP 1. Import required packages
- import java.sql.*;
- import java.util.*;
- public class GrafoDB {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "org.postgresql.Driver";
- static final String DB_URL = "jdbc:postgresql://[::1]:5432/postgres";
- // Database credentials
- static final String USER = "postgres";
- static final String PASS = "ayelen";
- public void conexion() {
- Connection conn = null;
- Statement stmt = null;
- try{
- //STEP 2: Register JDBC driver
- Class.forName("org.postgresql.Driver");
- //STEP 3: Open a connection
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- //STEP 4: Execute a query
- System.out.println("Creating database...");
- stmt = conn.createStatement();
- System.out.println("Database created successfully...");
- }catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- e.printStackTrace();
- }finally{
- //finally block used to close resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Goodbye!");
- }//end main
- //end JDBCExample
- public static void main(String[] args){
- Connection conn = null;
- Statement stmt = null;
- try{
- //STEP 2: Register JDBC driver
- Class.forName("org.postgresql.Driver");
- //STEP 3: Open a connection
- System.out.println("Connecting to a selected database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Connected database successfully...");
- stmt = conn.createStatement();
- String sql = "CREATE TABLE IF NOT EXISTS grafoS " +
- "( nombre_grafo VARCHAR(255), " +
- " PRIMARY KEY ( nombre_grafo ))";
- stmt.executeUpdate(sql);
- sql = "DROP TABLE IF EXISTS nodos";
- stmt.executeUpdate(sql);
- sql = "CREATE TABLE IF NOT EXISTS nodos " +
- "( id_nodo VARCHAR(255) not NULL, " +
- " nombre_grafo VARCHAR(255) not NULL, " +
- " PRIMARY KEY ( id_nodo, nombre_grafo )," +
- " FOREIGN KEY ( nombre_grafo ) REFERENCES grafos(nombre_grafo))";
- stmt.executeUpdate(sql);
- sql = "DROP TABLE IF EXISTS arcos";
- stmt.executeUpdate(sql);
- sql = "CREATE TABLE arcos " +
- "( id_arco VARCHAR(255) not NULL, " +
- " nombre_grafo VARCHAR(255) not NULL, " +
- " nodo1 VARCHAR(255) not NULL, " +
- " nodo2 VARCHAR(255) not NULL," +
- " PRIMARY KEY ( id_arco, nombre_grafo )," +
- " FOREIGN KEY ( nombre_grafo ) REFERENCES grafos(nombre_grafo)," +
- " FOREIGN KEY ( nodo1, nombre_grafo ) REFERENCES nodos (id_nodo, nombre_grafo)," +
- " FOREIGN KEY ( nodo2, nombre_grafo ) REFERENCES nodos (id_nodo, nombre_grafo))";
- stmt.executeUpdate(sql);
- sql = "ALTER TABLE atributosnodos " +
- "( id_nodo VARCHAR not NULL, " +
- " nombre_grafo VARCHAR(255) not NULL, " +
- " nombre_atributo VARCHAR(255) not NULL, " +
- " valor VARCHAR(255), " +
- " PRIMARY KEY ( id_nodo, nombre_grafo, nombre_atributo )," +
- " FOREIGN KEY ( id_nodo, nombre_grafo) REFERENCES nodos (id_nodo, nombre_grafo))";
- // stmt.executeUpdate(sql);
- sql = "ALTER TABLE atributosarcos " +
- "( id_arco VARCHAR not NULL, " +
- " nombre_grafo VARCHAR(255) not NULL, " +
- " nombre_atributo VARCHAR(255) not NULL, " +
- " valor VARCHAR(255), " +
- " PRIMARY KEY ( id_arco, nombre_grafo, nombre_atributo )," +
- " FOREIGN KEY ( id_arco, nombre_grafo) REFERENCES arcos (id_arco, nombre_grafo))";
- //stmt.executeUpdate(sql);
- System.out.println("Created table in given database...");
- }catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- e.printStackTrace();
- }finally{
- //finally block used to close resources
- try{
- if(stmt!=null)
- conn.close();
- }catch(SQLException se){
- }// do nothing
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }
- }
- public static void agregarGrafo(Grafo g){
- Set<Arco> arcos = g.getArcos();
- Set<Nodo> nodos = g.getNodos();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement