|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.utils.db; |
| 18 | + |
| 19 | +import java.sql.PreparedStatement; |
| 20 | +import java.util.Date; |
| 21 | +import java.util.TimeZone; |
| 22 | + |
| 23 | +import com.cloud.utils.DateUtil; |
| 24 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 25 | + |
| 26 | +public class DbTestDao extends GenericDaoBase<DbTestVO, Long> implements GenericDao<DbTestVO, Long> { |
| 27 | + protected DbTestDao() { |
| 28 | + } |
| 29 | + |
| 30 | + @DB |
| 31 | + public void create(int fldInt, long fldLong, String fldString) { |
| 32 | + Transaction txn = Transaction.currentTxn(); |
| 33 | + PreparedStatement pstmt = null; |
| 34 | + try { |
| 35 | + txn.start(); |
| 36 | + pstmt = txn |
| 37 | + .prepareAutoCloseStatement("insert into cloud.test(fld_int, fld_long, fld_string) values(?, ?, ?)"); |
| 38 | + pstmt.setInt(1, fldInt); |
| 39 | + pstmt.setLong(2, fldLong); |
| 40 | + pstmt.setString(3, fldString); |
| 41 | + |
| 42 | + pstmt.executeUpdate(); |
| 43 | + txn.commit(); |
| 44 | + } catch (Exception e) { |
| 45 | + throw new CloudRuntimeException("Problem with creating a record in test table", e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @DB |
| 50 | + public void update(int fldInt, long fldLong, String fldString) { |
| 51 | + Transaction txn = Transaction.currentTxn(); |
| 52 | + PreparedStatement pstmt = null; |
| 53 | + try { |
| 54 | + txn.start(); |
| 55 | + pstmt = txn.prepareAutoCloseStatement("update cloud.test set fld_int=?, fld_long=? where fld_string=?"); |
| 56 | + pstmt.setInt(1, fldInt); |
| 57 | + pstmt.setLong(2, fldLong); |
| 58 | + pstmt.setString(3, fldString); |
| 59 | + |
| 60 | + pstmt.executeUpdate(); |
| 61 | + txn.commit(); |
| 62 | + } catch (Exception e) { |
| 63 | + throw new CloudRuntimeException("Problem with creating a record in test table", e); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments