DBUpdate.java
00001 package edu.virtualschool.jwaa.dbms;
00002
00003 import java.sql.PreparedStatement;
00004 import java.sql.ResultSet;
00005 import java.sql.SQLException;
00006 import java.sql.Timestamp;
00007
00008 import edu.virtualschool.jwaa.dbms.DB.Fault;
00009
00010 public class DBUpdate
00011 {
00012 final String sql;
00013 int index;
00014 PreparedStatement stmt;
00015
00023 DBUpdate(DB dbms, String sql)
00024 throws DB.Fault
00025 {
00026 try
00027 {
00028 this.sql = sql;
00029 this.stmt = dbms.connection.prepareStatement(sql);
00030 index = 0;
00031 }
00032 catch (SQLException e)
00033 {
00034 throw new DB.Fault(sql, e);
00035 }
00036 }
00037 public int executeUpdate() throws DB.Fault
00038 {
00039 try
00040 {
00041 int i = stmt.executeUpdate();
00042 return i;
00043 }
00044 catch (SQLException e)
00045 {
00046 throw new DB.Fault(e, e);
00047 }
00048 }
00049 public void close() throws DB.Fault
00050 {
00051 try
00052 {
00053 stmt.close();
00054 }
00055 catch (SQLException e)
00056 {
00057 throw new DB.Fault(e, e);
00058 }
00059 }
00060 public final long getRecordNumber() throws Fault
00061 {
00062 try
00063 {
00064 ResultSet rs = stmt.getGeneratedKeys();
00065 if (rs.next())
00066 return rs.getLong(1);
00067 }
00068 catch (Throwable e) {}
00069 throw new Fault(this + " couldn't get record number");
00070 }
00071
00072 public void setString(Object s) throws DB.Fault
00073 {
00074 try
00075 {
00076 stmt.setString(++index, s.toString());
00077 }
00078 catch (SQLException e)
00079 {
00080 throw new DB.Fault(e, e);
00081 }
00082 }
00083 public void setLong(long s) throws DB.Fault
00084 {
00085 try
00086 {
00087 stmt.setLong(++index, s);
00088 }
00089 catch (SQLException e)
00090 {
00091 throw new DB.Fault(e, e);
00092 }
00093 }
00094 public void setDouble(double s) throws DB.Fault
00095 {
00096 try
00097 {
00098 stmt.setDouble(++index, s);
00099 }
00100 catch (SQLException e)
00101 {
00102 throw new DB.Fault(e, e);
00103 }
00104 }
00105 public void setTimestamp(Timestamp s) throws DB.Fault
00106 {
00107 try
00108 {
00109 stmt.setTimestamp(++index, s);
00110 }
00111 catch (SQLException e)
00112 {
00113 throw new DB.Fault(e, e);
00114 }
00115 }
00116 public void setObject(Object s) throws DB.Fault
00117 {
00118 try
00119 {
00120 stmt.setObject(++index, s);
00121 }
00122 catch (SQLException e)
00123 {
00124 throw new DB.Fault(e, e);
00125 }
00126 }
00127 public void setBytes(byte[] s) throws DB.Fault
00128 {
00129 try
00130 {
00131 stmt.setBytes(++index, s);
00132 }
00133 catch (SQLException e)
00134 {
00135 throw new DB.Fault(e, e);
00136 }
00137 }
00138
00139 }