0
    import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
class Jdbc1
{
    public static void main(String[] args) throws Exception
    {
        JdbcOdbcDriver jd=new JdbcOdbcDriver();
        DriverManager.registerDriver(jd);
        Connection con=DriverManager.getConnection("jdbc:odbc:sai123","SYSTEM","sai123");
        Statement st=con.createStatement();
        int res=st.executeUpdate("insert into A1 values('1','tomato','10')");
        System.out.println(res+"record is inserted..");
        st.close();
        con.close();
    }
}

and I have creates the table as:

CREATE TABLE A1(
INO INTEGER NOT NULL,
INAME CHAR(30) NOT NULL,
IPRICE INTEGER NOT NULL
);

When I compile and run the java prog it says "1 record inserted.." but when I open my table in SQLdeveloper I dont find any updates in the table. They are all null! I have also set the classpath for ojdbc-6.jar. What mistake am I doing herE? Are there any other files that I should copy and paste? I am just a beginner so kindly please help. And how does my program in the Editplus direct the updates directly into the table created in SQL. What path does it follow?

8
  • 1
    Please don't create tables in the SYS or SYSTEM account... Commented Jun 25, 2013 at 13:00
  • but that is how I can connect to the oracle database isnt it? Commented Jun 25, 2013 at 13:01
  • Don't put single quote marks around integers in your INSERT statement. Commented Jun 25, 2013 at 13:33
  • You can, but you should only do admin stuff as SYSTEM (see docs.oracle.com/cd/E11882_01/server.112/e25789/…), and especially don't create tables there. It's very difficult to get rid of the space you requested for your test tables. Do you need instructions how to create a user? Commented Jun 25, 2013 at 13:39
  • yeah that'd surely help..please tell me how! Commented Jun 25, 2013 at 13:44

3 Answers 3

4

Oracle has auto-commit set to false by default. Either set autocommit to true from Oracle Sql Developer (or code) or commit your changes from code:

//before con.close()
con.commit();

Or if you want to set to auto-commit:

//after creating Connection
conn.setAutoCommit(true);
Sign up to request clarification or add additional context in comments.

6 Comments

Ive added that..its still the same .
The second way is just commit transaction after executeUpdate(). This is done by conn.commit()
@SaiKrishna if it works, please consider accepting the answer, you and m3th0dman will get reputation points :-)
What does SELECT * FROM DBA_TABLES WHERE table_name='A1' say in SQLDeveloper?
So there are many different tables with the name 'A1' in your database. Maybe you insert into one of them and look at another one, as @Sourav suggests. Look at the user 'SYSTEM', and it's table 'A1', this is the one you use in your Java example...
|
0

Try committing the Record by calling conn.commit();

also refer the Why Execute Update Says, one row updated on insert query

the final code for main should look like public static void main(String[] args) throws Exception
{
JdbcOdbcDriver jd=new JdbcOdbcDriver();
DriverManager.registerDriver(jd);
Connection con=DriverManager.getConnection("jdbc:odbc:sai123","SYSTEM","sai123");
Statement st=con.createStatement();
int res=st.executeUpdate("insert into A1 values('1','tomato','10')");
System.out.println(res+"record is inserted..");
con.commit();
st.close();
con.close();
}

1 Comment

Are you sure you are checking the correct table under correct schema and server ?
0

Is it possible that your Java code and SQLDeveloper are referring to different schema? I would suggest to do the following

  1. Make INO the Primary Key of the table
  2. Put con.commit(); between int res=st.executeUpdate("insert into A1 values('1','tomato','10')"); and System.out.println(res+"record is inserted..");
  3. Run the program multiple times to check if you are getting Primary Key violation exception. This will tell you if your program is inserting to a table you are not looking at.

1 Comment

And how does my program in the Editplus direct the updates directly into the table created in SQL. What path does it follow?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.