Connect to oracle database in java language
Tech-Today

Connect to oracle database in java language


This code connects to an oracle database using java programming language.

This to remember:
1.) You must add ojdbc14.jar to the project's build path.
2.) Oracle's driver name: oracle.jdbc.driver.OracleDriver
3.) Connection string: jdbc:oracle:client:@server:port:database
Example: jdbc:oracle:thin:@localhost:1521:xe
4.) It is assumed that we have a table employee(firstname(string), lastname(string)) in the database.

package com.kbs.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Runner {
public static void main(String args[]) throws Exception {
Class.forName ("oracle.jdbc.driver.OracleDriver");

Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe", "demo", "demo");
// @machineName:port:SID, userid, password
try {
Statement stmt = conn.createStatement();
try {
ResultSet rset = stmt.executeQuery("select firstname from employee");
try {
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
} finally {
try { rset.close(); } catch (Exception ignore) {}
}
} finally {
try { stmt.close(); } catch (Exception ignore) {}
}
} finally {
try { conn.close(); } catch (Exception ignore) {}
}

}
}




- Database
MSSQLMSSQL equivalent of DESC in MYSQLPostgresqlChange postgresql's postgres passwordHow to install hstore in postgresql 9.1JDBC provider setting in java for postgresql, mysql and oracleHow to alter the sequence value in postgresqlOracleHow to change...

- How To Setup Toad Oracle Freeware
The following article will help us in configuring our Toad Oracle Freeware with Oracle XE database. For development purposes I normally use XE so it's light.  Steps: Download and install Oracle XE - I use 10.2.xI installed in: C:\java\oraclexeDownload...

- Jdbc Provider Setting In Java For Postgresql, Mysql And Oracle
Below are the summary of connection settings for differenct jdbc provider: Database Connection Url Driver Class Hibernate Dialect Cache Provider Class PostgreSQL jdbc:postgresql://[host]:[port]/[db] org.postgresql.Driver org.hibernate.dialect.PostgreSQLDialect...

- How To Connect Msaccess Database From Java Using Jdbc Driver
The method is almost similar to my previous code, and like that it's better to explain it by code: private static Connection getConnection() { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:anime"; //anime is the database...

- How To Create And Call A Database Routine (stored Procedure/function) Using A Jdbc Driver In Java
How to create and call a database routine (stored procedure/function) using a jdbc driver in java Stored procedure/function is a routine that is defined and executed in the database itself. Minimizing the time of execution, since the code is already residing/compiled...



Tech-Today








.