Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Java Programming/Database Programming

From Wikibooks, open books for an open world
<Java Programming
NetworkingJava Programming
Database Programming
Regular Expressions
NavigateAdvanced topic:()

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

Connecting toMicrosoft Access databases

[edit |edit source]

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

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

Connecting toMySQL

[edit |edit source]
try{Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost/MySite?user=MyAccount&password=MyPassword");conn.close();}catch(SQLExceptione){e.printStackTrace();}

Connecting toOracle Database

[edit |edit source]

This script returns the database size:

importjava.sql.*;importjava.io.*;importoracle.jdbc.*;importoracle.sql.*;publicclassOracleDatabase{publicstaticvoidmain(String[]args){try{DriverManager.registerDriver(neworacle.jdbc.OracleDriver());Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:MyDatabase","MyAccount","MyPassword");conn.setAutoCommit(true);Stringsql="SELECT sum(bytes) from dba_segments;";Statementstmt=conn.createStatement();stmt.execute(sql);stmt.close();conn.close();}catch(SQLExceptione){e.printStackTrace();}}}

Connecting toPostgreSQL

[edit |edit source]
try{Class.forName("org.postgresql.Driver");Connectionconn=DriverManager.getConnection("jdbc:postgresql:MyDatabase","MyAccount","MyPassword");conn.close();}catch(SQLExceptione){e.printStackTrace();}

Known errors

[edit |edit source]

package oracle.jdbc does not exist

[edit |edit source]

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 fromhttp://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

[edit |edit source]

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

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

References

[edit |edit source]
Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Database_Programming&oldid=3675925"
Category:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp