简体   繁体   English

我无法将记录插入到在oracle数据库中创建的表中。 我在做什么错?

[英]I am unable to insert a record into my table created in oracle database. What mistake am I doing?

    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. 当我编译并运行Java Prog时,它说“已插入1条记录。”但是当我在SQLdeveloper中打开表时,在表中找不到任何更新。 They are all null! 它们都是空的! I have also set the classpath for ojdbc-6.jar. 我还为ojdbc-6.jar设置了类路径。 What mistake am I doing herE? 我在做什么错E? 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. 以及我在Editplus中的程序如何将更新直接定向到用SQL创建的表中。 What path does it follow? 它走什么路?

Oracle has auto-commit set to false by default. Oracle默认将自动提交设置为false。 Either set autocommit to true from Oracle Sql Developer (or code) or commit your changes from code: 从Oracle Sql Developer(或代码)将autocommit设置为true或从代码提交更改:

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

Or if you want to set to auto-commit: 或者,如果您想要设置为自动提交:

//after creating Connection
conn.setAutoCommit(true);

Try committing the Record by calling conn.commit(); 尝试通过调用conn.commit();提交Record 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();
}
main的最终代码应类似于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();
}
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();
}

Is it possible that your Java code and SQLDeveloper are referring to different schema? 您的Java代码和SQLDeveloper是否可能引用了不同的架构? I would suggest to do the following 我建议做以下事情

  1. Make INO the Primary Key of the table 将INO作为表的主键
  2. Put con.commit(); con.commit(); between int res=st.executeUpdate("insert into A1 values('1','tomato','10')"); 之间int res=st.executeUpdate("insert into A1 values('1','tomato','10')"); and System.out.println(res+"record is inserted.."); 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. 这将告诉您程序是否正在插入您未查看的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM