Monday, 15 April 2013

Java: Connect to Database (Derby)


public class PersonDAO {

//AddressBookDB is the database name to which we want to connect
    private final String url = "jdbc:derby://localhost/C:/Users/Administrator/.netbeans-derby/AddressBookDB";

    PersonDAO() {
    }

    private Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            con = DriverManager.getConnection(url, "test", "test"); // test, test are the //user name and password for the database.
        } catch (Exception e) {
            System.out.println("Exception occured while connecting to the database: " + e.getMessage());
            e.printStackTrace();
        }
        return con;
    }
}

No comments:

Post a Comment