Basic steps for working with a database using JDBC

  • Registration of drivers;
  • Establishing a connection to the database;
  • Creation of query(s) to the database;
  • Executing the query(s) to the database;
  • Processing the result(s);
  • Closing the connection to the database.

Register the JDBC driver

Driver registration can be done in several ways:

  • java.sql.DriverManager.registerDriver(%driver class object%)
  • Class.forName("fully qualified driver class name").NewInstance()
  • Class.forName("fully qualified driver class name")

Establish a connection to the database

The java.sql.DriverManager.getConnection(...) static call is used to establish a connection to the database.

The parameter can be passed:

  • Database URL

    static Connection getConnection(String url)
    

  • Database URL and set of properties to initialize

    static Connection getConnection(String url, Properties info)
    

  • Database URL, username and password

    static Connection getConnection(String url, String user, String password)
    

As a result of the call, a connection to the database will be established and an object of the java.sql.Connection class will be created - a kind of "session", within the context of which further work with the database will take place.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed