Java Programming/Database Programming Learning | Java Programming/Database Programming Facts and Resources | DefaultLogic For Business

Database Programming

Java uses JDBC, an API that defines how a client may access a database. Its drivers may be installed first. [1][2]

Connecting to Microsoft Access databases

The following is used to open an ODBC connection to an Access database. Note that the username (and password, if applicable) are given in the DSN rather than the getConnection call. This is a MS Windows-only example, due to the requirement for the Microsoft Access Driver.

String path = "C:/example.mdb";
String dsn = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + path + ";UID=admin";
accessConn = DriverManager.getConnection(dsn, "", "");

Connecting to MySQL

try {
   Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/MySite?user=MyAccount&password=MyPassword");
} catch(SQLException e) { e.printStackTrace; }

Connecting to Oracle Database

This script returns the database size:

import java.sql.*; 
import oracle.jdbc.*; 
import oracle.sql.*; 

public class OracleDatabase {
  public static void main(String[] args) {
    try {
      DriverManager.registerDriver(new oracle.jdbc.OracleDriver);
      Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:MyDatabase", "MyAccount", "MyPassword"); 	
      String sql = "SELECT sum(bytes) from dba_segments;";
      Statement stmt = conn.createStatement;
    } catch(SQLException e) { e.printStackTrace; }

Connecting to PostgreSQL

try {
   Connection conn = DriverManager.getConnection("jdbc:postgresql:MyDatabase", "MyAccount", "MyPassword");
} catch(SQLException e) { e.printStackTrace; }

Known errors

package oracle.jdbc does not exist

If you have Oracle Database on your PC, just add to the classpath its following files[3]:

  • C:\oracle12\app\oracle\product\12.1.0\dbhome_1\jdbc\lib\ojdbc7.jar
  • C:\oracle12\app\oracle\product\12.1.0\dbhome_1\sqlj\lib\runtime12.jar

Otherwise, ojdbc7.jar can be downloaded from, and add it to the compilation. Eg:

javac -classpath ojdbc7.jar

ORA-28009: connection as SYS should be as SYSDBA or SYSOPER

Just add " as sysdba" after your Oracle account name, eg:

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:MyDatabase", "MyAccount as sysdba",


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at Create an account using LinkedIn to manage and organize your omni-channel knowledge. is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us