GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main Page
  • Help
  • Browse
  • Cookbook
  • Wikijunior
  • Featured books
  • Recent changes
  • Special pages
  • Random book
  • Using Wikibooks
  • Reading room forum
  • Community portal
  • Help out!
  • Policies and guidelines

This view

  • Original page
  • New address

GeezerVison is showing en.wikibooks.org — Java Programming/Database Programming - Wikibooks, open books for an open world

< Java Programming
Navigate Advanced topic:
  • Networking
  • Database programming
  • Regular Expressions
  • Libraries, extensions and frameworks
  • 3D programming
  • Java Native Interface
  • Invoking C
  • Foreign Function and Memory API
  • Byte Code

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.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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 {
   Class.forName("com.mysql.jdbc.Driver");
   Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/MySite?user=MyAccount&password=MyPassword");
   conn.close();
} catch(SQLException e) { e.printStackTrace(); }

Connecting to Oracle Database

This script returns the database size:

import java.sql.*; 
import java.io.*; 
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"); 	
      conn.setAutoCommit(true);
      String sql = "SELECT sum(bytes) from dba_segments;";
      Statement stmt = conn.createStatement();
      stmt.execute(sql); 
      stmt.close();
      conn.close();   
    } catch(SQLException e) { e.printStackTrace(); }
  }
}

Connecting to PostgreSQL

try {
   Class.forName("org.postgresql.Driver");
   Connection conn = DriverManager.getConnection("jdbc:postgresql:MyDatabase", "MyAccount", "MyPassword");
   conn.close();
} 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 http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html, and add it to the compilation. Eg:

javac MyClass.java -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",

References

  1. ↑ http://www.cloudera.com/documentation/archive/cdh/4-x/4-7-0/CDH4-Installation-Guide/cdh4ig_topic_13_7.html
  2. ↑ http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
  3. ↑ http://docs.oracle.com/database/121/IMURG/ch_appdev.htm#IMURG11551
Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Database_Programming&oldid=3675925"
Category:
  • Book:Java Programming
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.