如何有效地使用预备语句?

时间:2013-03-10 10:37:25

标签: java java-ee jdbc

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            更新价值我无法理解任何机构帮助我的原因            这就是为什么它更新id为102的年龄值...

3 个答案:

答案 0 :(得分:0)

尝试使用

con.setAutoCommit(false);


con.commit();

答案 1 :(得分:0)

在Oracle中,

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

即。插入或更新数据后,

您应该显式提交操作,以便在oracle db中保留数据。

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

您可以在代码中检查此问题吗?

你应该使用 -

在代码开头

con.setAutoCommit(false);

con.commit(); to commit the data

答案 2 :(得分: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");

        }