preparedStatement.execute()方法的卡死解决


在oracle中,preparedStatement.execute()常常有卡死现象;

一个被忽略的原因就是此数据库多处被连接,并且被操作着。

因此你必须将目前这个方法放在外面或者前面执行。

因为这样,在当前数据改变之后,其他连接再执行,就不会有什么commit的问题存在了。

eg:


import java.sql.*;

public class Delete_date {

    public static void delete(Long id) throws Exception{

        String url = "jdbc·······";
        String user = "******";
        String password = "*****";

        Connection conn = null;

        PreparedStatement preparedStatement = null;

            //注册驱动(数据库的类型)
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //获取连接
            conn = DriverManager.getConnection(url, user, password);

            //执行SQL
            String sql = "delete from std  where ID="+id;

            //打印sql
            System.out.println("sql=" + sql);

            preparedStatement = conn.prepareStatement(sql);

            boolean result = preparedStatement.execute();

            //处理结果   preparedstatement execute()操作成功!但是返回false
            if (result) {
                System.out.println("操作失败");
            } else {
                System.out.println("操作成功");
            }

            //释放资源
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    preparedStatement = null;   //gc处理
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    conn = null;    //gc处理
                }
            }

    }
}
下面的,就是我之前所说的,把它放在其他连接之前或者之外,因为上面有close()等方法(这样做,其实类似给予了优先级。)
try {
   Delete_date.delete(id);
} catch (Exception e) {
   e.printStackTrace();
}
使用preparedStatement.execute()预处理,提高数据库操作性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值