Java JDBC Connection connect to Derby Database

Description

Java JDBC Connection connect to Derby Database


import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
  public static void main(String[] args) {
    Driver derbyEmbeddedDriver = new org.apache.derby.jdbc.EmbeddedDriver(); 
    DriverManager.registerDriver(derbyEmbeddedDriver); 

    // Prepare the connection URL   
    String dbURL = "jdbc:derby:YourDBName;create=true;user=app;password=app"; 

    Connection conn = null; //from   w  ww  .  j  a v a 2 s . c o m

    try { 
        conn = DriverManager.getConnection(dbURL, "app", "app"); 
        System.out.println("Connected to database successfully"); 

        // Perform database activities here...   
    } catch(SQLException e) { 
        e.printStackTrace(); 
    } finally { 
        if (conn != null) { 
            try { 
                // Close the connection   
                conn.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
  }
}



PreviousNext

Related