📄 oracletaccountdao.java
字号:
package dao.product.oracle;
import java.sql.*;
import java.util.*;
import dao.product.DbUtil;
import dao.product.TAccountDAO;
import bean.TAccount;
public class OracleTAccountDAO implements TAccountDAO {
public boolean add(TAccount account) {
DbUtil db = new OracleDbUtil();
Connection conn = db.getConnection();
String sql = null;
sql = "insert into TAccount (account_id,contact_person,contact_address,account_balance) values(?,?,?,?)";
try {
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setInt(1, account.getAccount_id());
prepStmt.setString(2, account.getContact_person());
prepStmt.setString(3, account.getContact_address());
prepStmt.setDouble(4, account.getAccount_balance());
prepStmt.execute();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
db.close();
}
}
public boolean delete(int id) {
DbUtil db = new OracleDbUtil();
Connection conn = db.getConnection();
String sql = "delete TAccount where account_id=?";
try {
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setInt(1, id);
prepStmt.execute();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
db.close();
}
}
public boolean update(TAccount account) {
DbUtil db = new OracleDbUtil();
Connection conn = db.getConnection();
String sql = "update TAccount set contact_person=?,contact_address=?,account_balance=? where account_id=?";
try {
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setString(1, account.getContact_person());
prepStmt.setString(2, account.getContact_address());
prepStmt.setDouble(3, account.getAccount_balance());
prepStmt.setInt(4, account.getAccount_id());
prepStmt.execute();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
db.close();
}
}
public TAccount findByID(int id) {
TAccount account = null;
DbUtil db = new OracleDbUtil();
Connection conn = db.getConnection();
String sql = "select account_id,contact_person,contact_address,account_balance from TAccount where account_id=?";
try {
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setInt(1, id);
ResultSet rs = prepStmt.executeQuery();
while(rs.next()) {
account = new TAccount();
account.setAccount_id(rs.getInt("account_id"));
account.setContact_person(rs.getString("contact_person"));
account.setContact_address(rs.getString("contact_address"));
account.setAccount_balance(rs.getDouble("account_balance"));
}
return account;
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
db.close();
}
}
public List findAll() {
List<TAccount> list = new ArrayList<TAccount>();
DbUtil db = new OracleDbUtil();
Connection conn = db.getConnection();
String sql = "select account_id,contact_person,contact_address,account_balance from TAccount";
try {
PreparedStatement prepStmt = conn.prepareStatement(sql);
ResultSet rs = prepStmt.executeQuery();
while(rs.next()) {
TAccount account = new TAccount();
account.setAccount_id(rs.getInt("account_id"));
account.setContact_person(rs.getString("contact_person"));
account.setContact_address("contact_address");
account.setAccount_balance(rs.getDouble("account_balance"));
list.add(account);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
db.close();
}
}
public static void main(String[] args) {
// TAccountDAO test = new TAccountDAO();
// TAccount t = new TAccount();
// t.setContact_address("hadian");
// t.setContact_person("tom");
// test.add(t);
// List list = new ArrayList();
// list = test.findAll();
// t.setAccount_id(14);
// t.setContact_address("hell");
// test.update(t);
// System.out.println("ss:"+t.getContact_address());
//// test.delete(10);
// System.out.println(((TAccount)list.get(0)).getContact_person());
// System.out.println(((TAccount)list.get(1)).getContact_person());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -