How to connect java with oracle 10g?

i want to connect java and oracle. i can create design in java and also create and insert table using sql in oracle. now i need to connect both of them. any one can help?[URGENT]

Replies

  • rahul69
    rahul69
    Hi there! I think u need to use JDBC for your purpose. Make a patient reading about it. For start, check out this link : Introducing JDBC
  • Anoop Kumar
    Anoop Kumar
    import java.sql.DriverManager;
     
    import java.sql.Connection;
     
    import java.sql.SQLException;
     
    public class OracleJDBC {
     
    public static void main(String[] args) {
     
    Connection conn= null;
     
    try {
     
    Class.forName("oracle.jdbc.driver.OracleDriver");
     
    conn= DriverManager.getConnection("jdbc:0racle:thin@localhost:1521:DB_NAME", "username", "password");
     
    }catch (SQLException e) {
     
    e.printStackTrace();
     
    }
     
    }
     
    
    You might know above code. but first check your DB is up and running by openind SQLPlus from start menu and connecting by admin and password you given at installation time.
    If this program is ok then just use this connection (conn) object to insert into table and then fetch them.
    If you have some problem post here.



  • krishbcs
    krishbcs
    ianoop
    import java.sql.DriverManager;
     
    import java.sql.Connection;
     
    import java.sql.SQLException;
     
    public class OracleJDBC {
     
    public static void main(String[] args) {
     
    Connection conn= null;
     
    try {
     
    Class.forName("oracle.jdbc.driver.OracleDriver");
     
    conn= DriverManager.getConnection("jdbc:0racle:thin@localhost:1521:DB_NAME", "username", "password");
     
    }catch (SQLException e) {
     
    e.printStackTrace();
     
    }
     
    }
     
    
    You might know above code. but first check your DB is up and running by openind SQLPlus from start menu and connecting by admin and password you given at installation time.
    If this program is ok then just use this connection (conn) object to insert into table and then fetch them.
    If you have some problem post here.



    ianoop
    import java.sql.DriverManager;
     
    import java.sql.Connection;
     
    import java.sql.SQLException;
     
    public class OracleJDBC {
     
    public static void main(String[] args) {
     
    Connection conn= null;
     
    try {
     
    Class.forName("oracle.jdbc.driver.OracleDriver");
     
    conn= DriverManager.getConnection("jdbc:0racle:thin@localhost:1521:DB_NAME", "username", "password");
     
    }catch (SQLException e) {
     
    e.printStackTrace();
     
    }
     
    }
     
    
    You might know above code. but first check your DB is up and running by openind SQLPlus from start menu and connecting by admin and password you given at installation time.
    If this program is ok then just use this connection (conn) object to insert into table and then fetch them.
    If you have some problem post here.



    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.SQLException;

    public class OracleJDBC
    {

    public static void main(String[] args)
    {

    Connection conn= null;
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn= DriverManager.getConnection("jdbc:0racle:thin@localhost:1521:fea", "scott", "tiger");

    }catch (SQLException e)
    {
    e.printStackTrace();
    }
    }
    }
    while compile above program, following error occurs...................
    --------------------Configuration: --------------------
    C:\Documents and Settings\user\Desktop\Project\new\OracleJDBC.java:14: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    Class.forName("oracle.jdbc.driver.OracleDriver");
    ^
    1 error

    Process completed.
  • krishbcs
    krishbcs
    And also try this, BUt it also error:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Properties;
    import java.sql.PreparedStatement;

    /**
    * Simple Java Program to connect Oracle database by using Oracle JDBC thin driver
    * Make sure you have Oracle JDBC thin driver in your classpath before running this program
    * @author
    */
    public class OracleJdbcExample {

    public static void main(String args[]) throws SQLException {
    //URL of Oracle database server

    String url = "jdbc:0racle:thin@localhost:1521:fea";

    //properties for creating connection to Oracle database
    Properties props = new Properties();
    props.setProperty("user","scott");
    props.setProperty("password","tiger");

    //creating connection to Oracle database using JDBC
    Connection conn = DriverManager.getConnection(url,props);

    String sql ="select sysdate as current_day from dual";

    //creating PreparedStatement object to execute query
    PreparedStatement preStatement = conn.prepareStatement(sql);

    ResultSet result = preStatement.executeQuery();

    while(result.next()){
    System.out.println("Current Date from Oracle : " +result.getString("current_day"));
    }
    System.out.println("done");

    }
    }

    Compile Error is NO
    but Run time Error occur

    --------------------Configuration: --------------------
    Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:0racle:thin@localhost:1521:fea
    at java.sql.DriverManager.getConnection(DriverManager.java:640)
    at java.sql.DriverManager.getConnection(DriverManager.java:169)
    at OracleJdbcExample.main(OracleJdbcExample.java:26)

    Process completed.
  • Anoop Kumar
    Anoop Kumar
    Do you have classes12.jar in your class path. download it and paste it into jdk lib.
    #-Link-Snipped-#
    It should solve your problem.

    Tip: For any oracle/java error/exception just search google by that whole error and find answer on coderanch.com or #-Link-Snipped-#😀
    you can find 99.99 answers on these sites.
  • krishbcs
    krishbcs
    hai, i almost complete my connection, but i can not clear this error please tell solution for following error:

    --------------------Configuration: --------------------
    Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:322)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:173)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:200)
    at jack.Database.(Database.java:20)
    at addItem.(addItem.java:21)
    at addItem.main(addItem.java:90)

    Process completed.
  • Anoop Kumar
    Anoop Kumar
    This seems , you are connecting to Microsoft access DB not the oracle db . right?
    then check this out.
    #-Link-Snipped-#
  • krishbcs
    krishbcs
    yes this exception error cleared. but another one exception is following please try for me.


    this is my Database Connection program....(Database.java)
    package jack;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.PreparedStatement;


    public class Database
    {
    Connection cn;
    ResultSet rs;
    Statement stmt,stmt1;
    public Database()throws Exception
    {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    cn=DriverManager.getConnection("jdbc😲dbc:fea","system","mypassword");
    cn.setAutoCommit(true);
    stmt=cn.createStatement();
    stmt1=cn.createStatement();
    }

    public ResultSet Select(String qry)throws Exception
    {
    rs=stmt.executeQuery(qry);
    return rs;
    }

    }
    this is my addItem Program😔addItem.java)(Form Design) this has two text field one is ited Id and another one Item name. One save button, while i click save, i need to insert data to my database.

    import java.awt.Frame;
    import java.awt.Button;
    import java.awt.Label;
    import java.awt.TextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowListener;
    import java.awt.event.WindowEvent;
    import jack.Database;
    import jack.Same;
    import java.sql.ResultSet;


    public class addItem extends Frame implements ActionListener
    {
    Button save_item,clear_item,cancel_item;
    Label lid_item,lname_item;
    TextField tid_item,tname_item;

    ResultSet rs;
    Database db=new Database();
    String Qry,dat;

    public addItem()throws Exception
    {

    setTitle("Add Item");
    setLayout(null);


    lid_item=new Label("Item Id");
    add(lid_item);
    lid_item.setBounds(70,50,40,20);

    lname_item=new Label("Item Name");
    add(lname_item);
    lname_item.setBounds(70,100,65,20);

    tid_item=new TextField(20);
    add(tid_item);
    tid_item.setBounds(200,50,120,20);

    tname_item=new TextField(20);
    add(tname_item);
    tname_item.setBounds(200,100,120,20);

    save_item=new Button("Save");
    add(save_item);
    save_item.setBounds(100,150,50,20);

    clear_item=new Button("Clear");
    add(clear_item);
    clear_item.setBounds(200,150,50,20);

    save_item.addActionListener(this);



    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we)
    { dispose(); }
    });

    setSize(400,200);
    setVisible(true);
    setLocation(400,200);
    }
    public void actionPerformed(ActionEvent ae)
    {
    int no=Integer.parseInt(tid_item.getText());
    String name=tname_item.getText();
    Same s=new Same();
    s.summa(no,name); // this is method from Same.java
    dispose();
    }

    public static void main(String args[])throws Exception
    {
    new addItem();

    }
    }


    and also i use following to support this process😔Same.java)

    package jack;
    import java.sql.ResultSet;
    import jack.Database;



    public class Same
    {
    ResultSet rs;
    Database db;
    String Qry,dat;
    public void summa(int no,String name)
    {
    try
    {
    db=new Database();
    Qry="insert into rawmaterial values("+no+",'"+name+"')";
    System.out.println(Qry);
    rs=db.Select(Qry);
    rs.next();
    dat=rs.getString(1);
    }
    catch(Exception e)
    {
    }

    }
    }

    first i compiled Database.java program: process completed.
    second i compiled Same.java program: process completed.
    third i compiled addItem.java program: process completed.

    while i run third program the following error come:

    --------------------Configuration: --------------------
    Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS:could not resolve the connect identifier specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:322)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:173)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:200)
    at jack.Database.(Database.java:20)
    at addItem.(addItem.java:22)
    at addItem.main(addItem.java:79)

    Process completed.

    what did mistake here.
  • Anoop Kumar
    Anoop Kumar
    This driver is not used to connect oracle driver.
    Which database you are using. if oracle then first try to connect using SqlPlus from program menu. then use code from my first post. or you can find oracle driver code on google easily.
  • krishbcs
    krishbcs
    boss... I have installed Oracle 10g and java jdk1.7.0.

    you mean Oracle jdbc driver?

    After download this what i do?
  • krishbcs
    krishbcs
    boss... I have installed Oracle 10g and java jdk1.7.0.

    you mean Oracle jdbc driver?

    After download this what i do?
  • krishbcs
    krishbcs
    ianoop
    This driver is not used to connect oracle driver.
    Which database you are using. if oracle then first try to connect using SqlPlus from program menu. then use code from my first post. or you can find oracle driver code on google easily.
    using sql plus i created table only. Using sql plus how to connect with java?
  • Anoop Kumar
    Anoop Kumar
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    The above line used to connect  bridge between jdbc and odbc which is used for Microsoft access.
     
    Class.forName("oracle.jdbc.OracleDriver")
    used to connect Oracle database.
    
    have patience to read following
    OracleDriver (Oracle ® Database JDBC API Reference)

    Here is full example:#-Link-Snipped-#
  • krishbcs
    krishbcs
    thanks boss i have connected successfully and also can inserted record.

You are reading an archived discussion.

Related Posts

Codeproject had organized a world wide article competition to teach the App development fundamental. Here is the link for the competition.​ https://www.codeproject.com/competitions/611/Ultrabook-Article-competition The good thing is that I finally managed...
I want my project to operate on internet, like giving updates or notifications to user using internet on mobile phone or pc. for that i will need to implement tcp...
Hey i want to make a car which has a solar panel system on the roof so from that we can utilise the solar power to run a vehicle. I...
I am in my final semester of electronics and instrumentation. What are the different postgraduate courses i can opt for? Both in India and abroad. Please suggest. Thank You.
heyy. i need help with iis. i installed components of iis from control panel but still i cannot find inetmgr.exe or run it while trying URL https://localhost get following error...