VB icon

Oracle database connectivity

Email
Submitted on: 1/5/2015 3:17:00 AM
By: Muthukumar Lakshmipathi (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: Java (JDK 1.5)
Views: 2177
 
     simple and effective program for connecting oracle with java and methods available for insertion, updation and deletion
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Oracle database connectivity
// Description:simple and effective program for connecting oracle with java and methods available for insertion, updation and deletion
// By: Muthukumar Lakshmipathi (from psc cd)
//
// Inputs:not required
//
// Side Effects:i used JDBC and ODBC bridge so i works in Windows only
//**************************************

/*
 programmer name:Muthukumar and Deva
steps for creating data source
control panel->Administrative Tools->Data Sources (ODBC)->
select user DSN tab then click add button
select Microsoft ODBC driver for Oracle
fill your data source (here it is patni)
and username and servername(oracle)
click of
Limitations:
 This code only works in Windows platform
*/
import java.sql.*;
class Connectivity
{
Connection conn;
 Statement stmt;
 PreparedStatement s1;
 void connect()
 {
try
{
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
 
 conn = DriverManager.getConnection("jdbc:odbc:patni","devarajan","devarajan");
 // patni--->datasource,devarajan---->username,devarajan->password
}
catch(Exception e)
{
 System.out.println("ee"+e.getMessage());
 
}
 } 
 
 void insertData(String nme,String roll)
 {
try
{
 s1=conn.prepareStatement("insert into stu values(?,?)");
s1.setString(1,nme);
s1.setString(2,roll);
s1.executeUpdate();
System.out.println("Record inserted"); 
s1.close();
}
catch(Exception s)
{
 System.out.println(s.getMessage());
}
 
 }
 
void updateData()
 {
try
{
stmt=conn.createStatement();
int i=stmt.executeUpdate("update stu set name='muthukumar4u' where roll='05mx23'" );
System.out.println("\t\t\t"+i +" record updated in updation funtion"); 
stmt.close();
}
catch(Exception s){
 System.out.println(s.getMessage());
}
 
 }
 void deleteData()
 {
try
{
stmt=conn.createStatement(); 
int i=stmt.executeUpdate("delete from stu where name='muthukumar4u'" );
System.out.println(i+" records Deleted"); 
stmt.close();
}
catch(Exception s){
 System.out.println(s.getMessage());
}
 }
 void displayData()
 {
try
{
stmt=conn.createStatement(); 
ResultSet rs = stmt.executeQuery("select * from stu"); 
System.out.println("The table contains:");
while(rs.next())
{
 System.out.println("Name:"+rs.getString("name"));
 System.out.print("Roll no:"+rs.getString("roll")); 
}
}
catch(Exception e)
{
 System.out.println(e.getMessage());
}
 }
}
public class JavatoOracle 
{
 
public static void main(String args[])
{
 Connectivity c=new Connectivity();
c.connect();
c.insertData("muthukumar", "05mx23");
c.displayData();
c.updateData();
c.displayData();
c.deleteData();
c.displayData();
}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.