Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to do database connectivity using java for Oracle database?

Description

Steps for creating a table and inserting data into it in oracle database have been give here. OracleTest.java file presents the connectivity code for database. It initially loads the diver (“sun.jdbc.odbc.JdbcOdbcDriver”) and creates the connection with the database using DSN (“user”). Then statement object is created for the established connection that executes the query (“select”) mentioned and produces the result of query execution (ResultSet).

Steps for setting classpath for jar files java using environment variable

Step 1
  • Microsoft ODBC for Oracle driver should be selected while setting DSN
Step 2
  • Database Creation in Oracle 10g

Database connectivity using java for Oracle database
Microsoft ODBC for Oracle driver should be selected while setting DSN
Database Creation in Oracle 10g
Database connectivity
Oracle database in Java
Step 3
  • Code execution

import java.sql.*;
class OracleTest {
public static void main(String args[]) {
String username="system";
String password="system";
try {
// loading driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Connection set up with database with dsn name as user
Connection c = DriverManager.getConnection("jdbc:odbc:user",username,password);
// Creating statement for the connection to use sql queries
Statement st = c.createStatement();
// Executing sql query using the created statement over the table user_details located in the database pointing by the dsn
ResultSet rs = st.executeQuery("SELECT * from user_details");
// Accessing the result of query execution
while(rs.next())
{
username = rs.getString(1);
System.out.println(username);
}
// Closing the statement and connection
st.close();
c.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
.