0
import java.sql.*;
public class Login
{
    public static void main(String args[]) throws SQLException
    {
        Connection con = null;
        PreparedStatement stmt = null;
        Statement st = null;
        try {
            Driver driver = new oracle.jdbc.driver.OracleDriver();
            DriverManager.registerDriver(driver);
            System.out.println("coneecting to the database:");
            con = DriverManager.getConnection("driverURL","usrr","pass");
            System.out.println("creating statement");
            String sql = "Update abhi SET age = ? WHERE id = ?";
            stmt = con.prepareStatement(sql);
            stmt.setInt(1, 35);
            stmt.setInt(2,102);
            int rows = stmt.executeUpdate();
            S.O.P("rows updated"+rows);
            stmt.close();
            String sql2;
            sql2 = "select * from abhi";
            st = con.createStatement();
            ResultSet rs = st.executeQuery(sql2);
            while(rs.next())
            {
                System.out.println("hi");
                int age = rs.getInt("age");
                System.out.println("age is"+age);
            }
            rs.close();
            stmt.close();
            con.close();
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (st != null)
                    st.close();
            }
            catch(Exception e) {
                 // nthing to do
            }
            try {
                if(con != null)
                    con.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("good bye johny");
    }
}

这是我的代码,它运行良好,没有任何错误......但是在我的数据库中,它的 nt 更新值我无法理解为什么有任何机构帮助我解决为什么它的 nt 更新 id 为 102 的年龄值 .. .

4

3 回答 3

0

在甲骨文中,

默认的数据库事务模式是两阶段提交。

即插入或更新数据后,

您应该明确提交操作,以将数据持久保存在您的 oracle 数据库中。

但是在这里,我没有在您的代码中看到任何提交。

你可以在你的代码中检查这个问题吗?

你应该使用 -

con.setAutoCommit(false);在代码的开头

con.commit(); to commit the data
于 2013-03-10T10:45:17.683 回答
0

尝试使用

con.setAutoCommit(false);


con.commit();
于 2013-03-10T10:41:30.670 回答
0

正如其他人提到的,尝试像这样使用连接提交

import java.sql.*;
public class Login
{
public static void main(String args[]) throws SQLException
{
    Connection con = null;
    PreparedStatement stmt = null;
    Statement st = null;
    try
    {
        Driver driver = new oracle.jdbc.driver.OracleDriver();
        DriverManager.registerDriver(driver);
        System.out.println("coneecting to the database:");

    con = DriverManager.getConnection("driverURL","usrr","pass");       
    con.setAutoCommit(false);
    System.out.println("creating statement");

    String sql = "Update abhi SET age = ? WHERE id = ?";

    stmt = con.prepareStatement(sql);

    stmt.setInt(1, 35);

    stmt.setInt(2,102);

    int rows =    stmt.executeUpdate();

    S.O.P("rows updated"+rows);

    stmt.close();
    con.commit();
    String sql2;

    sql2 = "select * from abhi";

    st = con.createStatement();

    ResultSet rs = st.executeQuery(sql2);

    while(rs.next())
    {
        System.out.println("hi");


        int age = rs.getInt("age");

        System.out.println("age is"+age);
    }

                 rs.close();

    stmt.close();

    con.close();
}

catch(SQLException e)
{
    e.printStackTrace();
}
catch (Exception e)
         {
    e.printStackTrace();    
}
finally
{
    try
    {
        if(st!=null)
            st.close();
    }

    catch(Exception e) 
    {
               // nthing to do

                 }
    try
    {
        if(con!=null)

        con.close();
    }
    catch (Exception e)
    {

                e.printStackTrace();

                 }


         }

       System.out.println("good bye johny");

        }
于 2013-03-10T14:08:56.040 回答