CrazyEngineers
  • How to connect java with oracle 10g?

    krishbcs

    Member

    Updated: Oct 26, 2024
    Views: 1.0K
    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]
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • rahul69

    MemberJan 27, 2013

    Hi there! I think u need to use JDBC for your purpose. Make a patient reading about it. For start, check out this link : <a href="https://docs.oracle.com/cd/B19306_01/java.102/b14355/overvw.htm#CJAHEFFI" target="_blank" rel="nofollow noopener noreferrer">Introducing JDBC</a>
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 27, 2013

    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.



    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 29, 2013

    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: <Default>--------------------
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 29, 2013

    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: <Default>--------------------
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 29, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 30, 2013

    hai, i almost complete my connection, but i can not clear this error please tell solution for following error:

    --------------------Configuration: <Default>--------------------
    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.<init>(Database.java:20)
    at addItem.<init>(addItem.java:21)
    at addItem.main(addItem.java:90)

    Process completed.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 30, 2013

    This seems , you are connecting to Microsoft access DB not the oracle db . right?
    then check this out.
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 31, 2013

    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: <Default>--------------------
    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.<init>(Database.java:20)
    at addItem.<init>(addItem.java:22)
    at addItem.main(addItem.java:79)

    Process completed.

    what did mistake here.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 31, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 31, 2013

    boss... I have installed Oracle 10g and java jdk1.7.0.

    you mean Oracle jdbc driver?

    After download this what i do?
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 31, 2013

    boss... I have installed Oracle 10g and java jdk1.7.0.

    you mean Oracle jdbc driver?

    After download this what i do?
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberJan 31, 2013

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJan 31, 2013

    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
    <a href="https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html" target="_blank" rel="nofollow noopener noreferrer">OracleDriver (Oracle ® Database JDBC API Reference)</a>

    Here is full example:#-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberFeb 5, 2013

    thanks boss i have connected successfully and also can inserted record.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register